#What is this bug

1 messages · Page 1 of 1 (latest)

foggy flax
#

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

old ibex
#

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
old ibex
#

to set player directly instead of using groups

foggy flax
#

I

#

O*

foggy flax
#

I get thiis

magic crater
foggy flax
#

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

magic crater
# foggy flax

And which script is this? Because it says that it is game_manager.gd

#

Actually, they look very similar

foggy flax
#

GDscript?

magic crater
#

I mean, what file

foggy flax
#

umm

magic crater
#

It looks like you are setting the "player" variable to be a GameManager

foggy flax
#

GameManager is a node

#

Player is a charbody2D

magic crater
#

I know, but you are setting "player" to be a GameManager

#

So it obviously won't work

foggy flax
#

then how do i do it?

magic crater
#

You have to set it to a CharacterBody2D node

#

I am not understanding your setup

foggy flax
#

the gamemanger?

magic crater
#

The "player" variable

#

GameManager.player = someCharacterBodyNode

foggy flax
#

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

magic crater
#

That seems fine, but this is not the code that was causing the error

foggy flax
#

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😭

magic crater
#

I mean, i am just trying to help with your crash first. The game won't even work unless that is fixed first.

foggy flax
#

true

#

is this esier to fix?

magic crater
#

What calls Mlife()?

foggy flax
magic crater
#

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.

foggy flax
#

Mlife?

#

its just to remove 1 heart when i touche are2d

#

like in the gui

magic crater
# foggy flax Mlife?

This function i mean, it should have a green icon if it was connected trough the editor

foggy flax
#

what do you mean it works when im not trying to add this anim

#

to my player

magic crater
#

I am not sure i understand the issue

#

Then it is connected trough code i assume?

foggy flax
#

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

magic crater
#

So if you remove the play("Hit") the rest works?

foggy flax
#

yes i can show you

magic crater
#

The animation does not touch the hitboxes or anything functional right? It is just an AnimatedSprite2D(?)

foggy flax
#

nah its just a animated sprite

magic crater
#

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

foggy flax
#

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")

magic crater
#

I am completely lost as to what is the current issue or what is causing it

foggy flax
#

so am i

#

do you know how i can add so my player can play an animaton when i lose a life?

magic crater
#

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

foggy flax
#

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?

magic crater
#

This isn't really an advanced topic, there's just not enough info here.

foggy flax
#

what info do you need?

#

i can give you anything

magic crater
#

Your scripts and scene setup for starters

#

Just something that we can look at, otherwise we can only guess

foggy flax
#

that is every thin i have

magic crater
#

is the Player node in the "Player" group?

foggy flax
#

yes

magic crater
#

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

foggy flax
#

how do i do it then

magic crater
#

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

foggy flax
#

where do i add that?

magic crater
#

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.

foggy flax
#

bruh what do you mean

magic crater
#

Which point?

foggy flax
#

all of it

#

im not good at coding

magic crater
#

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.

foggy flax
#

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")

magic crater
#

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)

foggy flax
#

so should it work now?

magic crater
#

Give it a test, stuff rarely works first try. But you can't know until you try

foggy flax
#

nope

magic crater
#

Did anything change?

foggy flax
#

still the same bug

magic crater
#

I tought this was fixed? I guess not

#

Put a breakpoint in the _ready() function of GameManager
Just to make sure it runs

foggy flax
#

o wait i added

#

thje

#

player.play_hit()

#

back

#

no nothing changed same ass the video i sendt

#

it works

magic crater
#

It still crashes right?

foggy flax
#

and it works🎉

foggy flax
magic crater
#

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?

foggy flax
#

no

#

wait

magic crater
#

welp, if it isn't broken, don't fix it

foggy flax
#

new bug

#

after i die that bug pops up

magic crater
#

I assume that when the player dies, they get deleted, or the scene gets reloaded (?)

foggy flax
#

the scene gets reloaded

magic crater
#

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

foggy flax
#

the player dosent have a ready func

magic crater
#

Just give em one

#

All Nodes have a _ready() function you can replace.

foggy flax
#

i added it and im trying now

#

it works🎉

#

is there anything else i should do?

magic crater
#

I am not seeing any immediate bugs in the code.

foggy flax
#

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

magic crater
#

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.

foggy flax
#

sure...

magic crater
#

You can make the character script reset the health to 4 when it is ready

foggy flax
#

is dose

foggy flax
#

every thing works