Sign in to follow this  
RadiantRoma

OnFinishedWork function...

Recommended Posts

RadiantRoma    386

Hi, I'm making a custom speaking item, like Lucy. This item is able to chop trees and mine rocks, and I want it to speak after it does so, just like Lucy. I basically copied the code from Lucy, but it just doesn't work. In the item's prefab, I have this right now

local function OnFinishedWork(inst, action)
    if action == ACTIONS.CHOP and inst.components.equippable:IsEquipped() then
        inst.Say(STRINGS.TALISMAN.on_worked)
    end
    print("finishedwork")
end

--later on in the prefab, under masterpostinit
    inst:ListenForEvent("workfinished", OnFinishedWork)
--or
    inst:ListenForEvent("finishedwork", OnFinishedWork)

Lucy uses the listenforevent "finishedwork", but in the workable component I found "workfinished", so I just tried that too.

In game, after chopping a tree, nothing happens. The OnFinishedWork function isn't even called, which makes me think that the ListenForEvent is wrong. 

Any help to solve this would be greatly appreciated! 

Share this post


Link to post
Share on other sites
CarlZalph    4282

Generally for member functions in LUA you'd call it via 'inst:Say' instead of 'inst.Say'.

The colon syntax is sugar shorthand.

'inst:Say()' == 'inst.Say(inst)'

 

As for it not calling the events, "workfinished" is called from the player prefab and not your item's prefab.

So you'll need to listen for the event when it has been equipped in the hands slot and then remove the event listener when it is no longer in the hands slot.

The third parameter for listening to the event should be the holder/wearer- this is where the event is generated from.

Share this post


Link to post
Share on other sites
RadiantRoma    386

@CarlZalph, sorry I haven't been able to figure it out. So workfinished can't be called from the item's prefab, so i should make the character's prefab listen for workfinished? Or just have the character's prefab listen for the event, and then have the function that makes the item speak in the item's prefab?

Share this post


Link to post
Share on other sites
Aquaterion    821
12 hours ago, Cherryzion said:

@CarlZalph, sorry I haven't been able to figure it out. So workfinished can't be called from the item's prefab, so i should make the character's prefab listen for workfinished? Or just have the character's prefab listen for the event, and then have the function that makes the item speak in the item's prefab?

workfinished is called on the object being worked on (like trees, rocks, etc) and finishedwork is called on the entity working on the object (players, pigs, etc). So in your tool prefab, when you equip and unequip it, you need to add and remove the eventlistener on the player

//all these go in your talisman prefab.
local function OnFinishedWork(inst, data)
    //inst here is the player, not the item
    local equipped_hand = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if data.action == ACTIONS.CHOP and equipped_hand and equipped_hand.prefab == "talisman" then
        equipped_hand.Say(STRINGS.TALISMAN.on_worked)
    end
    print("finishedwork")
end

//You should already have these 2 functions or similar, 
//which means you would only need to copy the event related line from each function
local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_object", "swap_talisman", "swap_talisman")//make sure to change these to whatever you had
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
	
    owner:ListenForEvent("finishedwork", OnFinishedWork)
end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")

    owner:RemoveEventCallback("finishedwork", OnFinishedWork)
end

Share this post


Link to post
Share on other sites
RadiantRoma    386

@Aquaterion Thank you so much! i owe you my life

I did have to alter the function a little bit, but other than that it works perfectly!

(spoiler is the altered function in case anyone wants to know it)I also added so it goes if they mine a rock, and only a 51% chance to say it so it's not annoying.

Spoiler

local function OnFinishedWork(inst, data)
    --inst here is the player, not the item. the item is equipped_hand.
    local equipped_hand = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if math.random() <= 0.51 and data.action == ACTIONS.CHOP or data.action == ACTIONS.MINE and equipped_hand and equipped_hand.prefab == "talisman" then
        equipped_hand.components.sentientstick:Say(STRINGS.TALISMAN.on_worked)
    end
    print("finishedwork")
end
 

 

Share this post


Link to post
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
Sign in to follow this