Empty array read as 'Nil' by _process function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By thegoodcaptain13
:warning: Old Version Published before Godot 3 was released.

In my script I have an array (plan) that is initialized as an empty array. Then, in my “_process” function, I make a call to plan.size() that’s required for later code. The problem is, when I play the project I get an error “Invalid call. Nonexistent function ‘size’ in base ‘Nil’.
I can “trick” it into compiling properly if I initialize the array inside the _process function, but I don’t want the array to be re-set to empty every time it updates.

Here is the code I’m using:

extends Node2D
var state = "idle"
var plan = []  #this is the array

func _ready():
	set_process(true)

func _process(delta):
	#if I initialize the array here it will compile
	if (state == "idle"):
		if (plan.size() > 0):  #this is the line that causes the error
			etc...

Is there a way to fix this? Thanks.

codes seems fine to me. I can’t tell what causes problem with this codes.

volzhs | 2016-11-20 15:41

I second that. The array will be initialized before ready starts the process, so it isn’t nil in any of my projects.

avencherus | 2016-11-20 17:00

hmm… A bug in the engine perhaps?

thegoodcaptain13 | 2016-11-21 18:53

I think not…
Did you test just that simple code?
Or can you make a sample project to reproduce it?

volzhs | 2016-11-21 20:27

Okay, so now I feel kinda stupid… :stuck_out_tongue:

After acting on volzhs’ suggestion to isolate the code, I found that this code itself was not causing the problem; this is simply where the problem was being caught.
Further down in my script their was an incomplete function that was assigning a nul value to “plan”, thus causing the “size()” function to throw the error. What I had not realized before was that my game was actually completing one full step before crashing (thus, resetting the “plan” array each step would prevent the error.

Anyway, thanks. The problem is fixed!

thegoodcaptain13 | 2016-11-22 15:03