Game Crashes When Spawning Player at Campfire Tile

Godot Version

4.2

Question


Hi everyone,

I’m developing a game similar to Loop Hero, and I’m encountering an issue with my Godot script. The map generation works perfectly, and the campfire and paths are being generated randomly as expected. However, the problem arises when trying to spawn the character at the campfire position. Here is the script I’m using:

func find_campfire_and_spawn_player():
    print("Starting find_campfire_and_spawn_player")
    for x in range(grid_width):
        for y in range(grid_height):
            print("Checking cell at: ", x, y)
            var cell_id = campfire_tilemap.get_cell_source_id(Vector2i(x, y), 0)
            print("Cell ID at ", x, y, ": ", cell_id)
            if cell_id == CAMPFIRE_TILE_ID:
                var campfire_position = campfire_tilemap.map_to_world(Vector2i(x, y))
                print("Campfire position found at: ", campfire_position)
                
                var player_instance = PlayerScene.instantiate()
                player_instance.position = campfire_position
                add_child(player_instance)
                
                print("Player spawned at campfire position: ", campfire_position)
                return
    print("Campfire tile not found.")

The game sometimes crashes when the script tries to spawn the character. I’ve tried different variations such as using get_cell but nothing seems to work. Could anyone help me troubleshoot this or provide some insights on what might be going wrong?

Thanks in advance for your help!

There’s too little information here. What do you mean by crash? It just closes your Godot game and returns to the desktop? Is there any error?
What is the script attached to? When add_child is called, where is it added to? Are you calling this from the main thread?
Your function looks fine. I can see an issue where something bad happens when the player is not spawned if the campfire tile is not found but that’s it.

1 Like

ok so i will try to send you the code with the function ready and this function. this is currently a modified version, but anyway, after the map is generated for me, which is randomly generated and works flawlessly, the function find campfire and spawn player, so at that moment when I start the project F5, the game crashes as if it freezes

extends Node2D

# Grid size
var grid_width = 20
var grid_height = 20

# Tile IDs
const PATH_TILE_ID = 0
const CAMPFIRE_TILE_ID = 1

@onready var path_tilemap = $PathTileMap
@onready var campfire_tilemap = $PathTileMap

# Path to the player scene
@export var PlayerScene: PackedScene

func _ready():
    # Initializes the generator and starts loop generation
    print("Starting generation...")
    randomize()
    await generate_loop_path()
    print("Generation completed.")
    
    # Add timer to spawn player after 2 seconds
    var timer = Timer.new()
    timer.wait_time = 2.0
    timer.one_shot = true
    timer.connect("timeout", Callable(self, "find_campfire_and_spawn_player"))
    add_child(timer)
    timer.start()

func find_campfire_and_spawn_player():
    print("Starting find_campfire_and_spawn_player")
    
    # Get all positions of tiles with the campfire ID (CAMPFIRE_TILE_ID) in layer 0
    var campfire_positions = campfire_tilemap.get_used_cells_by_id(0, CAMPFIRE_TILE_ID)
    print("Found campfire positions: ", campfire_positions)
    
    # If at least one position is found, use the first one
    if campfire_positions.size() > 0:
        # Convert the coordinates to world coordinates
        var campfire_position = campfire_tilemap.map_to_world(campfire_positions[0])
        print("Campfire position found at: ", campfire_position)
        
        # Create an instance of the player and set its position to the campfire position
        var player_instance = PlayerScene.instantiate()
        player_instance.position = campfire_position
        add_child(player_instance)
        
        print("Player spawned at campfire position: ", campfire_position)
    else:
        print("Campfire tile not found.")


image

and i got many issue with that, sometimes when i try to use get cell its same,

I’m thinking that maybe I have a mistake in that the scene on the player is via node2D, should I make the scene like something else

ok here is little update

get_used_cells_by_id
local_to_maplocal_to_map

i used these and smth happend

it found my possition of campfire but my character was spawned on 0;0 possition

map_to_world is from godot 3.5. Something is wrong in your local_to_map call.
You can try using map_to_local instead of local_to_map.

1 Like


omg man, thanks so much it WORKS !!

1 Like

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