Jump to content

Dedicated server custom gamemode--how to save and load a variable?


Recommended Posts

Hello everyone,

 

I'm working on a custom gamemode meant to compromise survival and endless modes a bit, so that it's actually possible to have something resembling a survival challenge running on a dedicated server. I started by modifying "The Hunt" custom gamemode example. Basically, every time a player dies a number is decremented by one. Every time a giant is slain I add ten of these "lives." When this number hits zero, game over for everyone and the map is regenerated.

 

That's all working great, but when I reset the server for maintenance this number is always reverted back to its default of 10. I can't seem to figure out how to save it and load it back in case I need to save/load the server momentarily.

 

Does anyone have example code or know how I could accomplish this? Thanks!

Link to comment
Share on other sites

You can create and spawn a non-networked entity, that stores data.

 

Like this:

local function OnSave(inst, data)	data.livesleft = inst.livesleftendlocal function OnLoad(inst, data)	inst.livesleft = data.livesleft or 10endlocal function fn()	local inst = CreateEntity()	inst.entity:AddTransform()		local data = {}	OnLoad(inst, data)	inst.OnSave = OnSave	inst.OnLoad = OnLoad    return instendreturn Prefab("common/objects/worldstats", fn)
Link to comment
Share on other sites

@DarkXero, @Derthmonuter,  the simple method is attaching it to the worldstate component.

AddComponentPostInit( "worldstate", function(world_state)    if not TheWorld.ismastersim then        return world_state    end    -- Hence forth this will be your variable which you will need to change via TheWorld.state.darkness_level    world_state.data.darkness_level = 10    return world_stateend ) 

Simply change TheWorld.state.darkness_level from now own in your code and it will automatically save it. I am currently creating a mod which required changing a few things on the worldstate component without completely overwriting it unlike the Dawn mod which I was unable to do that with. I came up with this solution and it works surprisingly easy. Also with this particular method, it allows your mod to be disabled without breaking anything in the game because darkness_level will still persist, meaning that hosts can disable and enabled it at will without altering it's effective level. If you used a custom component then after disabling it and saving the game, the component would be removed from the save file meaning the next time the mod is enabled it would completely start from the basic default level.

 

Link to comment
Share on other sites

@DarkXero, @Kzisor:

 

Thanks for the quick responses! I spent some time fiddling around with DarkXero's solution first, but I'm really not too sure where in my code those methods are really supposed to fit in. So I tried Kzisor's solution, but I'm running into an error which crashes Don't Starve on the line "AddComponentPostInit( "worldstate", function(world_state) ... " which I believe to be caused by a similar misunderstanding of where exactly this would fit into the mod framework. Does it need to be in modmain.lua? I'll try that now.

Link to comment
Share on other sites

@Derthmonuter, it should automatically save because all information in wordstate.data is getting saved. 

 

Directly from worldstate component file:

function self:OnSave()    local data = {}    for k, v in pairs(self.data) do        data[k] = v    end    return dataendfunction self:OnLoad(data)    for k, v in pairs(data) do        if self.data[k] ~= nil then            self.data[k] = v            print("setting ", k, v)        end    endend

Make sure you are setting and getting TheWorld.state.darkness_level variable everywhere you would be getting the darkness level.

Link to comment
Share on other sites

@Derthmonuter, it should automatically save because all information in wordstate.data is getting saved. 

 

Directly from worldstate component file:

function self:OnSave()    local data = {}    for k, v in pairs(self.data) do        data[k] = v    end    return dataendfunction self:OnLoad(data)    for k, v in pairs(data) do        if self.data[k] ~= nil then            self.data[k] = v            print("setting ", k, v)        end    endend

Make sure you are setting and getting TheWorld.state.darkness_level variable everywhere you would be getting the darkness level.

 

I ended up slogging through my file and found a typo that was setting the wrong variable. Everything seems to be working now! Thanks for all your help! :)

Link to comment
Share on other sites

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
 Share

×
  • Create New...