#Player character not spawning.

34 messages · Page 1 of 1 (latest)

clever ibex
#

I've been learning godot and have been trying to make my own small game. Through watching various tutorials and doing the "Your first 2D game" thing in the documentation, i have been trying to spawn the enemies and player, which it seems like the enemies are spawning, but the player is not despite the code being mostly the same.

@export var player_scene: PackedScene
@export var zombo_scene: PackedScene

# Called when the node enters the scene tree for the first time.
func _ready():
    set_physics_process(false)
    level_init()

func level_init():
    await get_tree().physics_frame
    set_physics_process(true)
    var player = player_scene.instantiate()
    var zombo = zombo_scene.instantiate()
    var zombo2 = zombo_scene.instantiate()
    player.position = $PlayerSpawn.position
    zombo.position = $ZombieSpawn.position
    zombo2.position = $ZomboSpawn.position
    add_child(player)
    add_child(zombo)
    add_child(zombo2)
    
proper lynx
#

Do you get any error messages in the debugger? If you look at the remote scene tree during a run, can you tell if the player node is in the scene? If the player is in the scene, maybe it's not instantiating into a good position?

clever ibex
#

The only errors i get are related to navigation, an error i fixed before but i guess have "un-fixed" 😦
As for the remote scene tree, seems like theres no player in there, the @ CharacterBody says it's an instance of the zombo scene again

plucky wadi
#

Have you tried putting a print statement in the _ready function in player to see if it prints?

you are instantiating the player in the same function as the enemy, meaning unless the enemy is being instantiated somewhere else the player is also being instantiated. Otherwise it should return an error I believe.

Is the player getting deleted right after being instantiated?

#

Could the player accidentally be put as a child of something else in the scene?

clever ibex
#

Theres no code anywhere saying to delete anything, All 3 instances (player and 2 zombies), have different spawn locations which work as the zombies are spawning orrectly, and i just added the print statements in their _ready() functions and they all printed correctly

plucky wadi
clever ibex
#

i just checked the entire remote scene tree and theres no sign of the player though

proper lynx
#

In the script editor, beside var player = player_scene.instantiate(), put a breakpoint and see what's happening.

#

step through line by line in the debugger

#

and maybe move add_child(player) up, so it's immediately following the player instantiate line, so you don't have to step through all the zombie instantiation code while debugging.

clever ibex
#

going through line by line (never done this, im still a relatively new programmer), it looks like the player instantiate isn't picking up the player scene properly. Checking their local properties for the variables, the zombo is appearing in the first image, player in the second one

#

although using my original player scene (i remade it from scratch for testing to see if a new scene fixed it and it didn't), my original player scene when loaded as the packed scene appears more like the zombo one, but it still doesn't appear in game

plucky wadi
#

You could try using the "child_exiting_tree" signal in the same script you are instantiating the player and see if that gives any results.

proper lynx
#

Strange. If I run your code verbatim, I get correct spawning.

clever ibex
#

yeah, i don't see why it doesn't work.

proper lynx
#

is your player falling off the map or teleporting somewhere, or queuing_free, or maybe an animation player is relocating it?

clever ibex
#

I just tried the "child_exiting_tree" thing on the player, and made it print when the player exits the tree

#

for some reason, all 3 are being instantiated and apparently spawning correctly, but the player is getting removed from the tree 4 times?

#

if you wanted to look at all the scripts i have, theres only 3, they are all in here

#

the script for the map, the player and the zombo

clever ibex
proper lynx
#

if health >= 0:
queue_free()

#

that doesn't seem right

clever ibex
#

did i get the angle bracket wrong?

proper lynx
#

Probably

clever ibex
#

somehow i can never remember these brackets correctly

proper lynx
#

That'll be important in game dev.

plucky wadi
clever ibex
#

im sure many people have

#

same kind of error where you have a 1 letter typo or are missing a ;

#

Well i appreciate your help both 🙂

#

Many thanks