Jump to content

Recommended Posts

Today I tried with the help of a friend to create a mechanic that would cause feeding an ally to receive 25% more healing than if he did. The code was like this:
 

local function onatefood(inst, data)
    local food = data.food


    if food and food.components.edible and food.components.edible.healthvalue > 0 then
        -- Encuentra todos los aliados cercanos
        local allies = TheSim:FindEntities(inst.Transform:GetWorldPosition(), 10, {"player"})


        for i, ally in ipairs(allies) do
            if ally ~= inst and ally.components.health then
                local extrahealing = food.components.edible.healthvalue * 0.25
                ally.components.health:DoDelta(extrahealing, true)
            end
        end
    end
end

    local oldoneat = inst.components.eater.oneatfn
inst.components.eater.oneatfn = function(inst, food)
    oldoneat(inst, food)
    onatefood(inst, {food = food})
end

Neither the compiler nor the game detects errors when starting it, but in the end it's as if I hadn't added anything, the extra healing doesn't take place. Have I done something wrong?

Connor Lua ZIP.zip

 

The complete Lua of the character for orientation

I don't believe the game sets an oneatfn for the player by default, those tend to be there for customization purposes, as you can set this yourself without overriding it. Doing 'inst.components.eater:SetOnEatFn(oneatfn)' can be used instead without issue. It looks like only Walter actually has this set, so unless you were setting this on every player character (therefore changing it for Walter) hooking it is really not needed.

Your code also appears to be trying to heal the one doing the feeding (and everyone else around), which I'm not sure was the desired effect by the description you gave (inst is the one being fed, so ally would be all players surrounding them)

However, I think it would be more beneficial to what you're trying to do if you used the ListenForEvent instead, as the one doing the feeding is a given there and you don't have to look for them yourself. Usually the oneatfn would be better than having a listener but you want the feeder variable.

inst:ListenForEvent("oneat", OnEat)


local function OnEat(inst, data)
  -- Your Code Here. data.food is the food and data.feeder is the one feeding the food
end

If this is intended for just your own custom character(s) then putting this in their character prefab would suffice, otherwise you'll need to do AddPrefabPostInit in modmain to give everyone the event listener and function

On 3/24/2023 at 11:42 PM, Merkyrrie said:

I don't believe the game sets an oneatfn for the player by default, those tend to be there for customization purposes, as you can set this yourself without overriding it. Doing 'inst.components.eater:SetOnEatFn(oneatfn)' can be used instead without issue. It looks like only Walter actually has this set, so unless you were setting this on every player character (therefore changing it for Walter) hooking it is really not needed.

Your code also appears to be trying to heal the one doing the feeding (and everyone else around), which I'm not sure was the desired effect by the description you gave (inst is the one being fed, so ally would be all players surrounding them)

However, I think it would be more beneficial to what you're trying to do if you used the ListenForEvent instead, as the one doing the feeding is a given there and you don't have to look for them yourself. Usually the oneatfn would be better than having a listener but you want the feeder variable.

inst:ListenForEvent("oneat", OnEat)


local function OnEat(inst, data)
  -- Your Code Here. data.food is the food and data.feeder is the one feeding the food
end

If this is intended for just your own custom character(s) then putting this in their character prefab would suffice, otherwise you'll need to do AddPrefabPostInit in modmain to give everyone the event listener and function

Very very thanks, i gonna try that

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
×
  • Create New...