Nycidian Posted November 7, 2013 Share Posted November 7, 2013 From when I made other mods I was under the impression that to save and reload variables in components all one had to do was something like...function Amanager:OnSave() return { killed = self.killed, }endfunction Amanager:OnLoad(data) if data.killed ~= nil then self.killed = data.killed endend...but for some reason neither of these functions are firing. My guess is I'm missing something obvious but for the life of me I can't find it. Here is the mod... achieve_0.1_working.zip ...if anyone could point out what I am doing wrong I would appreciate it. Link to comment Share on other sites More sharing options...
squeek Posted November 8, 2013 Share Posted November 8, 2013 If you're adding the component to a character prefab using SimPostInit, saving won't work. Not totally sure the cause, but simplex posted a workaround hereSince his code got smashed into one line, here it is in the proper format:-- need to add the component in here, otherwise OnSave doesn't work rightAddPrefabPostInit("world", function(inst) GLOBAL.assert( GLOBAL.GetPlayer() == nil ) local player_prefab = GLOBAL.SaveGameIndex:GetSlotCharacter() -- Unfortunately, we can't add new postinits by now. So we have to do -- it the hard way... GLOBAL.TheSim:LoadPrefabs( {player_prefab} ) local oldfn = GLOBAL.Prefabs[player_prefab].fn GLOBAL.Prefabs[player_prefab].fn = function() local inst = oldfn() -- Add components here. inst:AddComponent("yourcomponent") return inst endend) Link to comment Share on other sites More sharing options...
Nycidian Posted November 8, 2013 Author Share Posted November 8, 2013 If you're adding the component to a character prefab using SimPostInit, saving won't work. Not totally sure the cause, but simplex posted a workaround hereSince his code got smashed into one line, here it is in the proper format:-- need to add the component in here, otherwise OnSave doesn't work rightAddPrefabPostInit("world", function(inst) GLOBAL.assert( GLOBAL.GetPlayer() == nil ) local player_prefab = GLOBAL.SaveGameIndex:GetSlotCharacter() -- Unfortunately, we can't add new postinits by now. So we have to do -- it the hard way... GLOBAL.TheSim:LoadPrefabs( {player_prefab} ) local oldfn = GLOBAL.Prefabs[player_prefab].fn GLOBAL.Prefabs[player_prefab].fn = function() local inst = oldfn() -- Add components here. inst:AddComponent("yourcomponent") return inst endend)Thank you, that's good to know I thought I was going crazy thanks @simplex as well. Link to comment Share on other sites More sharing options...
Malacath Posted November 8, 2013 Share Posted November 8, 2013 Just curious if I'm mistaken here, but from my experience they work when adding them via PrefabPostInit. I know this is problematic with mod characters but as long as you are interested in adding a component to your own mod character that should be easier than what you're doing now. Link to comment Share on other sites More sharing options...
simplex Posted November 8, 2013 Share Posted November 8, 2013 Just curious if I'm mistaken here, but from my experience they work when adding them via PrefabPostInit. I know this is problematic with mod characters but as long as you are interested in adding a component to your own mod character that should be easier than what you're doing now. Using AddPrefabPostInit works, what doesn't is using AddSimPostInit. So the above workaround is for when you want to add a component to all player characters, not just a predetermined subset. Link to comment Share on other sites More sharing options...
Nycidian Posted November 8, 2013 Author Share Posted November 8, 2013 Just curious if I'm mistaken here, but from my experience they work when adding them via PrefabPostInit. I know this is problematic with mod characters but as long as you are interested in adding a component to your own mod character that should be easier than what you're doing now.What I'm doing is making a modular mod that all you have to do is add a few lines of code to the modmain and it runs a few different lua files that add in more than just a lone component could do (at least as far as I can tell there are things in my achieve.lua that I can't do in a component file). The other thing I believe it will do is if multiple mods use these files it will only install it once, though I need to test this feature to make sure. Link to comment Share on other sites More sharing options...
Malacath Posted November 8, 2013 Share Posted November 8, 2013 Well, and suddenly I'm in the situation that I need that code as well... So thanks for this timely placed thread! Link to comment Share on other sites More sharing options...
Heavenfall Posted November 9, 2013 Share Posted November 9, 2013 W.T.F is simpostinit anyway. Amirite? Link to comment Share on other sites More sharing options...
Recommended Posts
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.