Godot Version
4.3
Question
making a deltarune fangame but im stuck on the room system since i want a clear hiearchy i cant just use chane_scene_from_file/packed() so im trying to do it via a singleton but every time i try to enter a room it returns this error Cannot call method ‘instantiate’ on a null value.
i can show some of my code here
singleton called Single.tscn original i know
extends Node
@onready var state:String
signal battle
# Called when the node enters the scene tree for the first time.$kris, $kris/Camera2D, $Tem$kris, $kris/Camera2D, $Templa$kris, $kris/Camera2D, $Templ$$kriskrisate, $tempenemyte, $tempenemyplate, $tempenemy
func _ready() -> void:
pass
# Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
pass
func teleport(sceneroot,sceneinst:PackedScene,newloc:Vector2,player:CharacterBody2D):
print("wowzers")
print(sceneroot.name)
print(sceneinst)
print(newloc)
#scenedel.queue_free()
var tree = player.get_node("..")
print(tree)
#print(sceneinst.can_instantiate())
var newscne = sceneinst.instantiate()
tree.call_deferred("add_child",newscne)
player.position = newloc
newscne = null
area2d node called area_teleport.tscn
as you can tell by this mess of spaghetti code i have tryed a lot of approaches the deleting of the camera is because of position smoothing and drag
extends Area2D
@onready var timer: Timer = $Timer
@export var newlocation: PackedScene
@export var newlocinscn : Vector2
# Called when the node enters the scene tree for the first time.
# Load the scene as a PackedScene
var scene = preload("res://scenes/camera.tscn") # Replace with your scene path
# Instance the scene
var instance = scene.instantiate()
# Get the node you want to add the scene to
func getcam(searched):
var searchedchilderen = searched.get_children()
for i in searchedchilderen:
if i is Camera2D:
var camera = i
print(i)
return camera
func _ready() -> void:
pass
func _on_body_entered(body: Node2D) -> void:
if body.is_in_group("Player"):
var _nodepathbody = get_path_to(body)
Single.teleport(get_tree().current_scene,newlocation,newlocinscn,body)
get_parent().queue_free()
#var camera = NodePath("body/Camera2D")
#var cameranode = get_node(camera)
#print(body)
##print(camera)
#var camera = getcam(body)
#
##if cameranode is Camera2D:
#camera.queue_free()
#body.global_position = newlocation
#print("deleted")
#var scene = preload("res://scenes/camera.tscn") # Replace with your scene path
## Instance the scene
#var instance = scene.instantiate()
#body.add_child(instance)
#instance.name = "Camera2D"
#print(newlocation)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
pass
player for all that helps (characterbody2d of course)
extends CharacterBody2D
@onready var _animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
@export var SPEED = 100
@export var DARK = false
# Called when the node enters the scene tree for the first time.
func doabattle():
print("wow its a battle scene")
func _ready() -> void:
var _animationstart = _animated_sprite_2d.animation
_animated_sprite_2d.stop()
Single.battle.connect(doabattle)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
var inputvector = Input.get_vector("left","right","up","down")
if Input.is_action_pressed("down"):
_animated_sprite_2d.play("walk south"+"_"+var_to_str(DARK))
elif Input.is_action_pressed("right"):
_animated_sprite_2d.play("walk east"+"_"+var_to_str(DARK))
elif Input.is_action_pressed("left"):
_animated_sprite_2d.play("walk west"+"_"+var_to_str(DARK))
elif Input.is_action_pressed("up"):
_animated_sprite_2d.play("walk north"+"_"+var_to_str(DARK))
else:
_animated_sprite_2d.stop()
if Input.is_action_pressed("deny"):
velocity = inputvector * SPEED * 1.5
else:
velocity = inputvector * SPEED
move_and_slide()
please help im banging my head against a wall like a fly trying to solve this