Isaac wrote:minmay wrote:You can also freely set fields in the global tables like Config and GameMode, so
Code: Select all
Config.foo = function() print("it works!") end
works, and then you can call Config.foo(). But the global tables are not serialized, so make sure that anything you add to them is added by your init files and not from a script entity. Of course, using the global tables for this purpose is even worse practice than defining global functions.
That's pretty cool.
So this seems a place to put a few choice utility functions; (like a pronoun utility for use in all hudPrinted dialog). Is there a reason not to fill that table with a large number of (often used) functions?
The potential problems with it are
1. it's bad programming practice
2. not serialized in savegames, so you must be careful to define them again when the game is reloaded
3. the tables are persistent until the game is exited. You can save and load, you can even go back to the main menu and into another dungeon, and the table will keep all your extra stuff in it. So if you change one of the default keys in a table (like changing GameMode.advanceTime() to do something specific to your dungeon) you could break other dungeons if the player switches away from your dungeon without exiting the game.
Isaac wrote:**Funny issue though... It would seem that functions in the Config table are [above?] some of the 'global' functions; like hudPrint() [which is nil when called].
It's not clear what you are saying here. Do you mean that Config is present in the init file namespace and hudPrint() isn't? That is correct, but you shouldn't think of it as "above".
Isaac wrote:***This might prove useful for defining items using dynamic property values. IE. using a custom function [variable] to return the value for an item property during the definition of it; where [afaik] one usually cannot. But by using Config this way, it can work in a definition:
uiName = Config.uiName(), ~uiName gets assigned the return value; instead of the function id (were you to try to use one).
You don't need to use this hack for that. Init files have a shared namespace that you can freely modify. There is no advantage to what you describe over doing this in an init file:
Code: Select all
function getUiName()
-- whatever
end