Set index 'position' (on base: 'Nil')

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

I’m trying to use a grid to generate custom panel nodes. My Grid has the following function:

func spawn_panels(charType):
  var panelInit = null
  var panel = null
  if charType == PLAYER:
	  panelInit = AllyPanel
  elif charType == ENEMY:
	  panelInit = EnemyPanel

  for x in grid_s_x:
	  for y in grid_s_y:
		  panel = panelInit._init(x, y)
		  panel.position = map_to_world(Vector2(x, y)) + cell_size / 2

AllyPanel and EnemyPanel extends CusPanel, which in turn extends Area2D

When spawn_panels is ran, I get the following error:

Invalid set index 'position' (on base: 'Nil') with value of type 'Vector2'.

I’m quite new to the engine, so if anyone can point me to what I did wrong I’d appreciate it. Thanks!

The error is because panel is null, you have setted it as null, but then you do a misterious panel = panelInit._init(x, y) I guess trying to give it a value. What does it do?

hinasis | 2020-09-06 08:47

:bust_in_silhouette: Reply From: jgodfrey

Based on the error, it sounds like panel is null. You should verify that it contains the expected reference.

I was able to get pass the reference by calling new() instead; thanks!

Conanap | 2020-09-06 02:34