#Help

1 messages · Page 1 of 1 (latest)

real zenith
#

that should be pretty easy. On the player you could just make a function called hit(), or take_damage() or whatever. All it needs would be :

animated_sprite.play("hit")

#

and then in the code for the area2D serving as the slime's killzone, under

_on_body2D_entered():
if body.has_method("hit"):
body.hit()

#

it could be a little more complicated than that if you wanted to add being hit to your state machine like maybe if you wanted a cooldown for the animation or damage, or stun the player, but on a basic level that should make the sprite flash red when it enters the slime's area2D

real zenith
#

actually if you wanted to have this animation play whenever you take damage, not just on the slime, you could trigger it in the gameManager function Mlife instead. Like where it says to reduce health by 1. However then the game manager will need to know about the player, since you'll have to trigger the player's "hit" function in the GameManager. which isn't inherently bad but you'd have to set that up.

Unless you might not want that animation playing in some scenarios like if the player falls off the edge or loses hearts a different way though that probably wouldn't be a big issue.

On that note though, I think it would be better if Mlife takes an argument, so that you can lose any amount of health, not just 1. Like

func Mlife(dmg : int) :
life -= dmg

then on the slime area2D, GameManager.Mlife(1) to make him lose 1 health. That way you could have an enemy that deals two damage or whatever by changing that number

next sail
#

Thank you garmpeeld ill try this out

real zenith
#

so if you wanna do that instead, get the player node in gamemanager.

is GameManager an autoload?

real zenith
#

ok looks like it must be actually.

I recommend adding player to a Group called Player. Groups can be be referenced from anywhere, so it works for this. Whereas you can't just drag the player node into an autoloaded script.

In Gamemanager:

var player :CharachterBody3D = null

func ready():
player = get_tree().get_first_node_in_group("Player")

then in Mlife

func Mlife():
player.hit()
life -= 1

#

hopefully that works. that's the gist anyway im sure you can also look up more if needed

next sail
#

i use 2d

#

and the name is Player

#

will it still work?

real zenith
#

sorry i meant 2D

#

forgot your game is 2D, it should still work. Make sure you write everything correctly for your project, since I might have spelled something wrong or made mistakes

next sail
#

what is this player.hit()

#

and player.hit() dose the same

real zenith
#

its a function i made up. so you need to write that function.

#

so in the player script, write a function called "hit"

#

and there is where you finally play the flashing animation you already have

next sail
#

O okay

next sail
real zenith
#

uhh because in the player script you named the function play_Hit

#

but in the gameManager, you ask the player to call the function "hit"

#

in base 'nil' also means that it might not be finding the player. So if you make the functions names' match, and it still doesn't work, make sure the player node is actually added to the group. You can also add a print statement to the ready function like print(player), just to see if gamemanager is finding it correctly when you start the game.

#

ohhh so you wrote func ready(). But it's not the ready function that Godot recognizes and will automatically call. For that you need to use an underscore so like func _ready(). and you'll see the program will autofill with the correct ready function

next sail
#

thanks