Jump to content

Trying to modify firestaff function with addprefabpostinit


Recommended Posts

It's been a while since I made a mod, almost a year. Currently I'm trying to do a very simple change to the firestaff that makes it do 34 damage instead of set things on fire. A mod I made a while back used AddComponentPostInit to successfully overwrite the ReleaseHound function in hounded.lua, so I assumed AddPrefabPostInit worked the same way and tried to duplicate... to no avail.

 

I noticed that all the different staves seem to be stored in staff.lua, at least that's what it looks like to me. So I wrote a modmain.lua that looks like this...

PrefabFiles = {}Assets = {}local function setDamage(prefab)	prefab.onattack_red = function(inst, attacker, target, skipsanity)			if target.components.burnable and not target.components.burnable:IsBurning() then			if target.components.freezable and target.components.freezable:IsFrozen() then                                   target.components.freezable:Unfreeze()            			end   		end		if target.components.freezable then			target.components.freezable:AddColdness(-1) --Does this break ice staff?			if target.components.freezable:IsFrozen() then                        target.components.freezable:Unfreeze()            			end		end		if target.components.sleeper and target.components.sleeper:IsAsleep() then			target.components.sleeper:WakeUp()		end		if target.components.combat then			target.components.combat:SuggestTarget(attacker)			if target.sg and target.sg.sg.states.hit and not target:HasTag("player") then                        target.sg:GoToState("hit")			end		end		if attacker and attacker.components.sanity and not skipsanity then			attacker.components.sanity:DoDelta(-TUNING.SANITY_SUPERTINY)		end		attacker.SoundEmitter:PlaySound("dontstarve/wilson/fireball_explo")		target:PushEvent("attacked", {attacker = attacker, damage = 34})	endendAddPrefabPostInit("staff", setDamage)

The only change I made to the original function was remove the call to ignite the target, and change the damage at the end from 0 to 34.

 

The mod is completely nonfunctional. When I look at it in the mod list it always says the mod crashed.

 

I clearly have no idea how to use AddPrefabPostInit, so can someone tell me?

Edited by code4240
Link to comment
Share on other sites

@code4240, Although all staffs are in the staff.lua prefab file, that doesn't mean they're the same prefab. AddPrefabPostInit operates on an game prefab object, not the files. There are a few files that return multiple objects, (most notorious being hats.lua), but you must AddPrefabPostInit on each returned prefab separately.

Link to comment
Share on other sites

@code4240, Although all staffs are in the staff.lua prefab file, that doesn't mean they're the same prefab. AddPrefabPostInit operates on an game prefab object, not the files. There are a few files that return multiple objects, (most notorious being hats.lua), but you must AddPrefabPostInit on each returned prefab separately.

 

Hmm. Well, I've already tried using "firestaff" instead, and I also just tried calling it for all of them like this-

AddPrefabPostInit("firestaff", setDamage)AddPrefabPostInit("icestaff", setDamage)AddPrefabPostInit("telestaff", setDamage)AddPrefabPostInit("orangestaff", setDamage)AddPrefabPostInit("greenstaff", setDamage)AddPrefabPostInit("yellowstaff", setDamage)

-but I've got no results. Is there some other way I should be handling this?

Link to comment
Share on other sites

@code4240, You're providing a function and storing it in inst.onattack_red. This field is never referenced or called anywhere else. onattack_red is a local function in the staff.lua file, but it gets attached to the weapon component on line 513 of staff.lua:

    inst.components.weapon:SetOnAttack(onattack_red)

This is presumably storing it in a field that the weapon component then calls.

 

To give a small example of what you're running into:

local number = 5local entity = {}entity.num = numbernumber = 6print(entity.num) -- will print "5"

Although in your case there's a few more things thwarting you, because onattack_red is never attached to the entity table, it's just local to the file. Things that are local to a file cannot really be referenced from outside of that file (technically there are ways you might do it, but it's... not a good/reliable thing to do).

Link to comment
Share on other sites

<snip>

 

EUREKA!

 

After reading the contents of red(), which contained line 513, I removed the "prefab.onattack_red = function()" line from setDamage and simply did this:

AddPrefabPostInit("firestaff", function(inst)			inst.components.weapon:SetDamage(34)			inst.components.weapon:SetOnAttack(setDamage)end)

And it worked! Thanks a ton pal!

 

EDIT: I cannot into forum formatting

Edited by code4240
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...