Gabinours Posted August 3, 2017 Share Posted August 3, 2017 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 More sharing options...
. . . Posted August 3, 2017 Share Posted August 3, 2017 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 More sharing options...
Gabinours Posted August 3, 2017 Author Share Posted August 3, 2017 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 More sharing options...
. . . Posted August 3, 2017 Share Posted August 3, 2017 Sorry I missed one "end", hahaha ! 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 More sharing options...
Gabinours Posted August 3, 2017 Author Share Posted August 3, 2017 2 hours ago, SuperDavid said: Sorry I missed one "end", hahaha ! 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 More sharing options...
Lumina Posted August 3, 2017 Share Posted August 3, 2017 (edited) 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 August 3, 2017 by Lumina Link to comment Share on other sites More sharing options...
Gabinours Posted August 4, 2017 Author Share Posted August 4, 2017 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now