Jump to content

Recommended Posts

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)
   end
end
 
Can someone tell me what I am doing wrong?
I would really appreciate it

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

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?

  • Developer
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

:D 

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 file

local function OnFinishedWork(inst, data)	local target = data.target	if target and target:HasTag("boulder") then		inst.components.sanity:DoDelta(TUNING.SANITY_TINY)	endend

and put this inside the master_postinit function

inst:ListenForEvent("finishedwork", OnFinishedWork)

When a workable gets finished, this code runs

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

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

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