Jump to content

Testing hats


Recommended Posts

I'm making my first character mod and i've stumbled across an issue I can't solve by either looking through the game code or here on the forums.

Basically I want to create a function that says this:

"If I equip a certain hat, remove the monster tag from my character. If I unequip said hat, add the monster tag back to my character, and remove all followers with the tag pig. Else, keep the monster tag."

I've been fiddling around with some code I've found here on the forums but I don't think it's worth showing since it doesn't make much sense from what I have done to it. From what I understand I need to make a prefab that chooses which hats will remove the monster tag, and then test when one of those hats is equipped. My main problem is that I don't know what code I can use to actually test what is in the (hat?) slot, and I don't know if this code should be going in my modmain or character lua. I also don't know how to go about using this function since it is a lot different from my other perks.

Link to comment
Share on other sites

I'm making my first character mod and i've stumbled across an issue I can't solve by either looking through the game code or here on the forums.

Basically I want to create a function that says this:

"If I equip a certain hat, remove the monster tag from my character. If I unequip said hat, add the monster tag back to my character, and remove all followers with the tag pig. Else, keep the monster tag."

I've been fiddling around with some code I've found here on the forums but I don't think it's worth showing since it doesn't make much sense from what I have done to it. From what I understand I need to make a prefab that chooses which hats will remove the monster tag, and then test when one of those hats is equipped. My main problem is that I don't know what code I can use to actually test what is in the (hat?) slot, and I don't know if this code should be going in my modmain or character lua. I also don't know how to go about using this function since it is a lot different from my other perks.

 

Here is a sample code:

 

local function OnEquip(inst, owner)     owner.AnimState:OverrideSymbol("swap_hat", "hat_bee_bw_swap", "swap_hat")    owner.AnimState:Show("HAT")    owner.AnimState:Show("HAT_HAIR")    owner.AnimState:Hide("HAIR_NOHAT")    owner.AnimState:Hide("HAIR")    if owner:HasTag("player") then        owner.AnimState:Hide("HEAD")        owner.AnimState:Show("HEAD_HAIR")    end    if owner:HasTag("monster") then        owner:RemoveTag("monster")    endendlocal function OnUnequip(inst, owner)     owner.AnimState:Hide("HAT")    owner.AnimState:Hide("HAT_HAIR")    owner.AnimState:Show("HAIR_NOHAT")    owner.AnimState:Show("HAIR")    if owner:HasTag("player") then        owner.AnimState:Show("HEAD")        owner.AnimState:Hide("HEAD_HAIR")    end    owner:AddTag("monster")    if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend 

Link to comment
Share on other sites

Here is a sample code:

local function OnEquip(inst, owner)     owner.AnimState:OverrideSymbol("swap_hat", "hat_bee_bw_swap", "swap_hat")    owner.AnimState:Show("HAT")    owner.AnimState:Show("HAT_HAIR")    owner.AnimState:Hide("HAIR_NOHAT")    owner.AnimState:Hide("HAIR")    if owner:HasTag("player") then        owner.AnimState:Hide("HEAD")        owner.AnimState:Show("HEAD_HAIR")    end    if owner:HasTag("monster") then        owner:RemoveTag("monster")    endendlocal function OnUnequip(inst, owner)     owner.AnimState:Hide("HAT")    owner.AnimState:Hide("HAT_HAIR")    owner.AnimState:Show("HAIR_NOHAT")    owner.AnimState:Show("HAIR")    if owner:HasTag("player") then        owner.AnimState:Show("HEAD")        owner.AnimState:Hide("HEAD_HAIR")    end    owner:AddTag("monster")    if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend
Wow thanks for this. I'll get back to you tommorrow on whether or not I've figured it out.
Link to comment
Share on other sites

Here is a sample code:

local function OnEquip(inst, owner)     owner.AnimState:OverrideSymbol("swap_hat", "hat_bee_bw_swap", "swap_hat")    owner.AnimState:Show("HAT")    owner.AnimState:Show("HAT_HAIR")    owner.AnimState:Hide("HAIR_NOHAT")    owner.AnimState:Hide("HAIR")    if owner:HasTag("player") then        owner.AnimState:Hide("HEAD")        owner.AnimState:Show("HEAD_HAIR")    end    if owner:HasTag("monster") then        owner:RemoveTag("monster")    endendlocal function OnUnequip(inst, owner)     owner.AnimState:Hide("HAT")    owner.AnimState:Hide("HAT_HAIR")    owner.AnimState:Show("HAIR_NOHAT")    owner.AnimState:Show("HAIR")    if owner:HasTag("player") then        owner.AnimState:Show("HEAD")        owner.AnimState:Hide("HEAD_HAIR")    end    owner:AddTag("monster")    if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend
So I've added this code into my characters .lua file but I'm having difficulty figuring out how to implement it when I, for example, equip a straw hat. Do you mind giving me some more pointers? Would I have to use a "ListenForEvent"? Looking at the beefalo hat code in "hats.lua" there are seperate functions for equipping and unequipping (like the code you gave me) and then both of those functions are used in one function with the code "inst.components.equippable.SetOnEquip() / SetOnUnequip()." I made a function that does just that but I'm still not sure how to call it and if that is all there is to it? How would I go about using those functions to test specific hats?
Link to comment
Share on other sites

By that do you mean inst.AnimState:SetBank() and inst.AnimState:SetBuild() ? Would I have to add an asset to this file? (Sorry this is completely alien to me)

 

This line:

owner.AnimState:OverrideSymbol("swap_hat", "hat_bee_bw_swap", "swap_hat")

Link to comment
Share on other sites

This line:

owner.AnimState:OverrideSymbol("swap_hat", "hat_bee_bw_swap", "swap_hat")

Sorry for being a pain but I'm really confused right now. I need to replace "hat_bee_bw_swap" right? I've been trying everything I can think of but I can't get it to work...
Link to comment
Share on other sites

@ScallyCat, post your code so I can help further. 

 

 

local function OnEquip(inst, owner,)	local fname = "hat_"..name	local build = fname_override or fname	        owner.AnimState:OverrideSymbol("swap_hat", build, "swap_hat")        owner.AnimState:Show("HAT")        owner.AnimState:Show("HAT_HAIR")        owner.AnimState:Hide("HAIR_NOHAT")        owner.AnimState:Hide("HAIR")                if owner:HasTag("player") then            owner.AnimState:Hide("HEAD")            owner.AnimState:Show("HEAD_HAIR")        end			if owner:HasTag("monster") then		owner:RemoveTag("monster")	endendlocal function onUnequip(inst, owner)		owner.AnimState:Hide("HAT")        owner.AnimState:Hide("HAT_HAIR")        owner.AnimState:Show("HAIR_NOHAT")        owner.AnimState:Show("HAIR")        if owner:HasTag("player") then            owner.AnimState:Show("HEAD")            owner.AnimState:Hide("HEAD_HAIR")        end			owner:AddTag("monster")		if inst.components.leader then		inst.components.leader:RemoveFollowersByTag("pig")	endendlocal function TestHat(inst, owner)	inst.components.equippable:SetOnEquip(OnEquip)	inst.components.equippable:SetOnUnequip(onUnequip)end

The "local fname" and "local build" were just copied from hats.lua. I was just playing around with them to see what would work.

Link to comment
Share on other sites

@ScallyCat, you shouldn't overwrite the functions like that. While it is there for you to do you should do something like:

 

local function OnEquip(inst, owner, fname_override)    inst.components.equippable:_OnEquip(inst, owner, fname_override)      if owner:HasTag("monster") then        owner:RemoveTag("monster")    endend   local function onUnequip(inst, owner)    inst.components.equippable:_OnUnequip(inst, owner)      owner:AddTag("monster")           if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend   local function TestHat(inst, owner)       inst.components.equippable._OnEquip = inst.components.equippable.onequipfn    inst.components.equippable:SetOnEquip(OnEquip)    inst.components.equippable._OnUnequip = inst.components.equippable.onunequipfn    inst.components.equippable:SetOnUnequip(onUnequip)   end   

 
Link to comment
Share on other sites

@ScallyCat, you shouldn't overwrite the functions like that. While it is there for you to do you should do something like:

 

local function OnEquip(inst, owner,)    inst.components.equippable:_OnEquip(inst, owner)    if owner:HasTag("monster") then        owner:RemoveTag("monster")    endend local function onUnequip(inst, owner)    inst.components.equippable:_OnUnequip(inst, owner)    owner:AddTag("monster")         if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend local function TestHat(inst, owner)     inst.components.equippable._OnEquip = inst.compoennts.equippable.onequip    inst.components.equippable:SetOnEquip(OnEquip)    inst.components.equippable._OnUnequip = inst.components.equippable.onunequip    inst.components.equippable:SetOnUnequip(onUnequip) end 

 

 

 

 

When I equip the straw hat, it gives me this error:

 

"...modmain.lua:77: attempt to call a method '_OnEquip' (a nil value)"

 

Line 77 being:

inst.components.equippable:_OnEquip(inst, owner)

Not really sure what the underscores are for but even without those I get the same error.

 

Link to comment
Share on other sites

@ScallyCat, Sorry, I've updated the code, it should work now. 

 

Hmm now I get an error that says:

 

"...hats.lua:29: bad argument #1 to 'OverrideSymbol' (string expected, got table)"

 

The 29th line of hats.lua being:

owner.AnimState:OverrideSymbol("swap_hat", build, "swap_hat")
Link to comment
Share on other sites

 

Hmm now I get an error that says:

 

"...hats.lua:29: bad argument #1 to 'OverrideSymbol' (string expected, got table)"

 

The 29th line of hats.lua being:

owner.AnimState:OverrideSymbol("swap_hat", build, "swap_hat")

 

No this is the updated code that you need:

 

local function OnEquip(inst, owner, fname_override)    inst.components.equippable:_OnEquip(inst, owner, fname_override)     if owner:HasTag("monster") then        owner:RemoveTag("monster")    endend  local function onUnequip(inst, owner)    inst.components.equippable:_OnUnequip(inst, owner)     owner:AddTag("monster")          if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend  local function TestHat(inst, owner)      inst.components.equippable._OnEquip = inst.compoennts.equippable.onequipfn    inst.components.equippable:SetOnEquip(OnEquip)    inst.components.equippable._OnUnequip = inst.components.equippable.onunequipfn    inst.components.equippable:SetOnUnequip(onUnequip)  end  

Link to comment
Share on other sites

No this is the updated code that you need:

 

local function OnEquip(inst, owner,)    inst.components.equippable:_OnEquip(inst, owner)     if owner:HasTag("monster") then        owner:RemoveTag("monster")    endend  local function onUnequip(inst, owner)    inst.components.equippable:_OnUnequip(inst, owner)     owner:AddTag("monster")          if inst.components.leader then        inst.components.leader:RemoveFollowersByTag("pig")    endend  local function TestHat(inst, owner)      inst.components.equippable._OnEquip = inst.compoennts.equippable.onequipfn    inst.components.equippable:SetOnEquip(OnEquip)    inst.components.equippable._OnUnequip = inst.components.equippable.onunequipfn    inst.components.equippable:SetOnUnequip(onUnequip)  end  

 

 

Sorry I should have clarified, I used the same exact code as you provided. Here's the error traceback:

 

http://imgur.com/MTVJzwL

Link to comment
Share on other sites

Sorry I should have clarified, I used the same exact code as you provided. Here's the error traceback:

 

http://imgur.com/MTVJzwL

 

Ohh, that is because the third parameter did not show up properly. I've updated both code blocks with the correct third parameter. Sorry about that, not sure why it got left off on both codes.

 

Simply add ", fname_override" behind owner in the equip function and when you call _OnEquip inside the same function.

 

Edit: Spelling

Link to comment
Share on other sites

Ohh, that is because the third parameter did not show up properly. I've updated both code blocks with the correct third parameter. Sorry about that, not sure why it got left off on both codes.

Simply add ", fname_override" behind owner in the equip function and when you call _OnEquip inside the same function.

Edit: Spelling

Okay cool, it might be a couple days before I can back to you on whether or not this works but either way I'll let you know :)

Link to comment
Share on other sites

Ohh, that is because the third parameter did not show up properly. I've updated both code blocks with the correct third parameter. Sorry about that, not sure why it got left off on both codes.

Simply add ", fname_override" behind owner in the equip function and when you call _OnEquip inside the same function.

Edit: Spelling

Hmm I'm still getting the same error message...

Link to comment
Share on other sites

Make sure you spell check everything, one of the components is spell incorrectly. (Line 21 specifically in the code I provided.)

Yeah I had noticed that early on but I ckecked everything again thoroughly and it all seems to match. Like I said its the same error message as the picture I sent before. Something about expecting a string but recieving a table?

Link to comment
Share on other sites

Yeah I had noticed that early on but I ckecked everything again thoroughly and it all seems to match. Like I said its the same error message as the picture I sent before. Something about expecting a string but recieving a table?

 

Please post your entire modmain.lua file so I can look at it.

Link to comment
Share on other sites

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.

×
  • Create New...