• Godot Help
  • failure to create dynamic scene through tile_map.set_cell()

Hi everyone, this is my 3rd day coding in Godot and playing with the game engine! 😃 Nice to meet everyone. I am also following Brackeyes on Youtube for a tutorial walkthrough on this game engine. Can I kindly get someones help on this problem.. please do take a look! Thank you!

Below is my code, and I am unable to properly send my player to this scene.

'levelgenerator.gd'
extends Node2D

var tile_scene = preload("res://scenes/TileMap.tscn")
var evil_blue_slime_scene = preload("res://scenes/evil_blue_slime.tscn")
var evil_pink_slime_scene = preload("res://scenes/evil_pink_slime.tscn")

var tile_mapping = {
"grass": Vector2i(0, 0),
"rock": Vector2i(1, 0),
"desert": Vector2i(2, 0),
"desert_rock": Vector2i(3, 0),
"sand_rock": Vector2i(4, 0),
"rock_2": Vector2i(5, 0),
"snow_rock": Vector2i(6, 0),
"snow": Vector2i(7, 0),
"stone": Vector2i(8, 0),
"bridge_start": Vector2i(9, 0),
"bridge_middle": Vector2i(10, 0),
"bridge_end": Vector2i(11, 0),
"tree_head": Vector2i(2, 4),
"tree_trunk": Vector2i(3, 7),
"mushroom": Vector2i(7, 5),
}

func _ready():

var dynamic_level = generate_level()

add_child(dynamic_level)

func generate_level(level_data):
var dynamic_level = Node2D.new()


# Instantiate the TileMap scene
var tile_map = tile_scene.instantiate()
dynamic_level.add_child(tile_map)

# Hardcoded flat plane of grass tiles
var grass_tile_type = "grass"
var grass_atlas_coords = tile_mapping[grass_tile_type]

for y in range(10):  # Number of rows
    for x in range(10):  # Number of columns
        var tile_position = Vector2i(x, y)
        tile_map.set_cell(tile_position, 0, grass_atlas_coords)
# print(tile_map)

# Create a player spawn point on the first tile
var player_spawn = Node2D.new()
player_spawn.name = "player_spawn"
player_spawn.position = tile_map.map_to_local(Vector2i(0, 0))  # Spawn on the first tile
dynamic_level.add_child(player_spawn)

return dynamic_level

'npc_teleporter.gd'
func _on_request_completed(result, response_code, headers, body):


    var level_generator = load("res://scripts/levelgenerator.gd").new()
    var dynamic_level = level_generator.generate_level(level_data)
    

    var packed_scene = PackedScene.new()
    packed_scene.pack(dynamic_level)
    print_packed_scene_content(packed_scene)
    
    get_tree().change_scene_to_packed(packed_scene)

so logically i should be able to send my player from 1 scene to another, from the change_scene_to_packed built in function. However, while my character was teleported, it is not spawned on the tilesets. When I print the packed_scene_content, using instantiate() , it shows that I only have a null Node2d, why is this so? My guess is that tilemaplayers does not have children object, but it should at least have a tilemaplayer asset when i hardcoded the generation. or is it that my atlas tileset is not properly registered?

Packed Scene Content:
Node:
Type: Node2D
Properties:
Node: <null>
_import_path:
name:
unique_name_in_owner: false
scene_file_path:
owner: <Object#null>
multiplayer: <Object#null>
Process: <null>
process_mode: 0
process_priority: 0
process_physics_priority: 0
Thread Group: <null>
process_thread_group: 0
process_thread_group_order: 0
process_thread_messages: 0
Physics Interpolation: <null>
physics_interpolation_mode: 0
Auto Translate: <null>
auto_translate_mode: 0
Editor Description: <null>
editor_description:
CanvasItem: <null>
Visibility: <null>
visible: true
modulate: (1, 1, 1, 1)
self_modulate: (1, 1, 1, 1)
show_behind_parent: false
top_level: false
clip_children: 0
light_mask: 1
visibility_layer: 1
Ordering: <null>
z_index: 0
z_as_relative: true
y_sort_enabled: false
Texture: <null>
texture_filter: 0
texture_repeat: 0
Material: <null>
material: <Object#null>
use_parent_material: false
Node2D: <null>
Transform: <null>
position: (0, 0)
rotation: 0
rotation_degrees: 0
scale: (1, 1)
skew: 0
transform: [X: (1, 0), Y: (0, 1), O: (0, 0)]
global_position: (0, 0)
global_rotation: 0
global_rotation_degrees: 0
global_scale: (1, 1)
global_skew: 0
global_transform: [X: (1, 0), Y: (0, 1), O: (0, 0)]
script: <null>
Children: