Jump to content

Conventions for Global Variables?


silktie

Recommended Posts

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 practice

function autosave:DoSave()    if not autosavecount then        autosavecount=0    end    autosavecount = autosavecount + 1    DoSave_base(self)end

Just 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

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

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 :grin:

 

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.autoSaveCountend

Just out of curiosity, is the function defined within Class() essentially a constructor?

Link to comment
Share on other sites

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.

×
  • Create New...