Eremus007 Posted January 13, 2016 Share Posted January 13, 2016 I am trying to write a code so my character gains 5 sanity (sanitymult.tiny) when he is has completely mined a gold vein or boulder.This code doesn't let the game crash but it doesn't work. Like this in the modmain: function onfinish (inst, destroyer) if inst:HasTag ("boulder" and destroyer and destroyer:HasTag("dwarf") then inst.components.sanity:DoDelta(TUNING.SANITY_TINY) endend Can someone tell me what I am doing wrong?I would really appreciate it Link to comment Share on other sites More sharing options...
Developer ImDaMisterL Posted January 13, 2016 Developer Share Posted January 13, 2016 (edited) Hey dude, welcome to the forums!EDIT: Muche seems to have handled it. Yay! Edited January 13, 2016 by ImDaMisterL Link to comment Share on other sites More sharing options...
Muche Posted January 13, 2016 Share Posted January 13, 2016 It seems to me that inst is intended to be the rock and destroyer the player.Maybe try this:function onfinish(inst, destroyer) if inst:HasTag("boulder") and destroyer and destroyer:HasTag("dwarf") then if destroyer.components.sanity then destroyer.components.sanity:DoDelta(TUNING.SANITY_TINY) end endend Link to comment Share on other sites More sharing options...
Eremus007 Posted January 13, 2016 Author Share Posted January 13, 2016 I put that code into the modmain and made sure I added the "dwarf" tag to my character.But unfortunately it still doesn't give my character sanity whenever he completely mines gold veins or stone. Is it just that the code doesn't work or do I have to copy it in the MY_CHARACTER_PREFAB file instead of the modmain? Link to comment Share on other sites More sharing options...
Eremus007 Posted January 13, 2016 Author Share Posted January 13, 2016 Oh, and thanks for the warm welcome Link to comment Share on other sites More sharing options...
Developer ImDaMisterL Posted January 13, 2016 Developer Share Posted January 13, 2016 Is it just that the code doesn't work or do I have to copy it in the MY_CHARACTER_PREFAB file instead of the modmain? I'm pretty sure this kind of code should go into your character's prefab, yeah! Oh, and thanks for the warm welcome Link to comment Share on other sites More sharing options...
DarkXero Posted January 13, 2016 Share Posted January 13, 2016 The function alone in modmain does nothing. onfinish is just a name. Who and when calls the function if it's just dumped in modmain?If you dump it like that in your character prefab, nothing will also happen. So we are going to listen to the event pushed to a worker when a workable gets finished. Put this function on your character prefab filelocal function OnFinishedWork(inst, data) local target = data.target if target and target:HasTag("boulder") then inst.components.sanity:DoDelta(TUNING.SANITY_TINY) endendand put this inside the master_postinit functioninst:ListenForEvent("finishedwork", OnFinishedWork)When a workable gets finished, this code runsworker:PushEvent("finishedwork", { target = self.inst, action = self.action })which means that all functions assigned to the worker, to its "finishedwork" section, via ListenForEvent, will get called and passed two arguments: worker (the source), and data (the table you see there, with target (the object worked) and action done). Following that, in OnFinishedWork, inst is the dwarf, and data is the table. Then I pick up target from the table and check if it's a boulder. Link to comment Share on other sites More sharing options...
Eremus007 Posted January 13, 2016 Author Share Posted January 13, 2016 THAT WORKED! You're a genius sir! Thanks! And thanks to everyone else that left a comment! Link to comment Share on other sites More sharing options...
Developer ImDaMisterL Posted January 13, 2016 Developer Share Posted January 13, 2016 You're a genius sir! How does DarkXero not have a modder badge yet? He SURE deserves one! Link to comment Share on other sites More sharing options...
Eremus007 Posted January 13, 2016 Author Share Posted January 13, 2016 How does DarkXero not have a modder badge yet? He SURE deserves one!He sure gets my seal of approval. Link to comment Share on other sites More sharing options...
DarkXero Posted January 13, 2016 Share Posted January 13, 2016 How does DarkXero not have a modder badge yet? You get that badge with gears by uploading a mod to the forums. Clicking the badge leads you to the files uploaded by the person. Link to comment Share on other sites More sharing options...
Developer ImDaMisterL Posted January 13, 2016 Developer Share Posted January 13, 2016 You get that badge with gears by uploading a mod to the forums. I thought you got it by uploading a mod to the forums that gets high-ratings. I know you didn't do that, but you sure deserve one like that. 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