So, stage is a global. In that case, if I'm running singleton classes for things such as audio control, config options etc. is adding them to stage the best way to go, as presumably I can then reference them with stage.myclass. Or is there a better way?
I'm still getting to grips with the performance and memory differences between locals and globals in Lua and working out the best way to access commonly used elements (not necessarily visual elements) - there seems to be a huge number of different ways to do the same thing in Lua, and I'm trying to get it clear in my head the relatives strengths and weaknesses of each one.
Comments
IMHO, If you're accessing a global variable many many times per frame, you should be careful about it. Otherwise you don't need to worry.
To store the options of a game, I usually create an options.lua file with this content:
(This code uses http://github.com/gideros/table.save to save/load a table and options.lua should be dependent to table.save-0.94.lua)
Hope this helps
Likes: chipster123
I have used this option in many of my games, not only the option data but also the level data.But recently I found it has a little problem when you add an option in update version.It may still load the old data,so I consider
to add an version number to the table and check the version after load from table.
https://sites.google.com/site/xraystudiogame
And on @andybarilla 's point, I wouldn't mind knowing if there's a difference in performance between having a global individually defined and having a local as a child of stage. Does one give better performance or memory usage than the other?
@moopf, @andybarilla I think there is no difference in terms of memory but accessing an individual global variable is a little bit faster.
As you can see from the example below I don't need any event handling code, I just call the :update method once each "frame" and then poll :complete to see if the effect is complete, so I'm going to try removing EventDispatcher as the base class and see if it makes any diffference.
Sorry to go too far off topic here but...
I'm also trying to work out how best to provide a core "utility" class that will hold small functions etc that I want to reuse from app to app and object to object and I'm wondering if this pattern might suffice.
Interestingly when using Corona I looked quite heavily into using tables for objects and also the use of the require statement - I noticed that no real Gideros code seems to be using the require statement, even for the process of making a local reference to a global table (I'm assuming here that both my Tween class and the Atilim's original Options class are both global tables).
My Lua knowledge is far less advanced than my C/C++ knowledge and so any advice with regards to creating a "table" of utility functions and any potential use of the require statement to localise references would be much appreciated.
Lastly (with regards to the code above) I should probably mention that in general when developing I try to have as few "onEnterFrame" listeners as possible, preferring to have one that is called which in turn calls an objects :update() method, I find the code structure to be a lot easier and less prone to strange leaks and errors when listeners aren't removed correctly.
Jon...
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Currently we don't give support for require function, because every .lua file in the project is executed automatically. Supporting the require function and giving option to enable/disable the automatic execution of .lua files are in progress.
Maybe you can populate your lua functions in a global "utility" table and then if you want to fast access you can assign them to a local variable (as in Lua's "math" and "io" tables).
In fact looking at the project and being able to set dependencies from one file to another (which I note affects the call order) seems a much better solution.
Personally I *really* like the idea of forcing init.lua to be executed first and main.lua last - that way I can discipline myself to put any and all globals in init.lua and know that everything will be ready when needed, your OOP solution for Lua is by far the best I've seen.
Kudos!
Jon...
Likes: atilim
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill