Jump to content

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?

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

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?

Edited by silktie

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...