0 votes

I am currently trying to get the boss on my game to take damage from my character's blast and lose lives. Here is part of the script for the character's blast:

func physicsprocess(delta):
translate(velocity)
velocity.x = SPEED * delta * direction
$AnimatedSprite.play("shoot")
var bodies = getoverlappingbodies()
for b in bodies:
if b.isingroup("Enemy"):
b.queuefree()
for a in bodies:
if a.name == "BossEye":
Game.update
boss(-1) This part right here is the problem
queue_free()

Here is the part of the script for the boss for bosses' hearts:

onready var life = $"bosslives/Lives"
onready var Heartss = [$"bosslives/Lives/Sprite", "bosslives/Lives/Sprite2", "bosslives/Lives/Sprite3", "bosslives/Lives/Sprite4", "bosslives/Lives/Sprite5"]

var Bossheartfull = load("res://Assets/bob.png.png")
var Bosshearthalf = load("res://Assets/bob2.png.png")
var Bossheartempty = load("res://Assets/bob3.png.png")

var livess = 10

func updateboss(delta):
livess += delta
for a in range(len(Heartss)):
Heartss[a].texture = Boss
heartfull
if livess < (a+1)*2:
Heartss[a].texture = Boss
heartempty
if livess == (a*2)+1:
Heartss[a].texture = Boss
hearthalf
if livess == 0:
get
tree().change_scene("res://Scenes/WinScreen.tscn")

asked Dec 7, 2019 in Projects by ItsYoBoi (50 points)

1 Answer

0 votes

This is telling you that Game is a null instance. This means that wherever you've assigned the value of Game it's not a valid node. This is a very common error and is typically the result of using the wrong node path.

Also, I'd point out that using get_overlapping_bodies() is probably the wrong approach here - Area's signals are almost always a better solution. In addition, even if you're calling that for the array of overlapping bodies, it's not a good idea to loop through that array twice in a row.

answered Dec 7, 2019 by kidscancode (16,476 points)

This is the connection for the Game:

onready var Game = get_node("/root/Boss")

The main node on the scene for our level is a 2D node

Can you by any chance help me write it the code to fix this problem? I am very new to Godot.

The code that I first posted first originated from the code for the main character's health. This main character's health is working but bosses' health is not.

Your problem probably stems from the fact that "Boss" is not yet ready when you're calling get_node() on it. Nodes in the tree become ready in a particular order. If you want to be sure that a node is ready when you get a reference to it, either use a signal, or manage the references from a common parent, which will always be ready after all its children.

The script wasnt the problem at all. So on my boss level, I have a canvas layer, and within the canvas layer is two nodes (one for color rect and one for the lives). This is for the players lives and stay on the screen even if the player moves. I had a similar thing for the boss but without a canvas layer. I just deleted it and remade it. I got it to work. Thanks for the help!

Actually I lied sorry. So this part of the code is for the player's hearts:

func update_lives(delta):
    lives += delta
    for h in range(len(Hearts)):
        Hearts[h].texture = heart_full
        if lives < (h+1)*2:
            Hearts[h].texture = heart_empty
        if lives == (h*2)+1:
            Hearts[h].texture = heart_half
    if lives == 0:
        get_tree().change_scene("res://Scenes/YouLose.tscn")

This part of the code is for the bosses' hearts:

func update_boss(delta):
    lives2 += delta
    for h in range(len(Hearts2)):
        Hearts2[h].texture = heart_full
        if lives2 < (h+1)*2:
            Hearts2[h].texture = heart_empty
        if lives2 == (h*2)+1:
            Hearts2[h].texture = heart_half
    if lives2 == 0:
        get_tree().change_scene("res://Scenes/WinScreen.tscn")

The player only has three hearts, and I was trying to give the boss fives hearts. I think this part of the code is the problem:

if lives < (h+1)2:
if lives == (h
2)+1:

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.