silktie Posted January 7, 2015 Share Posted January 7, 2015 Hey guys, Just wondering - Is there a convention of where to store global variables at all? (i.e. GLOBAL.varName?) My current code in modmain.lua works fine with the following, but I don't feel loose global variables like this is very good practicefunction autosave:DoSave() if not autosavecount then autosavecount=0 end autosavecount = autosavecount + 1 DoSave_base(self)endJust to note, this is a counter associated with the game itself and not a particular entity (maybe the player - although their instance isn't passed into the method to be able to hook the value onto) Thoughts? Link to comment Share on other sites More sharing options...
seronis Posted January 7, 2015 Share Posted January 7, 2015 Best thing you could do in my opinion would be to have a global component that manages your stuff and just save in the component. Like how the seasonmanager or krampus or hounded works. Components save with the world automatically after all. You just need your OnSave and OnLoad methods defined Link to comment Share on other sites More sharing options...
silktie Posted January 8, 2015 Author Share Posted January 8, 2015 Ah brilliant! Essentially a custom object I can stuff all my global variables in. Thank you for flagging the krampus and seasonmanager as examples - That was perfect, thank you Managed to get it working with the following: local checkPoint = Class(function(self, inst) self.inst = inst self.autoSaveCount = 0end)function checkPoint:OnSave() return { autoSaveCount = self.autoSaveCount }endfunction checkPoint:OnLoad(data) self.autoSaveCount = data.autoSaveCount or self.autoSaveCountendJust out of curiosity, is the function defined within Class() essentially a constructor? Link to comment Share on other sites More sharing options...
seronis Posted January 9, 2015 Share Posted January 9, 2015 Yes its the equivalent of a constructor. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.
Please be aware that the content of this thread may be outdated and no longer applicable.