So I’m trying to play a hit animation when I touch a Area2D and I have made this code but I always get this red text
#What is this bug
1 messages · Page 1 of 1 (latest)
the function is called play_Hit but you are calling hit
but that that is not even the error, the error is saying player is null
if your game manager is auto-loaded, just have the player assign itself to it on its ready function
func _ready() -> void:
GameManager.player = self
What is that for?
to set player directly instead of using groups
How does the GameManager script look?
this: extends Node
static var coin = 0
static var life = 4
var player :CharacterBody2D = null
func ready():
player = get_tree().get_first_node_in_group("Player")
func Mlife():
player.play_hit()
life -= 1
print(life)
func add_point():
coin += 1
print(coin)
and it is autoloaded
@magic crater
And which script is this? Because it says that it is game_manager.gd
Actually, they look very similar
GDscript?
I mean, what file
umm
It looks like you are setting the "player" variable to be a GameManager
I know, but you are setting "player" to be a GameManager
So it obviously won't work
then how do i do it?
the gamemanger?
i got told that by another guy to do that
he told me this: 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 :CharachterBody2D = null
func ready():
player = get_tree().get_first_node_in_group("Player")
then in Mlife
func Mlife():
player.hit()
life -= 1
That seems fine, but this is not the code that was causing the error
if you go up you can see that he told me to do that
im just trying to get an animaton to play when i get hit😭
I mean, i am just trying to help with your crash first. The game won't even work unless that is fixed first.
What calls Mlife()?
This
This function is not connected to anything, unless you are connecting it trough code somewhere?
I assume this is meant to be connected to a signal, judging by the name.
This function i mean, it should have a green icon if it was connected trough the editor
Didn't you say it isn't working?
I am not sure i understand the issue
Then it is connected trough code i assume?
okay so im trying to add an animaton that plays when i get hit by a slime, so im trying to add that to Mlife func but if i remove the part about adding an animaton for my player 1 heart gets removed and there is no bug
So if you remove the play("Hit") the rest works?
yes i can show you
The animation does not touch the hitboxes or anything functional right? It is just an AnimatedSprite2D(?)
nah its just a animated sprite
An AnimatedSprite has no way to alter anything other than the sprite it shows. So i don't see how that could affect the actual health modifying part
look at the hearts
thats what Mlife dose
these 2 dont need to be there either: var player :CharacterBody2D = null
func ready():
player = get_tree().get_first_node_in_group("Player")
I am completely lost as to what is the current issue or what is causing it
so am i
do you know how i can add so my player can play an animaton when i lose a life?
I mean, how you are doing it right now looks good.
Just make sure that while the animation plays, other animations cannot play.
Otherwise you will start the "Hit" animation and instantly get replaced by another one
it dosent play the animaton i have no way to get my animton to play
thats what im trying to
ass
add*
should i make a post in advanced?
This isn't really an advanced topic, there's just not enough info here.
Your scripts and scene setup for starters
Just something that we can look at, otherwise we can only guess
is the Player node in the "Player" group?
This block here needs to be prevented from running if the Hit animation is playing. Otherwise the Hit animation will always get replaced frame 0
how do i do it then
From this structure with all the code in a single function, it is going to get a bit more convoluted than it needs to.
But without a bunch of work, a fast solution is to use if animated_sprite.animation != "Hit":...
where do i add that?
You can encase the entire block of animations in it.
Or you can invert it and straight up terminate the whole function when it is true, using return
I recommend taking all the animation code in that screenshot and putting it in its own function.
bruh what do you mean
Which point?
IF creates a "block" of code. Everything inside of it only works if the condition is true.
So if you put all this code inside an IF that checks for the animation, it will enable or disable it based on that condition.
It is pretty intuitive "IF this is true: do X"
Inverting a condition just means making it have the opposite result
So instead of animation == "Hit" you can use animation != "Hit" or not (animation == "Hit")
Putting the code in its own function means literally that.
Create a new function like process_animations() and cut+paste all of this code inside of it.
And in its place, just call the process_animation() function.
so this? if animated_sprite.animation != "Hit":
if is_on_floor():
if velocity.x == 0:
animated_sprite.play("Idle")
else:
animated_sprite.play("Run")
else:
animated_sprite.play("Jump")
That is a way yeah.
Just make sure that all of the old animation code is further to the right than this top "IF"
IF blocks define their "block" by everything that is further to the right of them (until something is no longer to the right, there is where they end permanently)
so should it work now?
Give it a test, stuff rarely works first try. But you can't know until you try
nope
Did anything change?
still the same bug
I tought this was fixed? I guess not
Put a breakpoint in the _ready() function of GameManager
Just to make sure it runs
o wait i added
thje
player.play_hit()
back
no nothing changed same ass the video i sendt
it works
i did this
It still crashes right?
and it works🎉
no
That's weird, i just realized that the _ready() of the GameManager should not be working, but i guess something else is fixing it?
The player script has something like GameManager.player = self?
welp, if it isn't broken, don't fix it
I assume that when the player dies, they get deleted, or the scene gets reloaded (?)
the scene gets reloaded
The player is part of the scene, but the GameManager isn't. It does not get reset when the scene reloads.
I suggest putting this in the _ready() function of the player GameManager.player = self
This would make it so whenever a player is added to the scene, the GameManager gets updated
the player dosent have a ready func
I am not seeing any immediate bugs in the code.
nope none
um
this is after ive died 2 times
so its only that it replays the dead thing when i get hit another time
how can i fix that
Since "life" is stored in the GameManger, which is not being reset at any point. I assume the player just respawns with 0 life.
And only updates when it takes any damage.
sure...
You can make the character script reset the health to 4 when it is ready
is dose
every thing works