0 votes

I have a fruit-ninja (ish) game that I'm creating where the objects that are flying in the game are generated randomly at the start of the game, and stay consistent until the game is over.

here is my scene tree

In order to achieve this I generate a random number in an autoloaded script at the beginning of the game GetRandomNumber.gd. It is a simple script that creates a random number at the beginning of the game so I can use it to index into a preloaded array of images in the Objects script.

In GetRandomNumber.gd i have the following code

extends Node

func _ready():
    randomize()
    var array_element_number = randi()%7
    print(array_element_number)

However, I am unable to access the variable array_element_number if it is created inside the function _ready. I get the error Invalid get index 'array_element_number' (on base: 'Node (GetRandomNumber.gd)' every single time. I've tried creating my own function, and not create the variable inside _ready():, but I still get a similar error. I've also tried adding a line return array_element_number, which doesn't work either.

The only way i'm able to access it is if it is outside the loop:

extends Node

var array_element_number = randi()%7

func _ready():
    print(array_element_number)

I would be fine with this, except the way that the seed is set, the variable array_element_number will always pick the same number every single time, even if I restart the program. I need a way to have my script get a random number (e.g. array_element_number = 6) at the start of the game, and have that number stay 6 throughout the entire game. However, if I end the game and restart it, I should not consistently see array_element_number = 6. Using the randomize () method inside of the ready(): function would fix this, but I get the error above.

Here is my objects script
Here is my game script. Some functions that don't matter are hidden for brevity.

Am I missing some simple but crucial connection in my tree? Is it because my GetRandomNumber.gd doesn't extend the right Node? I have been trying to figure this out for forever.

Thanks in advance for whats probably a simple answer to the problem

in Engine by (45 points)

1 Answer

0 votes
Best answer

You want this:

extends Node

var array_element_number

func _ready():
    randomize()
    array_element_number = randi()%7

So, define the variable outside the function, but set its value in the function (which lets you call randomize() first).

by (10,932 points)
edited by

oh my gosh I knew that this was going to be an easy fix I was missing. Thank you so much!! That worked!!

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 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 webmaster@godotengine.org with your username.