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.

Feanara

Component OnLoad not firing

Recommended Posts

Feanara    1

I created a new component that gives absorbs 25% of damage until it expires. The OnLoad and OnSave should be pretty self explanatory. OnSave runs properly, but it seems like OnLoad is never firing (I don't see any of those strings in the log). I would love to understand better when/wear these functions get called and how I can debug them (specifically for components). 

function MageArmor:OnSave()
	print('Saving magearmor. current: '..self.current..' / is wearing: '..tostring(self.is_wearing))
    return 
    {
		current = self.current,
        is_wearing = self.is_wearing
	}
end

function MageArmor:OnLoad(data)
	print('Loading magearmor. Data: '..tostring(data))
    if data then
		print('Loading magearmor. current: '..data.current..' / is wearing: '..tostring(data.is_wearing))
        if data.current then
            self.current = data.current
        end

        if data.is_wearing then
            self.is_wearing = data.is_wearing
        end
		
		if data.wearer then
			self.wearer = data.wearer
		end
    end

    if self.is_wearing then
		self:PutOn(self.inst, GetPlayer())
    end
end

 

In case it helps, I'm including the code of the scroll prefab. When the scroll is read, the component is added to the reader.

	inst:AddComponent("spellscroll")
	inst.components.spellscroll.onread = armorfn


function armorfn(inst, reader)
	if not reader.components.magearmor then
		reader:AddComponent("magearmor")
	end
	reader.components.magearmor:PutOn(inst, reader)
    return true    
end

 

Share this post


Link to post
Share on other sites
krylincy    53

have you add those functions to your prefab?

in the main function of your prefab you need to add

inst.OnSave = onsave --set the save
inst.OnLoad = onload --and the load functions

And the functions like

local function onsave(inst, data)
	data.eatTimes = inst.eatTimes
end

local function onload(inst, data)
    if data and data.eatTimes then
		inst.eatTimes = data.eatTimes
	end     
end

so in this example i save the eatTimes of each inst.

 

why are you return an object from your save function?

Share this post


Link to post
Share on other sites
Feanara    1

This is a component, not a prefab, and as far as I can tell components typically return an object in OnSave. Both of these functions are in the component itself. 

 

This component gets added to the character when they do a certain action, and gets removed from the character when it expires. And I don't know how saving works, so is it possible that the component doesn't get add if its not explicit in the prefab initially? Are there other examples of components that get added and removed from an object?

 

I might need to make the component a permanent addition to the character, and only take effect when a certain event fires. 

Share this post


Link to post
Share on other sites