I'm using a custom, simple grid map to position actors.
In the code for the player setup, I set the position to 0, 0, then move it to the real starting spot (because at present my move_to func requires that the current cell and the new cell be different.
I get this error:
Invalid get index 'x' (on base: 'Nil')
On line 40, the first line of move_to()
. This is on the setup call, made from like 16.
The weird thing is that if I hover over the variables in my code while the debugger is running, the proper values are displayed in the tooltip that pops up. It seems the problem is that it's not recognizing the Vector2 variables as Vector2, which is why I tried static typing them, but that didn't help.
I really appreciate any insight.
1 extends Sprite
2
3 var map
4
5 var map_cell: Vector2
6
7 signal player_moved
8
9 # Called when the node enters the scene tree for the first time.
10 func _ready():
11 pass
12
13 func setup():
14 map = get_node("../SimpleMap")
15 map_cell = Vector2(0, 0)
16 move_to(Vector2(7, 7))
17
18 # Input event handler.
19 func _input(event):
20 var new_cell = Vector2(map_cell.x, map_cell.y)
21 if get_parent().player_turn:
22 if event is InputEventKey:
23 if event.is_action_pressed("player_left"):
24 new_cell.x -= 1
25 elif event.is_action_pressed("player_right"):
26 new_cell.x += 1
27 elif event.is_action_pressed("player_up"):
28 new_cell.y -= 1
29 elif event.is_action_pressed("player_down"):
30 new_cell.y += 1
31
32 if move_to(new_cell) > 0:
33 # If we make it here, the player has successfully moved.
34 emit_signal("player_moved")
35
36 func _process(delta):
37 pass
38
39 func move_to(new_pos: Vector2):
40 if new_pos.x == map_cell.x and new_pos.y == map_cell.y:
41 return -1
42 # Can't move the player off the map.
43 elif new_pos.x < 0 or new_pos.x > map.size.x - 1
44 or new_pos.y < 0 or new_pos.y > map.size.y - 1:
44 return -2
45 # Can't move into an enemy.
46 elif map.get_cell(new_pos) != null:
47 return -3
48 else:
49 position.x = map.offset.x + map.get_cell_origin(new_pos).x
50 position.y = map.offset.y + map.get_cell_origin(new_pos).y
51 map.set_cell(map_cell, null)
52 map.set_cell(new_pos, self)
53 map_cell = new_pos
54 return 1