Jump to content

[Solved] Need help with attacked event damage thing


Recommended Posts

Hello, if someone can help with this problem I have it would be great since I dunno what to do anymore :D...

So, here's the code...

Spoiler

inst:ListenForEvent("attacked", function(inst, data)
	  local random_say = math.random(1,6)
	  inst.components.sanity:DoDelta(-6)
      inst.components.hunger:DoDelta(-5)
	  
   if random_say == 1 then
   --inst.components.talker:Say("Ugh!")
	  
   elseif random_say == 2 then
   --inst.components.talker:Say("Ow!")
	  
	  elseif random_say == 3 then
   --inst.components.talker:Say("Ngh!")
	  
	  elseif random_say == 4 then
   --inst.components.talker:Say("Argh!")
	  
	  elseif random_say == 5 then
   --inst.components.talker:Say("Urgh!")
	  
	  elseif random_say == 6 then
   --inst.components.talker:Say("Ack!")
   
   end                                                                                     
end)

 

So, what I want to do is when my character gets hit I want 25% of the damage he took when attacked to also hurt his sanity & hunger (he would still take 100% of the health damage).

Because the way my code is now if he gets hit by something like bees then boom instant starvation or insanity while getting hit by something like Deerclops isn't as bad as it should be.. 

 

Also, if you noticed the so many "elseif"  just then my character can say something different when hit...is there a way to shorten that I remember there's something called a table...but I dunno how to do that :wilson_resigned:...

 

And finally one last question if you can help with this too that would be great :)! So, when my character gets hit I want him to say something, but sometimes when he would get hit the hit animation wouldn't play & my character would instantly go to talk anim & say his hit string :(... Is there a way I can prevent that or just get the text to appear over his head without him trying to talk?

Edited by SuperDavid
Link to comment
Share on other sites

about 25% of damage:
I suggest you try to put into this function "fastdump(data)" (put GLOBAL in fornt of it, if it is in modmain).
This will print in your logfile what "data" contains. Unfortunately not everything can be printed that way and will cause a crash. But you could try it at least ;)
That way you should see that data contains the damage value, which you can use (not sure if it is just data.damage).

About shortening your code:
If it should be the same chance for all of theese, you can do it this way:

local words = {"Ugh!","Ow!","Ngh!","Argh!","Urgh!","Ack!"}
inst.components.talker:Say(words[math.random(1,6)]) -- this will randomly choose the first to sixth value of the table

In talker component is also a "ShuptUp" function. You could try this. At the moment I have no idea where it would fit the best to achieve what you want, but you will find out ;)


edit:
you can also take a look at "util.lua", there are some nice functions in it.
Instead of the random solution above, you can also use the function "GetRandomItem(words)".
And if you want to remove the picked item after choosing it, you can use "PickSome([number],words)" (so after 6 times the list will be empty. Not helpful in this case, but maybe it is for other cases ;))

Edited by Serpens
Link to comment
Share on other sites

So, for the damage thing would it be something like this?

inst:ListenForEvent("attacked", function(inst, data) -- For taking 25% damage done to Health also to Hunger & Sanity!
local words = {"Ugh!","Ow!","Ngh!","Argh!","Urgh!","Ack!"}
inst.components.talker:Say(words[math.random(1,6)]) -- this will randomly choose the first to sixth value of the table
inst:DoTaskInTime(0.25, function(inst) inst.components.talker:ShutUp() end) -- For saying something without entering talking anim!

inst.components.hunger:DoDelta(-data.damage * .25)
inst.components.sanity:DoDelta(-data.damage * .25)
end)

EDIT: Here are the working codes!

Edited by SuperDavid
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...