Reputation: 9753
main.lua
local physics = require("physics")
local actor = require("actor")
physics.start()
//here physic is not nil
local a = Actor.new(200, 200, physics)
actor.lua
function Actor:new(x, y, physic)
//here physic is nil
end
How come I cant pass the physics object into another module?
Upvotes: 0
Views: 83
Reputation: 29573
In Lua you have to be careful when you define a function in a table: do you use .
or :
. In actor.lua
you define Actor:new(x,y,phys)
which means there is an implicit self
parameter that is available in the body of Actor:new
; this self
will refer to the containing Actor
table, and is the first parameter in the call to Actor:new
. In main.lua
you call Actor.new(200, 200, physics)
: note the dot instead of colon, so the first parameter is 200, meaning self
will be 200, x
will be 200, and y
will be physics
, and phys
will be nil. You should change the call to new
or the definition of new
, one or the other. For example in main.lua
:
local a = Actor:new(200, 200, physics)
Note also that if your actor.lua
doesn't return anything then local actor
in main.lua
will be nil
. Looks like you probably defined Actor
table as a global so you are able to reference it in main.lua
.
Upvotes: 1