Nil error when trying to move Sprite

Godot Version

4.3

Question

I´ve been trying to code an starting map to check my Sprites movement.
Got a node structure like this:

Game
-Map
–EventHandler

For now the basic idea is that the game will take a Sprite 2D Character I made and an TileMapLayer I created, instantiate them as child nodes of the Map node and then let me move the character around with the arrow keys, The main problem happens when I try to move the character. This is the relevant code:

class_name Game
extends Node2D

const area_scene := preload("res://src/Entities/Areas/area1.tscn")
const character_scene := preload("res://src/Entities/Actors/character.tscn")

@onready var area: Area
@onready var map: Node2D = $Map

func _ready() -> void:
	area = area_scene.instantiate()
	map.add_child(area)
	map.read_area(area)
	var character: Sprite2D = character_scene.instantiate()
	map.add_child(character)
	map.generate_character(character)

class_name Map
extends Node2D

const area_scene := preload("res://src/Entities/Areas/area1.tscn")

@onready var character: Sprite2D 
@onready var event_handler: EventHandler = $EventHandler
@onready var entities: Node2D = $Entities
var map_data: MapData
var character_grid_pos: Vector2i
var tiles: Array[Vector2i]

func _process(delta: float) -> void:
	var action: Action = event_handler.get_action()
	if action is MovementAction:
		character_grid_pos += action.offset
		character.position = Grid.grid_to_world(character_grid_pos)
	elif action is EscapeAction:
		get_tree().quit()

func read_area(area: Area) -> void:
	tiles = area.tiles_list
	character_grid_pos = area.initial_position

func generate_character(chara: Sprite2D) -> void:
	chara.position = Grid.grid_to_world(character_grid_pos)

class_name Grid
extends Object

const tile_size = Vector2i(16, 16)


static  func grid_to_world(grid_pos: Vector2i) -> Vector2i:
	var world_pos: Vector2i = grid_pos * tile_size
	return world_pos


static func world_to_grid(world_pos: Vector2i) -> Vector2i:
	var grid_pos: Vector2i = world_pos / tile_size
	return grid_pos
class_name MovementAction
extends Action

var offset: Vector2i

func _init(dx: int, dy: int) -> void:
	offset = Vector2i(dx, dy)

I think you never setup this, so give it a value in the generate_character function like this:

func generate_character(chara: Sprite2D) -> void:
    character = chara
	chara.position = Grid.grid_to_world(character_grid_pos)
1 Like

Well I´ll be damned, that solved it perfectly! Thanks a lot man, mind if I pick your brain about why that happened? I don’t think I understand exactly what the problem was and how that single line solved it.

1 Like

Actually you forgot to set the value of this character, defaultly it is null, “Sprite2D” is a class name only. And I observed that you are changing its position in the process function, but it is null. So you need to set the “character” to real player (that character of sprite 2d).

You already passed the chara (character or player), just needed to give it to character, I means just need to set the character to chara, as character = chara. So its did not null anymore.

2 Likes

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