0 votes

Hello everyone!

I have a problem with my godot. I get this error:

Node not Found Background

But it doesn't make any sense for me to receive this error, I don't know why. I know this error is when godot can't find the node, but I don't understand why it can't find the node, it's all right in the programming!

Important informations:

When I test the game, it works normally, but when I test the scene in the editor, it gives these errors, I'm using the tool, because, this script is to work in the game and in the inspector, for game designers, to test the look.

When I turn on the _init function, the error is bigger, it doesn't find, not only the background, but all nodes:

Please someone help me to solve this problem, I don't know what else to do!!!!!

Minha cena:

Meu script:

`tool
extends Node2D

"""##### EXPORTS ######"""

"""         Fonts and Collors"""
export(Font) var single_message_font setget _set_single_message_font
export(Color) var single_message_color_font setget _set_single_message_color_font
export(Font) var number_of_symbols_to_win_font setget _set_number_of_symbols_to_win_font
export(Color) var number_of_symbols_to_win_color_font setget _set_number_of_symbols_to_win_color_font
export(Font) var win_message_font setget _set_win_message_font
export(Color) var win_message_color_font setget _set_win_message_color_font
export(Texture) var background_texture setget _set_background_texture
"""         #################"""

"""         Messages"""
export(String) var ready_text = "PRESS SPIN TO START"
export(String) var good_luck_text = "GOOD LUCK!"
export(String) var pay_line_text = "ON LINE {0} PAYS {1}"
export(String) var scatters_pay_text = "SCATTERS PAY" ##Scatters Pay {0} -> é o 3x 4x e etc..
export(String) var none_text = "NONE"

"""         ########"""

"""         Size and Positions"""
export(Vector2) var number_of_symbols_to_win_position: Vector2 = Vector2(10, 0)
export(Vector2) var win_message_position: Vector2 = Vector2(120, 10)
export(Vector2) var single_message_center_position: Vector2 = Vector2(0, 7)

export(Vector2) var symbol_img_size: Vector2 = Vector2(0 , 0)
export(Vector2) var symbol_img_position: Vector2 = Vector2(0 , 0)
"""         #################"""

"""         variables for tests"""
export(String) var symbol_img_path_default: String = "res://assets/machine/symbols/aladdin/win/aladdin-loop_00.png"
export(String) var test_text = "PRESS SPIN TO START" setget _set_test_single_message
export(bool) var test_pay_message setget _set_test_pay_message
"""         ####################"

"""###################"""

var screen_amount_type : int 
var id = 0
var amount = 0
var symbol_path : String
var win_combo : WinCombo
var win_combo_mapping = {}

onready var background: Sprite = $Background
onready var single_message: Label = $Canvas/SingleMessage
onready var complex_message: BoxContainer = $Canvas/ComplexMessage
onready var number_of_symbols_to_win: Label = $Canvas/ComplexMessage/NumberOfSymbolsToWin
onready var symbol_img: TextureRect = $Canvas/ComplexMessage/SymbolImg
onready var win_message: Label = $Canvas/ComplexMessage/WinMessage
onready var canvas: Control = $Canvas

enum TextType {
    NONE,
    READY,
    GOOD_LUCK,
    PAY_LINE,
    PAY_SCATTERS,
    TEST 
}

class WinCombo :
    var symbol_id : int
    var number_of_trigger_symbols : int


"""Start Functions"""
func _init():
    background = $Background
    single_message = $Canvas/SingleMessage
    complex_message = $Canvas/ComplexMessage
    number_of_symbols_to_win = $Canvas/ComplexMessage/NumberOfSymbolsToWin
    symbol_img = $Canvas/ComplexMessage/SymbolImg
    win_message = $Canvas/ComplexMessage/WinMessage
    canvas = $Canvas

"""###################"""

"""Setters Functions"""
func _set_test_single_message(value) -> void:
    if Engine.editor_hint:
        single_message = $Canvas/SingleMessage
    test_text = value
    _update_labels(TextType.TEST)

func _set_test_pay_message(value) -> void:
    if Engine.editor_hint:
        complex_message = $Canvas/ComplexMessage
        number_of_symbols_to_win = $Canvas/ComplexMessage/NumberOfSymbolsToWin
        symbol_img = $Canvas/ComplexMessage/SymbolImg
        win_message = $Canvas/ComplexMessage/WinMessage
        canvas = $Canvas
    show_payline("3x", 300, symbol_img_path_default)
    pass

func _set_single_message_font(new_single_message_font) :
    single_message_font = new_single_message_font

    if single_message != null:
        single_message.set("custom_fonts/font", single_message_font)

func _set_number_of_symbols_to_win_font(new_single_message_font) :
    number_of_symbols_to_win_font = new_single_message_font
    if number_of_symbols_to_win != null:
        number_of_symbols_to_win.set("custom_fonts/font", single_message_font)

func _set_win_message_font(new_single_message_font) :
    win_message_font = new_single_message_font
    if win_message != null:
        win_message.set("custom_fonts/font", single_message_font)

func _set_single_message_color_font(new_color) -> void:
    single_message_color_font = new_color
    if single_message == null: return
    single_message.set("custom_colors/font_color", single_message_color_font)

func _set_number_of_symbols_to_win_color_font(new_color) -> void:
    number_of_symbols_to_win_color_font = new_color
    if number_of_symbols_to_win == null: return
    number_of_symbols_to_win.set("custom_colors/font_color", number_of_symbols_to_win_color_font)

func _set_win_message_color_font(new_color) -> void:
    win_message_color_font = new_color
    if win_message == null: return
    win_message.set("custom_colors/font_color", win_message_color_font)

func _set_background_texture(new_background_texture):
    if Engine.editor_hint:
        background = $Background
    background_texture = new_background_texture
    if background != null:
        background.texture = background_texture
        background.centered = true
"""###################"""
Godot version 3.2
in Engine by (15 points)

1 Answer

+1 vote
Best answer

This is because you are trying to get nodes in the _init method.
The _init method is executed before the node is ready, that is before_ready is called (see the relevant doc).
Basically, a node is ready when it enters the scene tree at which point it become possible to get_node, but not before.

The errors in the message log are occuring when the tool script is ran by the editor.
At runtime you should get errors in the error log (Debugger > Errors) and the reason it "runs fine" is because since _ready is called after _init, your onreadys put a valid node reference in your variables.

The reason you get an error even when commenting your _init method is because when exporting variables, the setter is called with the exported value at _init time (I believe), that is before the node is ready.
A workaround to this is testing in your setters where you need to access nodes if your instance is_inside_tree and skipping related code if not:

func set_my_thing(new_thing) -> void:
    print(is_inside_tree()) # false once on scene init
    if is_inside_tree():
        $Node.thing() # valid here

func _ready() -> void:
    set_my_thing(my_thing) # workaround: re-set property on _ready (where we know we're in the scene tree)
by (728 points)
selected by

It's worked

Thanks :)

enter image description here

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.