+1 vote

I need help with a script instances, here the code:

#Script instructions
extends Node
var currentScene = null
var PlayerName = "Mike"

func _ready():
   currentScene = get_tree().get_root().get_child(get_tree().get_root().get_child_count() -1)
   Globals.set("MAX_POWER_LEVEL",9000)

func setScene(scene):
   currentScene.queue_free()
   var s = ResourceLoader.load(scene)  #ERROR HERE ERROR HERE
   currentScene = s.instance()
   get_tree().get_root().add_child(currentScene)

func getPlayerName():
   return PlayerName

# Script call
extends Node

func _ready():
    set_process(true)

func _process(delta):
    if(Input.is_mouse_button_pressed(BUTTON_LEFT)):
        get_node("/root/global").setScene("res://Scenes/in_game_bot")

Edited by moderator: Please use code formatting (see the "{ }" button).

in Engine by (18 points)
recategorized by

Did you double check that this path and filename is correct? "res://Scenes/ingamebot"

am getting the same error but on the s.instance. Please help.

Attempt to call function 'setScene' in base 'null instance' on a null instance. (Error)
showing this error at :

getnode("/root/global").setScene("res://SecondScene.scn")
and i also tried, get
node("/root/global").setScene("res://SecondScene.tscn")

1 Answer

+1 vote
Best answer

Can you please try to add file extension on your res:// path: get_node("/root/global").set_scene("res://Scenes/in_game_bot.EXTENSION") (like .xml, .scn etc) and tell us if it helps? :)

You are sending file path (from "res://" space), not node path.

Example: get_node("/root/global").set_scene("res://Scenes/in_game_bot.scn")

Btw, i think you was going thru this tutorial on changing scene. That guy is using get_node("/root/globals").set_scene("res://Scenes/game.scn") - first is node path, second file path.

by (233 points)
selected by

worked , thanks man, but I wanted to do to change screen When you press a "button" in the first scene , I tried to use " getnode (node ​​) .ispressed ()" but did not work

Look for connect, actually it is explained in Part 5, section "Connecting Code to your Control".

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.