Jump to content

Recommended Posts

I'm trying to change what certain foods give for my character.

I need help with a crash that happens when I try eating a Powdercake

image.thumb.png.d73bef8f28e67bc85e2d3e088886f8c1.png

 

this is what I have:

local MakePlayerCharacter = require "prefabs/player_common"


local assets = {
    Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}
local prefabs = {}

-- Custom starting items
local start_inv = { "powcake"
}

-- When the character is revived from human
local function onbecamehuman(inst)
    -- Set speed when reviving from ghost (optional)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "esctemplate_speed_mod", 1)
end

local function onbecameghost(inst)
    -- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "esctemplate_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    end
end


-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
    -- Minimap icon
    inst.MiniMapEntity:SetIcon( "esctemplate.tex" )
end


-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
    -- choose which sounds this character will play
    inst.soundsname = "w"
    
    -- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
    
    -- Stats    
    inst.components.health:SetMaxHealth(100)
    inst.components.hunger:SetMax(250)
    inst.components.sanity:SetMax(140)
    
    -- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
    
    -- Hunger rate (optional)
    inst.components.hunger.hungerrate = 2 * TUNING.WILSON_HUNGER_RATE
    
    inst.components.eater:SetOnEatFn("oneat") 
    
    inst.components.eater.ignoresspoilage = true
    
end



local function oneat(inst, food)
  if inst.components.eater and food.prefab == "rottenegg" then
    inst.components.sanity:DoDelta(0)
    inst.components.health:DoDelta(5)
    inst.components.hunger:DoDelta(40)
  end
   if inst.components.eater and food.prefab == "spoiled_food" then
    inst.components.sanity:DoDelta(0)
    inst.components.health:DoDelta(0)
    inst.components.hunger:DoDelta(16)
  end
  if inst.components.eater and food.prefab == "wetgoop" then
    inst.components.sanity:DoDelta(5)
    inst.components.health:DoDelta(0)
    inst.components.hunger:DoDelta(25)
  end
  if inst.components.eater and food.prefab == "powcake" then
    inst.components.sanity:DoDelta(0)
    inst.components.health:DoDelta(0)
    inst.components.hunger:DoDelta(50)
  end
end

    return MakePlayerCharacter("esctemplate", prefabs, assets, common_postinit, master_postinit, start_inv)

 

 

Also I want to know how to change the speed of the character

Edited by GaryPowers
Link to comment
https://forums.kleientertainment.com/forums/topic/88683-crashing-when-eating/
Share on other sites

On 15/03/2018 at 12:41 AM, kertinker said:

Clearly the problem is you ate a powder cake. What is wrong with you.

But no seriously, you're setting the on eat function to the literal string "oneatfn" not your oneatfn function.

thanks for replying. But I'm not familiar with programming so I have no idea what to change. If you could give me an example it would be greatly appreciated

3 hours ago, GaryPowers said:

thanks for replying. But I'm not familiar with programming so I have no idea what to change. If you could give me an example it would be greatly appreciated

In lua, you may have literal strings like "literal string" which are just the letters l-i-t-e-r-a-l-space-s-t-r-i-n-g. If you print("literal string") it will print exactly that. When it is not enclosed in "", lua considers it to be code, whether it's a number like 42, or the name of a variable like oneatfn. There's no code inside the letters "oneatfn" that handle anything about eating, and in fact, there's no code at all, which means it's not callable. When you set the on eat function, there's no lookup table behind the scenes, that takes strings like that and maps them to the actual function. You have to pass as an argument the name of a function, without double quotes, because everywhere you use that name, the value of the function will be substituted instead. That value is code, is the code you want to be called, and is callable.

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