Jump to content

Add to INV on Eaten


Recommended Posts

I was wanting to have an food item, that upon being eaten, gives the player an object

This is what I am working with, but nothing happens.

local function OnEaten(inst, eater)
    inst.components.inventory:GiveItem(GLOBAL.SpawnPrefab("drupe"))
end

And, I would prefer that it only works if the eater is a player, so that if another mob eats the food, they don't get the item.

 

 

 

Link to comment
Share on other sites

2 hours ago, Ultroman said:

The inst parameter you have there is the food item. The player OR MONSTER/CREATURE eating it, is the eater parameter. Remember to check whether the eater is in fact a player.

First, Thank you,

Secondly, I guess I should have prefaced with . I don't know lua coding

I just search though the game for bits of code and try to cobble them together for what I want to do.

So I am not sure what to do to fix what you are referring to

But if I understand you correctly, what I've done is try to give an item to the food object, instead of to the person eating it.

=-=-=--=

This doesn't work either...

local function OnEaten(inst, eater)
    eater.components.inventory:GiveItem(GLOBAL.SpawnPrefab("drupe"))
end

=-=-=

also would this work for checking if eater is a player?

if eater,components.player then
  ...
end

Thank you again,

 

Link to comment
Share on other sites

LUA scripting is really one of the simpler languages you can learn. It wouldn't take you more than a few hours to learn it, and maybe a week of spare-time to get decent at. Do the LUA Crash Course at least. It'll be good for you :) Programming / scripting is a massively underrated skill. If I want a program that does something neat, I can usually bash it together within a few hours, and sometimes save myself days of work, or a lot of annoying clicks and waiting. I coded a program that looks through the webpages of all my mods on Steam, and reads the subscription stats for them, and shows their progress in a very simplistic way, so I can see how they're doing, and can gauge if there's something wrong with one of them (since not everyone who encounters a bug actually reports it; they just unsubscribe).

But I digress :)

Your new eater function should work, so long as there actually is a prefab called "drupe". Have you tested this item? Can you spawn it manually in the game, by typing this in the console c_spawn("drupe")

If so, you may not have actually linked up the OnEaten function properly. I'd need to see your whole mod file.

For the second thing, no, that would not work. This would:

if eater:HasTag("player")

If none of that works, you can try putting .inst after eater, so eater.inst, but I don't think it'll help. Please post your full mod here in a zip-file.

Link to comment
Share on other sites

 

well, technically no I haven't created the drupe yet,
I wanted to make sure the concept worked first.before creating the item,

so I've been testing with just 'seeds' from the game

Below is the full item.lua

Spoiler

 

local assets =
{
    Asset("ANIM", "anim/guavacado.zip"),
    Asset("ATLAS", "images/inventoryimages/guavacado.xml"),
}

local prefabs =
{
    "guavacado_cooked",
    "spoiled_food",
}

local function fn(Sim)
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    if TheSim:GetGameID() =="DST" then inst.entity:AddNetwork() end

    MakeSmallPropagator(inst)
    MakeInventoryPhysics(inst)
    
    if TheSim:GetGameID()~="DST" then
     if IsDLCEnabled(CAPY_DLC) then
        MakeInventoryFloatable(inst, "idle_water", "idle")
        MakeBlowInHurricane(inst, TUNING.WINDBLOWN_SCALE_MIN.LIGHT, TUNING.WINDBLOWN_SCALE_MAX.LIGHT)
    end
    else end
    
    inst.AnimState:SetBank("guavacado")
    inst.AnimState:SetBuild("guavacado")
    inst.AnimState:PlayAnimation("idle")
    
    if TheSim:GetGameID()=="DST" then
        if not TheWorld.ismastersim then
            return inst
        end
        
        inst.entity:SetPristine()
    end
    
    inst:AddComponent("edible")
    inst.components.edible.foodtype = "VEGGIE"
    inst.components.edible.foodstate = "RAW"
    inst.components.edible.healthvalue = TUNING.HEALING_SMALL
    inst.components.edible.hungervalue = TUNING.CALORIES_MED
    inst.components.edible.sanityvalue = 0
 
    --[[
    inst:AddComponent("dryable")
    inst.components.dryable:SetProduct("guavacado_dried")
    inst.components.dryable:SetDryTime(TUNING.DRY_FAST)
    ]]
    
    inst:AddComponent("inspectable")

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/guavacado.xml"
    
    inst:AddComponent("stackable")
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM
    
    inst:AddComponent("perishable")
    inst.components.perishable:SetPerishTime(TUNING.PERISH_SUPERFAST)
    inst.components.perishable:StartPerishing()
    inst.components.perishable.onperishreplacement = "spoiled_food"

    inst:AddComponent("cookable")
    inst.components.cookable.product="guavacado_cooked"

    inst:AddComponent("bait")
    inst:AddComponent("tradable")
    
    if TheSim:GetGameID()=="DST" then
        MakeHauntableLaunchAndPerish(inst)
    else end
        
    return inst
end

local function OnEaten(inst, eater)
  if eater:HasTag("player") then
      eater.components.inventory:GiveItem(SpawnPrefab("seeds"))
  else end  
end

return Prefab( "common/inventory/guavacado", fn, assets, prefabs )

 

If you need to see the full mod it is here:
https://steamcommunity.com/sharedfiles/filedetails/?id=381565292

However the changes I am trying to add aren't in the published version of the mod yet.

 

 

 

Link to comment
Share on other sites

Yeah, it's as I thought. Just pasting in a function doesn't do anything. You have to call it somewhere. In this case, you have to attach it to the edible component. After inst:AddComponent("edible"), do this:

inst.components.edible:SetOnEatenFn(OnEaten)

Make sure to move you OnEaten function ABOVE the fn(Sim) function. Functions need to be "declared" before you can refer to them. I can't remember if this is also true when just referencing it, but it IS true in most cases when calling them. But I won't go into that. You'll learn that in your LUA tutorials.

Oh, and you can remove the else here

local function OnEaten(inst, eater)
  if eater:HasTag("player") then
      eater.components.inventory:GiveItem(SpawnPrefab("seeds"))
  else end  <===== HERE
end

It should just be "end", not "else end".

Edited by Ultroman
Link to comment
Share on other sites

18 minutes ago, Ultroman said:

Yeah, it's as I thought. Just pasting in a function doesn't do anything. You have to call it somewhere. In this case, you have to attach it to the edible component. After inst:AddComponent("edible"), do this:


inst.component.edible:SetOnEatenFn(OnEaten)

Make sure to move you OnEaten function ABOVE the fn(Sim) function. Functions need to be "declared" before you can refer to them. I can't remember if this is also true when just referencing it, but it IS true in most cases when calling them. But I won't go into that. You'll learn that in your LUA tutorials.

 

Wow - yeah, whats funny is I was looking through the Mandrake for onEaten effect, and sure enough it has that line

-- although it should be 'inst.components.edible....'

But that solved the problem- I just tested it and it worked perfectly!

thank you much again for all your help, and for the link to lua tutorials, It looks like it will be very handy.

 

 

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...