Jump to content

Component OnLoad not firing

Recommended Posts

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))
		current = self.current,
        is_wearing = self.is_wearing

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

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

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


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.components.spellscroll.onread = armorfn

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


Link to comment
Share on other sites

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

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

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


why are you return an object from your save function?

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites


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.

  • Create New...