Derthmonuter Posted May 19, 2015 Share Posted May 19, 2015 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 https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/ Share on other sites More sharing options...
DarkXero Posted May 19, 2015 Share Posted May 19, 2015 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 https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638706 Share on other sites More sharing options...
Kzisor Posted May 19, 2015 Share Posted May 19, 2015 @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 https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638713 Share on other sites More sharing options...
Derthmonuter Posted May 19, 2015 Author Share Posted May 19, 2015 @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 https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638723 Share on other sites More sharing options...
Derthmonuter Posted May 19, 2015 Author Share Posted May 19, 2015 Couldn't figure out how to edit, and I mentioned you both wrong. @DarkXero, @Kzisor Link to comment https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638725 Share on other sites More sharing options...
Kzisor Posted May 19, 2015 Share Posted May 19, 2015 @Derthmonuter, it goes in modmain.lua. Link to comment https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638729 Share on other sites More sharing options...
Derthmonuter Posted May 19, 2015 Author Share Posted May 19, 2015 @Derthmonuter, it goes in modmain.lua. This throws me a crash of, "Attempting to index global 'TheWorld' (a nil value)" on line "if not TheWorld.ismastersim then"... do I need to provide any special modifiers to TheWorld to point it to a properly-existing World? Link to comment https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638733 Share on other sites More sharing options...
Derthmonuter Posted May 19, 2015 Author Share Posted May 19, 2015 @Kzisor I appended "GLOBAL" to the front of that line in modmain.lua, and that allowed me to load it without crashing. However, the number of lives remaining is still not saving...hmm. Link to comment https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638742 Share on other sites More sharing options...
Kzisor Posted May 19, 2015 Share Posted May 19, 2015 @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 endendMake sure you are setting and getting TheWorld.state.darkness_level variable everywhere you would be getting the darkness level. Link to comment https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638746 Share on other sites More sharing options...
Derthmonuter Posted May 19, 2015 Author Share Posted May 19, 2015 @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 endendMake 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 https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638753 Share on other sites More sharing options...
Maris Posted May 19, 2015 Share Posted May 19, 2015 the simple method is attaching it to the worldstate component Great solution! Link to comment https://forums.kleientertainment.com/forums/topic/54164-dedicated-server-custom-gamemode-how-to-save-and-load-a-variable/#findComment-638802 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