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.

DarkREM66

How to override a prefab function ?

Recommended Posts

DarkREM66    0

Hello,

I want to make a mod for the tooth trap. I want to override the OnExplode function to add a auto-reset tweak.

This is my code in the modmain.lua:
 

local function newOnExplode(inst, target)
    local oldOnExplode = inst.OnExplode
    oldOnExplode(inst, target)

	inst:DoTaskInTime(3, function() 
		if inst.components.mine then
			inst.components.mine.onreset(inst) 
			inst.components.mine:StopTesting()
			inst.components.mine.target = nil
			inst.components.mine.issprung = false
			inst.components.mine.inactive = false
			inst.components.mine:StartTesting()
		end
	end)
end

local function autoToothTrap(inst)
    inst.components.mine:SetOnExplodeFn(newOnExplode)
end

AddPrefabPostInit("trap_teeth", autoToothTrap)

But, the old OnExplode function is never triggered and it is what i'm searching ? Is it possible to override this function to add an action after the trap was trigerred ?

Share this post


Link to post
Share on other sites
DarkREM66    0

I find my mistake. I have to use the onexplode variable of the mine component to retrieve the old OnExplode function.

This code works fine:
 

local oldOnExplode

local function newOnExplode(inst, target)
    oldOnExplode(inst, target)
    inst:DoTaskInTime(3, function()
        if inst.components.mine then
            inst.components.mine.onreset(inst)
            inst.components.mine:StopTesting()
            inst.components.mine.target = nil
            inst.components.mine.issprung = false
            inst.components.mine.inactive = false
            inst.components.mine:StartTesting()
        end
    end)
end

local function autoToothTrap(inst)
    oldOnExplode = inst.components.mine.onexplode
    inst.components.mine:SetOnExplodeFn(newOnExplode)
end

AddPrefabPostInit("trap_teeth", autoToothTrap)

A great thank you to DrBLOOD for the Auto Tooth Trap mod which implement this tweak, but it's load a modified prefab file.

Share this post


Link to post
Share on other sites