How do I warp my player character back to where he started in a level?

it’s pretty basic stuff to generate
basically a game will need something like this
World Node (Node) → attach main_script.gd
|_Game2d Node (Node2D)
. |_Level Scene
. |_Player Scene → attach player.gd
. . etc
|_GUI Node (Control/CanvasLayer)
. | _ Player UI (Health)

like explained here:
Node Tree Structure

Ok, so I have a level scene that has a test level, I’ve made a Main node with a new script, so how do I add the code to spaw/despawn the player, and how do I connect the main script to the level?

on main_script.gd, add ’

@export var player_scene: PackedScene

var current_player

save the player as a scene
drag and drop the player.tscn scene to the Player Scene at inspector dock of world scene

to spawn the player, use

var new_player=player_scene.instantiate()
game2d_node.add_child(new_player)
current_player=new_player

to despawn the player, use

game2d_node.remove_child(current_player)
current_player.queue_free()


there’s an error

of course you put the spawn and despawn on a function, example on func _ready():
above are just code snippet, not a whole main script’s code

1 Like

errorparttwo
messed up again :face_with_diagonal_mouth:

yes, because it needs game2d_node reference
currently there is no game2d_node variable

so make the variable and reference the node from scene tree to the variable

1 Like

ok, so should game2d_node be a child node of main node?

if you follow this, then it’s the child of main script’s node

1 Like

also this is incorrect, you are missing the instantiate block code for spawning in _ready function

1 Like

mainnode
here’s what it looks like. player is in the level scene, Idk if I should change that or what. what did I do wrong in the code?

Game2d Node, doesnt have to be renamed that way
just name it Game2d
then in mainscript.gd add variable

var game2d_node=

it will be an error, drag and drop the Game2d Node to after the =
now this you referenced the Game2d node to the mainscript, and can use it

the code should spawn Another player scene in Game2d Node. which mean you can delete the player scene on Level scene now

1 Like


coffeecupnode
*CoffeeCup is name for my player node

forgot about @onready before var game2d_node
so the whole line of it should be
@onready var game2d_node=$game2d

1 Like


I tried testing it but…

you missed this

1 Like

where do I put it?

before the game2d_node.add_child(new_player)

1 Like

funcready
this it?

remove anything inside the ready function and just put this in

var new_player=player_scene.instantiate()
game2d_node.add_child(new_player)
current_player=new_player