Get variable from child error

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zombieakopaluetze

Hi there,

i have this simple scene structure, where I try to run a function from my parent, based on the input direction on the child and return the next possible cell position:


On the Karre (simple 2D-Sprite):

extends Sprite

func _ready():

func _process(delta):
	var direction = Vector2(0,0)	
	if Input.is_action_pressed("up"):
		direction += Vector2(0, -1)
	elif Input.is_action_pressed("down"):
		direction += Vector2(0, 1)
	if Input.is_action_pressed("left"):
		direction += Vector2(-1, 0)
	elif Input.is_action_pressed("right"):
		direction += Vector2(1, 0)	

And on the TileMap:

extends TileMap

var tile_size = get_cell_size()
var half_tile_size = tile_size/2
var grid_arr = []
var grid_size = Vector2(10,5)

func _ready():
	for x in grid_size.x:
		for y in grid_size.y:
func move_car(child_node):
	var grid_pos_ch = world_to_map(child_node.get_position())
	var debug = grid_pos_ch + child_node.direction
	var new_grid_pos_ch = map_to_world(debug) + half_tile_size
	return new_grid_pos_ch

This gives me the error:
Invalid get index ‘direction’ (on base: ‘Sprite (’)
and I dont understand it, shouldn’t it read the variable and return it in the function? I just can’t access the *.direction variable, always gives me errors.

var debug = child_node.direction
in my function on the parent doesn’t work. why?

What am I doing wrong or how is this supposed to work?


:bust_in_silhouette: Reply From: kidscancode

This is because of variable scope. Because you declare direction in the _process() function, it only exists in that function. If you want a property that can be accessed externally (and by all class functions), you need to declare it as a class variable outside the class functions:

extends Sprite

var direction  # declare class variables here

func _process(delta):
    direction = Vector2(0,0) 

ah I see, thanks for the clarification. I did that and the new error is:
"Invalid operands ““Vector2” and “Nil” in operator “+”.”

So even when I run the function “move_car” in the “_process()” The function only gets access to the state of the variable in the global scope, right? Thats why it is “Nil” and not the actual value of the running game. Am I right?

What I did now, and what seems to work is:
The new “move_car”:

func move_car(child_node, dir):
	var grid_pos_ch = world_to_map(child_node.get_position())
	var new_grid_pos_ch = map_to_world(grid_pos_ch + dir) + half_tile_size
	return new_grid_pos_ch

In the “_process()” on the

set_position(get_parent().move_car(self, direction))

And so I pass the direction on every Frame to the function.

Zombieakopaluetze | 2018-08-30 07:12

No - ‘nil’ means the variable exists but has no value assigned. When you first declare it using var direction its value is null until it has been set. You can give it an initial value at declaration time.

kidscancode | 2018-08-30 15:44