0 votes

I've been making a simple grid-based movement just for a little project and for some reason no matter how I use maptoworld or worldtomap it keeps giving me this error whenever I give any input. Here is my code:

     ##All extensions and imports that the script needs:
extends Sprite
onready var map = get_node("TileMap")
##All Exported Variables
export var TileSize = 16 ##This is the size of the tileset.
export var MoveAllowance = .25 ## this is the time that the player must wait before the next move is taken.

##All Internal Variables:
var timeDelay = 0
var mapGoal = Vector2(0,0)
var pos = Vector2(0,0) ##This will be where the player is.
var goalPos = Vector2(0,0) ## This is where the player wants to be.

##This will happen Only at the beginning of the game.
func _ready():
    self.position = Vector2(8,0) ## this is an alignment thing. for some reason if it isn't started with (+8,+0) it doesn't line up.

##This will happen at every tick of the game.
func _process(delta):
    pos = self.position
    if timeDelay == 0:
        if goalPos.x != 0 || goalPos.y != 0:
            self.position = map.map_to_world(goalPos)
            goalPos.x = 0
            goalPos.y = 0
            timeDelay = 1 ##this can be edited to determine how long the player must wait before moving.
        else:
            getInput()
            timeDelay = 0
    else: 
        timeDelay += -MoveAllowance*delta

##getInput Simply loads the next input into the _process function for processing.
func getInput():
    if goalPos.x == 0:
        if Input.is_action_pressed("ui_right"):
            goalPos.x += 1
        elif Input.is_action_pressed("ui_left"): 
            goalPos.x += -1
    if goalPos.y == 0:
        if Input.is_action_pressed("ui_up"):
            goalPos.y += -1
        elif Input.is_action_pressed("ui_down"):

Sorry about how messy it is, I just recently got back into coding. Any help would be appreciated

in Engine by (92 points)

1 Answer

0 votes
Best answer

Godot is telling you the map variable is null. So method calls on it result in this error.
Check the console for errors that happened before that.

onready var map = get_node("TileMap")

I suspect get_node could not find "TileMap". Nodes are accessed by path, relative to the calling node. If the TileMap is actually parent of your sprite, use get_node(".."), or get_parent().

by (28,738 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.