[Resolved] Access local variables from required-in modular function

Hi all,

I’m creating an adventure game and have reached a snag. The game is structured around ‘rooms’ (which are Storyboard lua files), each of which use the same touch logic for moving your character, opening the inventory, etc.

Rather than place the same redundant code in every storyboard file, I want to place the code for the room’s “touch” event in an external module. This works, but the touch event needs to access variables that are local to the storyboard file, things like the player character, the camera, etc. I thought that since I’m using “require” to place the touch event in a local function, this would work, but it seems not.

This is the relevant portion of the storyboard file:

[lua]-- Load common functions
local lunaCommon = require(“craters.luna_common”)

local Camera = nil
Camera = cameraActor.new(roomWidth, roomHeight)

– roomGroup is a display group that holds the scene
roomGroup.touch = lunaCommon.roomTouch
roomGroup:addEventListener(“touch”, roomGroup)[/lua]

The code in luna_common:

[lua]-- Basic room touches and inventory opening functionality
roomTouch = function(self, e)
– Camera movement, player movement, inventory opening, etc.
Camera.pan(e.x, e.y)
end[/lua]

When this code is run, I get an error: “attempt to index global ‘Camera’ (a nil value)”.

Is there any way to do this, or a better way to create a re-usable touch listener?

Thanks!

  • David [import]uid: 149111 topic_id: 31509 reply_id: 331509[/import]

I would just change the “local camera” to “camera”. /shrug. [import]uid: 147305 topic_id: 31509 reply_id: 125881[/import]

One of two solutions can work here, but there are always outside of the box solutions as well…

[lua]-- storyboard file
local lunaCommon = require ( “craters.luna_common” )
local Camera = cameraActor.new ( roomWidth, roomHeight )
roomGroup.camera = Camera
roomGroup.touch = lunaCommon.roomTouch
roomGroup:addEventListener(“touch”, roomGroup)

– craters.luna_common
roomTouch = function ( self, e )
self.camera.pan ( e.x, e.y )
end[/lua]
[lua]-- storyboard file
local lunaCommon = require ( “craters.luna_common” )
local Camera = cameraActor.new ( roomWidth, roomHeight )
lunaCommon.camera = Camera
roomGroup.touch = lunaCommon.roomTouch
roomGroup:addEventListener(“touch”, roomGroup)

– craters.luna_common
local lunaCommon = {}
lunaCommon.roomTouch = function ( self, e )
lunaCommon.camera.pan ( e.x, e.y )
end
return lunaCommon[/lua] [import]uid: 7721 topic_id: 31509 reply_id: 125887[/import]

@budershank: Thanks for the idea. I actually gave this direction a try, but ran into some problems, and ended up learning some (but not yet enough) about “_G”.

@ETdoFresh: Thanks! I used solution one, and it worked flawlessly. It never occurred to me to add it to the room group.

Thanks again!

  • David [import]uid: 149111 topic_id: 31509 reply_id: 125889[/import]

No Problem! Glad I could help! :slight_smile:

I like programming because there are no right or wrong answers! Some more efficient than others, but the possibilities are limitless! [import]uid: 7721 topic_id: 31509 reply_id: 125898[/import]

@ETdoFresh: Indeed, I think that’s both the most exciting and most frustrating thing about it, haha. :slight_smile: [import]uid: 149111 topic_id: 31509 reply_id: 125919[/import]

I would just change the “local camera” to “camera”. /shrug. [import]uid: 147305 topic_id: 31509 reply_id: 125881[/import]

One of two solutions can work here, but there are always outside of the box solutions as well…

[lua]-- storyboard file
local lunaCommon = require ( “craters.luna_common” )
local Camera = cameraActor.new ( roomWidth, roomHeight )
roomGroup.camera = Camera
roomGroup.touch = lunaCommon.roomTouch
roomGroup:addEventListener(“touch”, roomGroup)

– craters.luna_common
roomTouch = function ( self, e )
self.camera.pan ( e.x, e.y )
end[/lua]
[lua]-- storyboard file
local lunaCommon = require ( “craters.luna_common” )
local Camera = cameraActor.new ( roomWidth, roomHeight )
lunaCommon.camera = Camera
roomGroup.touch = lunaCommon.roomTouch
roomGroup:addEventListener(“touch”, roomGroup)

– craters.luna_common
local lunaCommon = {}
lunaCommon.roomTouch = function ( self, e )
lunaCommon.camera.pan ( e.x, e.y )
end
return lunaCommon[/lua] [import]uid: 7721 topic_id: 31509 reply_id: 125887[/import]

@budershank: Thanks for the idea. I actually gave this direction a try, but ran into some problems, and ended up learning some (but not yet enough) about “_G”.

@ETdoFresh: Thanks! I used solution one, and it worked flawlessly. It never occurred to me to add it to the room group.

Thanks again!

  • David [import]uid: 149111 topic_id: 31509 reply_id: 125889[/import]

No Problem! Glad I could help! :slight_smile:

I like programming because there are no right or wrong answers! Some more efficient than others, but the possibilities are limitless! [import]uid: 7721 topic_id: 31509 reply_id: 125898[/import]

@ETdoFresh: Indeed, I think that’s both the most exciting and most frustrating thing about it, haha. :slight_smile: [import]uid: 149111 topic_id: 31509 reply_id: 125919[/import]

Hi everyone,

I was reading this post cause i’m trying to make an adventure game. But at this point, i don’t know how to continue, i’m stuck where you start to pickin up objetcs, just don’t know how to make the inventory works.

I would aprreciate your help.

Thanks.

P.D. sorry for my english.

@elarmen: Welcome to the forums! You’d probably get more of a response if you start a new topic about this. I’m sure a lot of people have made something like an inventory for their game, but won’t see your post in this thread.

Good luck!

  • David

Hi everyone,

I was reading this post cause i’m trying to make an adventure game. But at this point, i don’t know how to continue, i’m stuck where you start to pickin up objetcs, just don’t know how to make the inventory works.

I would aprreciate your help.

Thanks.

P.D. sorry for my english.

@elarmen: Welcome to the forums! You’d probably get more of a response if you start a new topic about this. I’m sure a lot of people have made something like an inventory for their game, but won’t see your post in this thread.

Good luck!

  • David