Hello, a bit of a stupid question, but apparently I’m not staging the scene correctly

Code:
var inst = preload(“res://elements/battle/battle_scene.tscn”)
var battle = inst.instantiate()
self.add_child(battle)

errors^
E 0:00:01:0343 menu.gd:16 @ on_play_pressed(): res://maps/mount/level1.tscn:29 - Parse Error: [ext_resource] referenced non-existent resource at: res://maps/mount/level1.gd
<Исходный код C++>scene/resources/resource_format_text.cpp:162 @ _parse_ext_resource()
<>menu.gd:16 @ on_play_pressed()
E 0:00:01:0485 menu.gd:16 @ on_play_pressed(): Another resource is loaded from path ‘res://maps/mount/level1.tscn’ (possible cyclic resource inclusion).
<Ошибка C++> Method/function failed.
<Исходный код C++>core/io/resource.cpp:75 @ set_path()
<Трассировка стека>menu.gd:16 @ on_play_pressed()
code with error?
get_tree().change_scene_to_file(“res://maps/mount/level1.tscn”)

Could you show the full script in a code block?

On a new line press the </> button and paste within the three ticks like so
```
type or paste code here
```

enemy code
/

extends CharacterBody2D

@export var speed: int=150
@export var enemy_id: int
var chase= false
var onset=false


func _ready()->void:
	if enemy_id==1:
		signals.connect("defeated",on_defeated)
	if enemy_id==2:
		signals.connect("defeated2",on_defeated)

func on_defeated():
	queue_free()
func test():
	print("worked")
	

func _physics_process(_delta):
	var player=$"../player"
	var direction=(player.position-self.position).normalized()
	if chase==true:
		velocity=direction*speed
	else:
		velocity.x=0
		velocity.y=0
	move_and_slide()


func _on_detector_body_entered(body):
	if body.name =="player":
		chase=true

func _on_detector_body_exited(body):
	if body.name =="player":
		chase=false
		
func _on_battle_detector_body_entered(body):
	if body.name =="player":
		onset=true


func _process(_delta):
	if onset==true:
		globals.circleCount+=1
		if enemy_id==1:
			signals.battle_1.emit()
			get_tree().change_scene_to_file("res://elements/battle/battle_scene.tscn")

level code
/

extends Node2D

@onready var player=$player

func _ready():
	if globals.last_position !=Vector2(0, 0):
		$player.position=globals.last_position+Vector2(0, 5)
	load_room()

func load_room():
	signals.connect("battle_1",load_battle)
func load_battle():
	print("worked")
	var inst = preload("res://elements/battle/battle_scene.tscn")
	var win = inst.instantiate()
	add_child(win) 

func _on_detector_body_entered(body):
	if body.name =="player":
		globals.last_position=$player.position

battle code
/

@export var enemy: Resource=null

var HP_T=0
var HP_E=0
var ANG_E=0
var def = false

func _ready():
	display_text("Бой начался")
	set_health($health_T, state.currentHP_T, state.maxHP_T)
	set_health($health_enemy, enemy.currentHP_E, enemy.maxHP_E)
	
	HP_T=state.currentHP_T
	HP_E=enemy.currentHP_E
	ANG_E=enemy.currentAnger

func display_text(text):
	$battle_cont/HBoxContainer/text/Display.show()
	$battle_cont/HBoxContainer/text/Display/Label.text=text

func set_health(progress_bar, HP, maxHP):
	progress_bar.value= HP
	progress_bar.max_value=maxHP
	progress_bar.get_node("Label").text="HP: %d/%d"%[HP,maxHP]
	
func _on_def_t_pressed():
	def=true
	enemy_turn()
	
func _on_hug_t_pressed():
	HP_E=max(0,HP_E-state.damage_T)
	set_health($health_enemy, HP_E, enemy.maxHP_E)
	enemy_turn()
	if HP_E==0:
		display_text("Вы победили")
		var inst = preload("res://maps/mount/level1.tscn")
		var instance = inst.instantiate()
		add_child(instance) 
		if enemy.ID_E==1:
			signals.defeated.emit()
		elif enemy.ID_E==2:
			signals.defeated2.emit()

func _on_heal_t_pressed():
	if HP_T<state.maxHP_T:
		HP_T=min(state.maxHP_T, HP_T + state.heal_T)
		set_health($health_T, HP_T, state.maxHP_T)
		enemy_turn()
	else:
		display_text("nuh uh")

func enemy_turn():
	if def:
		def=false
		display_text("вы удачно защитились")
	else:
		var random_dam=randi_range(enemy.mindam_E, enemy.damage_E)
		HP_T=max(0,HP_T-random_dam)
		set_health($health_T, HP_T, state.maxHP_T)
		if HP_T==0:
			display_text("вы погибли")
			$battle_cont.visible=false

func _on_talk_pressed():
	ANG_E=max(0,ANG_E-state.talk)
	display_text("враг не хочет поговорить поэтому от вас удрал")
	if ANG_E==0:
		display_text("Вы победили")
		var inst = preload("res://maps/mount/level1.tscn")
		var win = inst.instantiate()
		add_child(win) 
		if enemy.ID_E==1:
			signals.defeated.emit()
		elif enemy.ID_E==2:
			signals.defeated2.emit()
		remove_child(win)

what is this code for? It seems to be loading level1 and removing it instantly

var inst = preload("res://maps/mount/level1.tscn")
var win = inst.instantiate()
add_child(win) 
if enemy.ID_E==1:
	signals.defeated.emit()
elif enemy.ID_E==2:
	signals.defeated2.emit()
remove_child(win)

If you must have level1 instantiated this way, try using load instead of preload.

1 Like

this code to go to level 1


when i use instantiate() for load battle_scene

“res://maps/mount/level1.gd” should probably be “res://maps/mount/level1.tscn”
please check the “menu.gd” file
on the function “on_play_pressed”

its tscn already

In your menu scene you change to your level scene, which preloads your battle scene, which preloads your level scene again, which (as the errors correctly pointed out) is a cyclic resource inclusion. As @gertkeno already pointed out, your inclusion of the level in the battle scene seems superfluous, because you instantiate it, add it to the scene and then remove it in the same frame again without doing anything with it.

1 Like

Hmm, okay, then I’ll try to make the battle scene load on the level 1 scene on the contrary. Thanks

Thanks fot @njamster and @gertkeno

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.