Jump to content

How to properly save this overriden component ?


Recommended Posts

Hi, I would want to add a member variable and get it properly saved.

AddComponentPostInit("saltlicker", function(comp) comp.lickcount = 0 end)

How to do it ? Which function do i need to overide ?

Here are the function used while saving/loading

function SaltLicker:OnSave()
    --V2C: can't trigger LoadPostPass unless there is any save data
    return self.salted and { salted = true } or nil
end

function SaltLicker:LoadPostPass()
    -- the timer's save/load has all the data we need...
    if self.inst.components.timer:TimerExists("salt") then
        _StopSeeking(self)
        self:SetSalted(true)
    end
end

Thanks

 

Link to comment
Share on other sites

56 minutes ago, waw said:

Hi, I would want to add a member variable and get it properly saved.


AddComponentPostInit("saltlicker", function(comp) comp.lickcount = 0 end)

How to do it ? Which function do i need to overide ?

Here are the function used while saving/loading


function SaltLicker:OnSave()
    --V2C: can't trigger LoadPostPass unless there is any save data
    return self.salted and { salted = true } or nil
end

function SaltLicker:LoadPostPass()
    -- the timer's save/load has all the data we need...
    if self.inst.components.timer:TimerExists("salt") then
        _StopSeeking(self)
        self:SetSalted(true)
    end
end

Thanks

 

so you want to add a variable "lickcount" to the component saltlicker, and save load it correctly, right?

Link to comment
Share on other sites

hm.... this saltlicker component looks strange... normally you have a OnSave and OnLoad function, dealing with "data".

So if it would be normal, you would do it this way eg for prefab teleportato
 

AddPrefabPostInit("teleportato", function(inst)  
    local _OnSave = inst.OnSave
    local function OnSave(inst,data)
        _OnSave(inst,data) -- call the previous
        data.activatedonce = inst.activatedonce
    end
    inst.OnSave = OnSave
    
    local _OnLoad = inst.OnLoad
    local function OnLoad(inst,data)
        _OnLoad(inst,data) -- call the previous
        if data then
            if data.activatedonce then
                inst.activatedonce = data.activatedonce
            end
        end
    end
    inst.OnLoad = OnLoad
end) 

I have no clue, why it is "LoadPostPass" here...

So with saltlicker it seems the OnSave only returns something, to trigger a load function. It seems it is not important what it returns...
I would try it that way now:
 

AddComponentPostInit("saltlicker", function(comp)  
    local _OnSave = comp.OnSave
    local function OnSave()
        local data =
        {
            lickcount = comp.lickcount,
        }
        olddata = _OnSave() -- just in case the old data is needed somewhere
        newdata = GLOBAL.ArrayUnion(data,olddata) -- merge the old and the new data
        return newdata
    end
    comp.OnSave = OnSave
    
    local function OnLoad(data)
        if data then
            if data.lickcount then
                comp.lickcount = data.lickcount
            end
        end
    end
    comp.OnLoad = OnLoad
end) 

The old OnSave function seems not to be important

Edited by Serpens
Link to comment
Share on other sites

1 hour ago, waw said:

Thanks, but i get a syntax error


12   local data =
13   {
14      data.lickcount = comp.lickcount,
15   }

14: '}' expected (to close '{' at line 13) near '='

 

ah yes, change "data.lickcount" to "lickcount" ;)

I guess there could be even more errors, since I did not tested it...
But it should be the right direction.

Link to comment
Share on other sites

I managed to correct them

Thanks a lot, i had similar code but it was crashing when saving

 

EDIT : Everything is working with this final code.

 

AddComponentPostInit("saltlicker", function(comp)
   comp.lickcount = 0

   local old_OnSave = comp.OnSave
   function comp:OnSave()
      local data = {}
      data.lickcount = comp.lickcount
      data.olddata = old_OnSave(comp)
      return data
   end
    
   function comp:OnLoad(data)
      if data and data.lickcount ~= nil then
         comp.lickcount = data.lickcount
      else
         comp.lickcount = 0
      end
   end
end)

 

Edited by waw
Link to comment
Share on other sites

On 9.9.2016 at 3:18 AM, waw said:

I managed to correct them

Thanks a lot, i had similar code but it was crashing when saving

 

EDIT : Everything is working with this final code.

 


AddComponentPostInit("saltlicker", function(comp)
   comp.lickcount = 0

   local old_OnSave = comp.OnSave
   function comp:OnSave()
      local data = {}
      data.lickcount = comp.lickcount
      data.olddata = old_OnSave(comp)
      return data
   end
    
   function comp:OnLoad(data)
      if data and data.lickcount ~= nil then
         comp.lickcount = data.lickcount
      else
         comp.lickcount = 0
      end
   end
end)

 

ah yes, you fixed some problems.
But in your code olddata is not used, since it is not merged. The merge function I used was wrong, it has to be MergeMaps

So this should be the final code:
 

AddComponentPostInit("saltlicker", function(comp)
   comp.lickcount = 0

   local old_OnSave = comp.OnSave
   function comp:OnSave()
      local data = {}
      data.lickcount = comp.lickcount
      local olddata = old_OnSave(comp)
      local newdata = GLOBAL.MergeMaps(data,olddata)
      return newdata
   end
    
   function comp:OnLoad(data)
      if data and data.lickcount ~= nil then
         comp.lickcount = data.lickcount
      else
         comp.lickcount = 0
      end
   end
end)

 

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