5 posts
Posted 24 April 2012 - 04:46 AM
API:
-- ninja_turtle.lua
ninja_turtle = {
current_position = {0, 0, 0},
rotation = 0,
-- __tostring = function (x) for i,v in pairs(x) do print(i, v) end
}
function ninja_turtle:new()
o = o or {}
setmetatable(o, self)
self.___index = self
return o
end
Code using the API:
-- doit
os.loadAPI("ninja_turtle.lua")
x = ninja_turtle:new{}
for i,v in pairs(x) do print (i, v) end
When running doit, I get this error: "doit:2: Attempt to index ? (a nil value)" which probably means that it can't find my ninja_turtle:new(), but why?
Thanks in advance,
454 posts
Location
London
Posted 24 April 2012 - 06:39 AM
You might need to remove the .lua extension from your API. If that doesn't work, make sure you're specifying the full path to it.
496 posts
Location
New Zealand
Posted 24 April 2012 - 07:12 AM
API:
-- ninja_turtle.lua
ninja_turtle = {
current_position = {0, 0, 0},
rotation = 0,
-- __tostring = function (x) for i,v in pairs(x) do print(i, v) end
}
function ninja_turtle:new()
o = o or {}
setmetatable(o, self)
self.___index = self
return o
end
Code using the API:
-- doit
os.loadAPI("ninja_turtle.lua")
x = ninja_turtle:new{}
for i,v in pairs(x) do print (i, v) end
When running doit, I get this error: "doit:2: Attempt to index ? (a nil value)" which probably means that it can't find my ninja_turtle:new(), but why?
Thanks in advance,
Like above remove the .lua extension,
Also the access the object it would be
ninja_turtle.ninja_turtle:new()
Because the file is in the global ninja_turtle variable
and then you ninja_turtle variable is inside that.
5 posts
Posted 30 April 2012 - 12:17 AM
Thanks guys!
Second thought, why remove the .lua? Since when do extensions mess with files?
1604 posts
Posted 30 April 2012 - 01:08 AM
The problem with the extension is that the API is loaded by it's file name, so when you need to call it you do:
<apiname>.<function>()
When you put an extension to the filename you have to call it like:
<api>.<extension>.<function>
and lua would take it as trying to index the variable "api", wich doesn't exist.
496 posts
Location
New Zealand
Posted 30 April 2012 - 04:51 AM
If you want to keep the extension or name with dots, you would have to access like this:
_G["My.API.File.lua"].coolFunction()
1604 posts
Posted 30 April 2012 - 08:20 PM
If you want to keep the extension or name with dots, you would have to access like this:
_G["My.API.File.lua"].coolFunction()
That would work, but it's not really nice to do :)/>/>.
@Nick: If you want to keep the extension you will have to do what tomass says in the programs that use the API, it will work, but why would you do it when you can rename the API file.
This is just an opinion, do whatever you want, after all it's your code :)/>/>