Jump to content

Need help saving the value of a component!


Recommended Posts

Hello. After a short break, I continue working on the mod. According to the idea, during an action, a number, possibly one, is entered into the component, and when one is present, the character is given a tag. It seems that I dug up the code from the “hunger” component, but I don’t know what doesn’t work in it. I think there are several reasons for this
1.There is no way to write down the number
2.To save
3.In component inspection

Component :

local metalscrapknowledge = Class(function(self, inst)
    self.inst = inst
    self.max = 1
    self.current = self.max,
end)

function metalscrapknowledge:SetMax(amount)
    self.max = amount
    self.current = amount
end

function metalscrapknowledge:DoDelta(delta)
    if self.redirect ~= nil then
        self.redirect(self.inst, delta)
        return
    end
end

function metalscrapknowledge:OnLoad(data)
    if data.metalscrapknowledge ~= nil and self.current ~= data.metalscrapknowledge then
        self.current = data.metalscrapknowledge
        self:DoDelta(0)
    end
end

function metalscrapknowledge:OnSave()
    return self.current ~= self.max and { metalscrapknowledge = self.current } or nil
end

return metalscrapknowledge

Character :

inst:AddComponent("metalscrapknowledge")
print(inst.components.metalscrapknowledge)
if inst.components.metalscrapknowledge == 1 then
inst:AddTag("evgens_craft_plus")
        end
    end)

If you know how to do this please write

Link to comment
Share on other sites

It works! I think there was a problem that the data names were different

local metalscrapknowledge = Class(function(self, inst)
    self.inst = inst
    self.current = 0
end)

function metalscrapknowledge:Set(amount)
    self.current = amount
end

function metalscrapknowledge:Get()
    return self.current
end

function metalscrapknowledge:OnSave()
    return self.current and { metalscrapknowledge = self.current } or nil
end

function metalscrapknowledge:OnLoad(data)
    if data.metalscrapknowledge ~= nil and self.current ~= data.metalscrapknowledge then
        self.current = data.metalscrapknowledge
    end
end

return metalscrapknowledge
 

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