Jump to content

[HELP] I am writing a custom code for my dwarven character


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
Link to comment
Share on other sites

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

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

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.

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