Public variables with composer vs. storyboard - question

Hello,

When using storyboard I could define a public variable at the beginning of the app like storyboard.variable1 = ‘abc’. Now I read I can pass a table with custom variables in it. Is there still any way to do it like storyboard? I only need to have 3 or 4 variables but I hate to pass a table each time.

Thanks,

Warren

Hi Warren,

Do you mean, what is the equal method in Composer? If so, that would be the “setVariable()” function (and the corresponding “getVariable()” function):

http://docs.coronalabs.com/daily/api/library/composer/setVariable.html

http://docs.coronalabs.com/daily/api/library/composer/getVariable.html

Hope this helps,

Brent

The problem with doing:  storyboard.myVariable = 10 is that it’s not safe.  We could come along and decide we want to have a member named .myVariable and then your app would break.  You have to protect your member’s by name spacing the variable names.   We at one point suggested using a table named “state” (storyboard.state.myVariable) with the assumption we would never use state.  But again, this isn’t a best practice because we could again start using it.  Composer adds a setVariable and getVariable method to provide you a safe way of doing this.

Regardless, it’s probably not as clean to do this when we gave  you a data module method in the Goodbye Globals tutorial that does the same thing in a more semantic way.

Now Storyboard provided the ability to pass parameters in a table between scenes and Composer carries that concept on as well.

storyboard.gotoScene( “mygame”,  { effect = “crossFade”, time = 250, params = { someVar = “SomeData”, someOtherVar = “someOtherData” } } )

Rob

Thanks for the answers. I did not know about the get and setVariable functions and that will work just fine. The table as a parameter is good also but I like the functions best.

I am switching from the storyboard to the composer and like it better. For some reason it took me a while to understand what happens in each event in these but your tutorials on here helped.

Thanks!

Warren

Hi Warren,

Do you mean, what is the equal method in Composer? If so, that would be the “setVariable()” function (and the corresponding “getVariable()” function):

http://docs.coronalabs.com/daily/api/library/composer/setVariable.html

http://docs.coronalabs.com/daily/api/library/composer/getVariable.html

Hope this helps,

Brent

The problem with doing:  storyboard.myVariable = 10 is that it’s not safe.  We could come along and decide we want to have a member named .myVariable and then your app would break.  You have to protect your member’s by name spacing the variable names.   We at one point suggested using a table named “state” (storyboard.state.myVariable) with the assumption we would never use state.  But again, this isn’t a best practice because we could again start using it.  Composer adds a setVariable and getVariable method to provide you a safe way of doing this.

Regardless, it’s probably not as clean to do this when we gave  you a data module method in the Goodbye Globals tutorial that does the same thing in a more semantic way.

Now Storyboard provided the ability to pass parameters in a table between scenes and Composer carries that concept on as well.

storyboard.gotoScene( “mygame”,  { effect = “crossFade”, time = 250, params = { someVar = “SomeData”, someOtherVar = “someOtherData” } } )

Rob

Thanks for the answers. I did not know about the get and setVariable functions and that will work just fine. The table as a parameter is good also but I like the functions best.

I am switching from the storyboard to the composer and like it better. For some reason it took me a while to understand what happens in each event in these but your tutorials on here helped.

Thanks!

Warren