Jump to content

Recommended Posts

Atm i just get part of code from betterconsole mod :

local function AddComponent()    local character = GLOBAL.SaveGameIndex:GetSlotCharacter()    GLOBAL.TheSim:LoadPrefabs {character}    local oldfn = GLOBAL.Prefabs[character].fn    GLOBAL.Prefabs[character].fn =        function(...)            local inst = oldfn(...)            inst:AddComponent("customcomponent")            return inst        endendAddPrefabPostInit("world", AddComponent)

its works fine, till i tried to enter caves.  which makes game crash and erasing saves.

 

second method :

local function AddComponent(p)        p:AddComponent("customcomponent")end AddSimPostInit(AddComponent)

works fine, even in caves, but have 1 major issue - component doesn't call OnLoad


 

Edited by Rincevvind

Atm i just get part of code from betterconsole mod :

local function AddComponent()    local character = GLOBAL.SaveGameIndex:GetSlotCharacter()    GLOBAL.TheSim:LoadPrefabs {character}    local oldfn = GLOBAL.Prefabs[character].fn    GLOBAL.Prefabs[character].fn =        function(...)            local inst = oldfn(...)            inst:AddComponent("customcomponent")            return inst        endendAddPrefabPostInit("world", AddComponent)

its works fine, till i tried to enter caves.  which makes game crash and erasing saves.

 

second method :

local function AddComponent(p)        p:AddComponent("customcomponent")end AddSimPostInit(AddComponent)

works fine, even in caves, but have 1 major issue - component doesn't call OnLoad

 

 

I recently found out magic, to load stuff, look on hunter component, definition of it:

 self.inst:DoTaskInTime(0, function(inst) inst.components.hunter:StartCooldown() end)

That makes sure all data is loaded, I also use that in OnLoad function, it works good for prefabs like pigs and merms, so it should work with character also.

 

looks like not, lol. just commented this save part and return {}. all works fine.

thx _Q_ for pointing on real problem... looks like my laziness is issue :/


Don't think so, maybe try to declare the inner table first.

Like in invenotry component for example.

well code works fine till caves, i checked it many times with loading/unloading. whole array is fine, its actually array of arrays and it was loaded/saved properly in this way

Edited by Rincevvind

looks like not, lol. just commented this save part and return {}. all works fine.

thx _Q_ for pointing on real problem... looks like my laziness is issue :/

well code works fine till caves, i checked it many times with loading/unloading. whole array is fine, its actually array of arrays and it was loaded/saved properly in this way

 

When I was making telestone mod saves were erased cause saveindex could not load when entering/exiting caves, cause of the way I was saving tables, can't say more, so you could just post whole code or mod here for others to take a look at it and see what is the cause of the problem.

Why even use deepcopy at all in OnSave? OnSave won't alter the table, so just doing

return {d = self.data}
or even

return self.data
should be fine.

Same with loading, no reason to deepcopy the table in OnLoad.

Edited by squeek

oh damn yes, so much fail lol, i just made function which coping array without variables which have "function" type and all works fine

local function RemoveFN(t)    local t2 = {}    for k,v in pairs(t) do        if type(v) == "table" then t2[k] = RemoveFN(v) else        if type(v)~="function" then t2[k] = v end    end    end    return t2end

its wasnt issue outside caves since functions was always redefined in array while init

oh damn yes, so much fail lol, i just made function which coping array without variables which have "function" type and all works fine

Functions can be saved (I did it in the trade sequence code I wrote for the Link mod).

But still a question, why so hack-like method is required for such simple action like adding component to player :-)

Thankfully, it won't be for too much longer.

Functions can be saved (I did it in the trade sequence code I wrote for the Link mod).

Yes, but this can cause issues if you're not careful, since every external local variable used by the function is also saved. A simple case where this is disastrous is if you store the global environment in a local variable and use it in the function saved.

Also, the saved function gets loaded in the global environment, so if it's a function defined in the mod environment (as seems to be the case here) it won't work after loading due to the environment change.

Edited by simplex

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