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 https://forums.kleientertainment.com/forums/topic/48656-conventions-for-global-variables/ 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 https://forums.kleientertainment.com/forums/topic/48656-conventions-for-global-variables/#findComment-598422 Share on other sites More sharing options...
silktie Posted January 8, 2015 Author Share Posted January 8, 2015 (edited) 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? Edited January 8, 2015 by silktie Link to comment https://forums.kleientertainment.com/forums/topic/48656-conventions-for-global-variables/#findComment-599007 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 https://forums.kleientertainment.com/forums/topic/48656-conventions-for-global-variables/#findComment-599089 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now