can lua files send messages to each other?
If I have two classes. Class A and class B, and class A wants to send a message to class B to unlock additional content, what is the best approach to take? [import]uid: 166068 topic_id: 31328 reply_id: 331328[/import]
Yes there are a few ways to do this
The easiest way, but not the best use of memory
Is to setup a global variable that both classes can read
If using director it’s built in not sure about storyboard I never use it but I think it’s built in also [import]uid: 7911 topic_id: 31328 reply_id: 125276[/import]
Sorry trying to drive and type
What I do sometimes is create a class with variables that need to be accessed from different classes then just require it in the different classes [import]uid: 7911 topic_id: 31328 reply_id: 125277[/import]
This blog post:
http://www.coronalabs.com/blog/2012/08/07/managing-state-between-scenes/
talks about using the storyboard object (really just a Lua table) to store your information that needs shared between storyboard scenes.
Basically you have a table named “state”, if you follow the blog post. I tend to use “settings” for my table name, though I suspect that may be dangerous in the long run should Corona Labs decide to add a “settings” field to the storyboard object.
... in main.lua:
local storyboard = require("storyboard")
storyboard.settings = {}
storyboard.settings.fred = "barney"
.. in some j-random scene file:
local storyboard = require("storyboard")
local scene = storyboard.newScene()
...
function scene:enterScene( event )
local group = self.view
print(storyboard.settings.fred) -- outputs "barney"
end
Easy schmesy [import]uid: 19626 topic_id: 31328 reply_id: 125286[/import]
Thank you both for your feedback. I shall take a look into both approaches! [import]uid: 166068 topic_id: 31328 reply_id: 125335[/import]
Yes there are a few ways to do this
The easiest way, but not the best use of memory
Is to setup a global variable that both classes can read
If using director it’s built in not sure about storyboard I never use it but I think it’s built in also [import]uid: 7911 topic_id: 31328 reply_id: 125276[/import]
Sorry trying to drive and type
What I do sometimes is create a class with variables that need to be accessed from different classes then just require it in the different classes [import]uid: 7911 topic_id: 31328 reply_id: 125277[/import]
This blog post:
http://www.coronalabs.com/blog/2012/08/07/managing-state-between-scenes/
talks about using the storyboard object (really just a Lua table) to store your information that needs shared between storyboard scenes.
Basically you have a table named “state”, if you follow the blog post. I tend to use “settings” for my table name, though I suspect that may be dangerous in the long run should Corona Labs decide to add a “settings” field to the storyboard object.
... in main.lua:
local storyboard = require("storyboard")
storyboard.settings = {}
storyboard.settings.fred = "barney"
.. in some j-random scene file:
local storyboard = require("storyboard")
local scene = storyboard.newScene()
...
function scene:enterScene( event )
local group = self.view
print(storyboard.settings.fred) -- outputs "barney"
end
Easy schmesy [import]uid: 19626 topic_id: 31328 reply_id: 125286[/import]
Thank you both for your feedback. I shall take a look into both approaches! [import]uid: 166068 topic_id: 31328 reply_id: 125335[/import]