#Health Bar compile error: Attempt to call function 'init_health' in base 'null instance' on a null

1 messages · Page 1 of 1 (latest)

cosmic ibex
#

Hello! I am working on setting up a health bar for a game! I followed this video:https://www.youtube.com/watch?v=f90ieBOoIYQ&t=367s. Whenever I try to run the game I get the error, Attempt to call function 'init_health' in base 'null instance' on a null instance.

Let's make a health bar / life bar inspired by popular fighting games such as Street Figther, Mortal Kombat, Tekken, Dragon Ball Fighterz, and even a shooter like Borderlands. The damage indicator aspect of this health bar is what really pushes it to the next level, both in terms of accessibility and game feel. And it's not made strictily for fi...

▶ Play video
visual leaf
#

Null instance means your code isn't finding the object it expects

#

Maybe you have to set an exported variable to a value? Maybe something is missing a child, or appears somewhere it didn't expect in the tree?

cosmic ibex
#

here is the line that causes the error

func _ready():
health_bar.init_health(health)

visual leaf
#

Anyway: the line it's complaining about has a node- typed variable that is empty. So whatever is supposed to assign it either isn't running, or can't find the thing it's supposed to insert

#

What sets health_bar?

cosmic ibex
#

func set_health(value):
health_bar._set_health(value)

visual leaf
#

No. That uses health bar; it doesn't assign anything to health bar itself

#

What I'm looking for has health_bar on the left hand side of an equals sign

cosmic ibex
#

oh my bad sorry I'm very new

#

@onready var health_bar: ProgressBar = $HealthBar

visual leaf
#

Ok! So at the time that runs (when this node is ready), there isn't a child named exactly HealthBar in the tree

#

(of the node that has this script attached)

#

It should also be complaining about this in the debugger and/or output tabs

cosmic ibex
#

nothin in output but the error is in debugger

visual leaf
#

Ok! So fix it: either change how you find the progress bar you want (different name? Different location?) or be more explicit and hook it up in editor with an export var instead of expecting it to find its own dependencies

cosmic ibex
#

how I have it set up is there is a hud node with the health bar inside and inside the health bar is two progress bars and a timer

visual leaf
#

Perhaps that's it; what you're calling HealthBar isn't the specific progress bar this script expects to be named HealthBar?

cosmic ibex
#

as a direct child of the player?

visual leaf
#

You've given me no names. Direct child of this script; what's this script?

cosmic ibex
#

oh sorry! the problem line is in the player script

#

would you like the github link?

visual leaf
#

Hang on, have we been looking at more than one script here? There's been a few lines (the error; the variable definition)

#

Sure (in general don't ask to ask to provide links to learn more ; worst I can do is not click!)

cosmic ibex
#

the two lines of code I sent were both from the player script but they are calling functions from the health bar script

visual leaf
#

Can't open; private repo?
If it's short enough, just paste code here

cosmic ibex
#

let me change that

#

sorry again very new

atomic obsidianBOT
#
Embedding code in discord messages
Inline Code

When you surround some words with single backticks like `this`, it will be formatted as code.

Code Blocks

You can also include code blocks by surrounding the code with three backticks. If you add "swift" then you will also get basic syntax highlighting:
```swift
print("hello world")
```
Discord will then show it as a code block like this:

print("hello world")
Upload

If your code snippet is rather long, you can upload it to a text paste site. One option that supports syntax highlighting for GDScript is https://bpa.st/

cosmic ibex
#

okay I think i changed it :)

visual leaf
cosmic ibex
#

'print("hello")'

visual leaf
#

Wrong quotes -- backtick, same key as tilde

#

Near escape, tab, etc

cosmic ibex
#

print('hi')

visual leaf
#

Up above, does my description of your problem make sense to you?

cosmic ibex
#

yes I think so

visual leaf
#

Ok! So: does this node have a direct child of type ProgressBar named HealthBar?

cosmic ibex
#

btw I have to go briefly at 5. I should be back 30 minutes later

cosmic ibex
#

The HUD node is connected to the player node and inside the HUD node is the health bar node

visual leaf
#

Then this won't work! (The line with the $ on it will fail; that means "get me the child named...")

cosmic ibex
#

gotcha

#

okay so can I still grab health bar with something like $HUD_NODE/healthbar

visual leaf
#

Yeah! I think that should work, if it doesn't, $"HUD_NODE/healthbar" (check capitalization against the exact names in the tree; it's picky, and that's odd!) will.

cosmic ibex
#

Thank you so much

#

let me try it

#

It worked!!

unreal dew
#

you could also do @export var health_bar: ProgressBar instead, and then assign the correct health bar in the inspector to the export variable

#

which has the benefit of not breaking if you move stuff around

#

(right now, if you move the structure, you'll need to change the path again- for every single thing that is moved and referenced like this)

#

(when you set a node in an export variable and move it, the reference is moved automatically)

cosmic ibex
unreal dew
cosmic ibex
#

select which thing?

unreal dew
#

you're exporting in the player script

#

so you need to be looking at the player scene

cosmic ibex
unreal dew
#

so like, you click on the player node

#

and look at the inspector window

#

for it

#

(also, your script file has the "have not saved changes" indicator, you might need to hit save first)

cosmic ibex
#

Okay so I have my HUD node under my player node only in the game scene, but in the player scene the HUD node isnt't there (the hud has the health bar) so I don't think I can assign it

unreal dew
#

Why is the HUD node only there in the game scene?

#

you can assign it in the game scene, if you click on the player node there, to be clear

cosmic ibex
#

oh okay awesome

unreal dew
#

but having the HUD be "part of the player" but "not in the player unless in the game scene" screams "something is wrong here" to me

cosmic ibex
#

yeah

#

I had to do a bunch of conviluted stuff to get the health bar to show

unreal dew
#

Personally, for my project, my HUD is part of an autoloaded node

#

so it's attached effectively to the root on it's own

#

but, my system isn't the simplest lol, I've done a lot of complex stuff

visual leaf
#

They're following Brackeys tutorial iirc

#

Which I think does this (sticks UI under player), or at least I've seen this pattern before on tuts.

#

I think it's not very sensible long term, but short term? Whatever compiles 😉

cosmic ibex
#

okay so I have two progress bars in health bar scene and that is in the HUD that I will add more to at sp. That HUD is in the game scene under player

unreal dew
#

^ yeah, if you're just doing tutorials and stuff to learn, then whatever compiles works

#

here's how my HUD is actually set up

#

(the invisible texture rect is just for visual alignment in the editor)

#

the HUDLayer is an autoload, so it attaches directly to the root, and it is a CanvasLayer node, so all it's children are drawn as part of a separate draw layer

#

the light casts on my health bar, which has normalmapping and such for lighting effects

cosmic ibex
#

oh cool

unreal dew
#

and then the health bar links itself to the player through my PlayerSpawner autoload

#

(whenever the player "respawns", it hooks the new player object to the health bar via signal)

cosmic ibex
unreal dew
#

the main complexities of my system are the global signal linking stuff

#

I have a lot of systems that are autoloaded and handle global stuff

cosmic ibex
#

yeah seems complex

unreal dew
#

like a PauseManager, ItemManager, EventManager, SceneLoader, SaveManager, etc

#

. . . funnily enough, my "main menu" stays as the "active scene" the entire time

#

when I load a level in, it's under the SceneLoader autoload

#

(which also handles animations between areas, etc)

#

the ItemManager handles when the player gets new items, and sending item acquired signals out to anything that cares

#

like, when the player gets a jetpack upgrade, the jetpack node of the player needs to know to change it's visuals and behavior, so it listens to that signal

cosmic ibex
#

cool

unreal dew
#

lots of autoloads lmao

cosmic ibex
#

btw I still can't propely export var b/c in the game scene you can't select the health bar b/c thats under the HUD scene

cosmic ibex
unreal dew
#

here, either you'd want something global like my system

#

or probably simpler, for the health bar to be part of the player node

#

which, despite my fancy system, I have a heat bar for the thrusters, which is part of my player node and not my hud system

#

(the heat bar follows the player around, while the health bar is static in the corner of the screen, so it makes more sense this way)

#

(but, even if you want static, that can be accomplished while being part of the player if you use a CanvasLayer as the root of the HUD that you want to stay static)

cosmic ibex
#

what if I attach the HUD to the player?

unreal dew
#

that is what I just suggested lol

cosmic ibex
#

and remove the health from the hud scene and put it under the hud in the player scene

cosmic ibex
unreal dew
#

I mean, either way

cosmic ibex
#

my bad

unreal dew
#

I don't know your exact project structure

#

so I don't know what all you'd need moved

#

but the idea is that if you're trying to access it from the Player's script

#

either you need to have it be globally linked in some way (like my fancy autoloads/signals)

#

or you need it to be a child of the player

cosmic ibex
unreal dew
#

(or, non-globally linked can be a third option for some stuff, but doesn't make any sense really for a HUD, because there's just one player hud)

#

really, the important thing to note is

#

should the health bar stay still, or move around with the player?

cosmic ibex
#

stay still

unreal dew
#

Then, putting a CanvasLayer under the player for the HUD, with the health bar inside that, would be the way to go IMO

#

unless you wanted to try fancy global stuff, which I personally think can be better in the long term, but, only if you really understand it and how you're hooking it together for your project, so, that's definitely a bigger thing to tackle lol

#

but locally under the player would be the simpler option, and a CanvasLayer will make it not move around with the player

cosmic ibex
#

I don't think the scale of the game is that large. I'm just trying to figure out the engine

unreal dew
#

yeah, exactly. Don't need to be that fancy then.

cosmic ibex
#

Although the scope has creeped in my brain

unreal dew
#

lol

cosmic ibex
#

The basic concept is a movement underwater game with like a harpoon grapling hook and you try to get to the surface

unreal dew
#

neat!

cosmic ibex
#

thank you

#

you have like an oxygen bar and the harpoon uses oxygen :)

#

okay if I delete hud from the game scene does that in general delete the HUD node

unreal dew
#

is the HUD saved as a scene file of it's own?

#

if yes, then no, if no, then yes

cosmic ibex
#

K awesome

unreal dew
#

you lose any changes that were modified in the game scene specifically

#

but not any that are part of the hud scene

cosmic ibex
#

okay so I have everything under the player scene now, but it wont let me call it health bar

#

it really wants it to be health bar 2

unreal dew
#

that seems weird

visual leaf
#

Hud node is a scene instance

#

Whatever it had, it has; that may include its own HealthBar

unreal dew
#

oh, err

#

yeah

#

you probably have 2 health bars there lol

#

you don't have the HUD_NODE's child nodes visible here

#

that are part of it's scene

cosmic ibex
visual leaf
#

Click editable children

cosmic ibex
#

I took the health bar out of the HUD scene

unreal dew
#

it should be ONLY in the hud scene

#

and the hud scene is inside the player scene

cosmic ibex
#

then I still can't select the health bar

cosmic ibex
visual leaf
#

Right click the instantiated scene/node; the context drop-down menu

cosmic ibex
#

oh cool

unreal dew
#

the uh fix for that is

#

don't export the health bar in the player script, export the hud scene

#

and then in a script for the hud scene, export the health bar

#

@export var HUDScene: CanvasLayer
->
@export var health_bar: ProgressBar

#

and then you would do like

#

HUDScene.health_bar in the player script

#

instead of health_bar

cosmic ibex
visual leaf
#

The error you described doesn't have much to do with what you responded to

visual leaf
#

I'm just a simple change to the way the scene tree is displayed, aw shucks

#

Your null pointer has some other cause

unreal dew
#

where is init_health being called?

cosmic ibex
#

in the player script

unreal dew
cosmic ibex
#

oh nvm it's working

#

I think I missed clicked or something my bad

unreal dew
#

happens

cosmic ibex
#

You guys are so helpful by the way thank you

unreal dew
#

because that way you can edit the scenes entirely separately

#

you can just edit the HUD scene to put whatever HUD stuff you want on it, move stuff around, etc

#

and the player wouldn't even know

#

(matters more if you have more than one thing to export from the HUD lol)

#

(definitely not a big deal for a small test project)

cosmic ibex
#

sorry I was re reading everything so I understood what you were putting down

#

I think I am gonna leave it as is for now and just work on making the health bar go down

#

my idea is to make a damage scene that I can add to enemies containing a area 2D node

visual leaf
#

"damage scene"->"hitbox". Sounds great!