0 votes

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.

in Engine by (12 points)

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

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

hmm... A bug in the engine perhaps?

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

Okay, so now I feel kinda stupid... :P

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!

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.