Jump to content

Need help - function not doing anything


Recommended Posts

Hi guys !

I'm trying to make my first mod (which is a character) and I can't get my function to work :/

Game doesn't crash or anything, it's just as if I didn't write my function.

It's a perk that would give my character benefits when eating raw mushrooms, here's my code :

 

local function oneat(inst, food)    
    if food and food.prefab == "red_cap" then
           inst.components.health:DoDelta(30)
           inst.components.sanity:DoDelta(5) 
    elseif food and food.prefab == "green_cap" then
           inst.components.health:DoDelta(10) 
           inst.components.sanity:DoDelta(35) 
    elseif food and food.prefab == "blue_cap" then
           inst.components.health:DoDelta(10)
           inst.components.sanity:DoDelta(20) 
     end
end

 

I put this in my character prefab and I thought it would work but it doesn't, I'm probably just plain stupid and need to "activate" the function somewhere but I'm clueless, so a little help would be more than appreciated !  =)

Link to comment
Share on other sites

put this inside master_postinit of YOURCHARACTER.lua :)

inst:ListenForEvent("oneat", function(inst, data) 
if data.food.prefab == "red_cap" then
	inst.components.health:DoDelta(30)
    inst.components.sanity:DoDelta(5) 
elseif data.food.prefab == "green_cap" then
	inst.components.health:DoDelta(10) 
	inst.components.sanity:DoDelta(35) 
elseif data.food.prefab == "blue_cap" then
	inst.components.health:DoDelta(10)
	inst.components.sanity:DoDelta(20) 
end)

 

Link to comment
Share on other sites

3 hours ago, SuperDavid said:

put this inside master_postinit of YOURCHARACTER.lua :)


inst:ListenForEvent("oneat", function(inst, data) 
if data.food.prefab == "red_cap" then
	inst.components.health:DoDelta(30)
    inst.components.sanity:DoDelta(5) 
elseif data.food.prefab == "green_cap" then
	inst.components.health:DoDelta(10) 
	inst.components.sanity:DoDelta(35) 
elseif data.food.prefab == "blue_cap" then
	inst.components.health:DoDelta(10)
	inst.components.sanity:DoDelta(20) 
end)

 

Hey ! Thanks for stopping by

SuperDavid ! 

When I pur your code inside the master_postinit, the game crashes when I launch the server, here's the log :

 

[string "scripts/mainfunctions.lua"]:119: Error loading file prefabs/walter
[string "../mods/Walter/scripts/prefabs/walter.lua"]:76: unexpected symbol near ')'
LUA ERROR stack traceback:
        =[C] in function 'assert'
        scripts/mainfunctions.lua(119,1)
        =(tail call) ?
        =[C] in function 'xpcall'

 

It refers to the " end)", the last line of your code, any idea how to fix this ?

Link to comment
Share on other sites

Sorry I missed one "end", hahaha :wilson_dorky:!

Spoiler

inst:ListenForEvent("oneat", function(inst, data) 
	if data.food.prefab == "red_cap" then
		inst.components.health:DoDelta(30)
		inst.components.sanity:DoDelta(5) 
	elseif data.food.prefab == "green_cap" then
		inst.components.health:DoDelta(10) 
		inst.components.sanity:DoDelta(35) 
	elseif data.food.prefab == "blue_cap" then
		inst.components.health:DoDelta(10)
		inst.components.sanity:DoDelta(20) 
	end
end)

 

 

Link to comment
Share on other sites

2 hours ago, SuperDavid said:

Sorry I missed one "end", hahaha :wilson_dorky:!

  Reveal hidden contents


inst:ListenForEvent("oneat", function(inst, data) 
	if data.food.prefab == "red_cap" then
		inst.components.health:DoDelta(30)
		inst.components.sanity:DoDelta(5) 
	elseif data.food.prefab == "green_cap" then
		inst.components.health:DoDelta(10) 
		inst.components.sanity:DoDelta(35) 
	elseif data.food.prefab == "blue_cap" then
		inst.components.health:DoDelta(10)
		inst.components.sanity:DoDelta(20) 
	end
end)

 

 

Ahah no worries !  =)

It's working perfect now, thanks to you ! 

If you don't mind, I'd like it if you could explain why my version wasn't working, so that I don't make the same mistake again ^^ 

But that's totally ok if you don't, your work here is done ahah !

Thanks again !

Link to comment
Share on other sites

Because you probably missed the "listen for event" in your master post init.

You have two ways to do it, usually. The way superdavid did it, a big function directly in the listed for event, or in two steps. For example, i have this in my character master post init :


    inst:ListenForEvent("killed", onkilled)

Then in a separate function (in the same file) i have :


local function onkilled(inst, data)
if data.victim:HasTag("animal") then
 inst.components.sanity:DoDelta(-TUNING.SANITY_SMALL)
 end
end

But the function itself will never works without the "listenforevent".

 

(Also, the data. part must be important too)

Edited by Lumina
Link to comment
Share on other sites

22 hours ago, Lumina said:

Because you probably missed the "listen for event" in your master post init.

You have two ways to do it, usually. The way superdavid did it, a big function directly in the listed for event, or in two steps. For example, i have this in my character master post init :



    inst:ListenForEvent("killed", onkilled)

Then in a separate function (in the same file) i have :



local function onkilled(inst, data)
if data.victim:HasTag("animal") then
 inst.components.sanity:DoDelta(-TUNING.SANITY_SMALL)
 end
end

But the function itself will never works without the "listenforevent".

 

(Also, the data. part must be important too)

Oh ok I understand now ! 

Thank you !  =)

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