Randomly generate from two diffeeent arrays?

Godot Version

4.7

Question

Hi all. I’m making a little game where you put fruit in a bowl. Fruit and vegetables show up, you have three seconds to choose the right items. I was thinking I’d use two arrays. One fruit(right answers), one vegetable(wrong answers). That way I could have it check and say if player chose from wrong answers lose points and right answers gain points. I’ll have 10 slots where these items can be generated.

Can I have Godot pull from two different arrays at the same time and randomize into these slots? Or is there a better way to organize this? Haven’t written anything yet just trying to picture how to write it.

You mean something like this?

var fruits := [ apple, mango, banana, ... ]
var vegetables := [ salad, cucumber, tomato, ... ]

...

var chosen_fruits := fruits.duplicate()
chosen_fruits.shuffle() # randomize all fruits
chosen_fruits = chosen_fruits.slice(0, 5) # only get 5 fruits

# do the same with vegetables
# ...

# items to spawn
var items := chosen_fruits.duplicate()
items.append_array(chosen_vegetables)
items.shuffle()
for i: int in items.size():
    put_item_into_slot(i, items[i])

So yeah, arrays are a way to go for this.

You can do this, but I’d probably keep the two arrays for defining the categories and combine them into one array when generating the round.

For example, keep fruits and vegetables separate, then create a temporary items array containing the entries you want for that round and shuffle it with items.shuffle(). You can then fill your 10 slots from that shuffled array.

When the player selects an item, you can check which category it belongs to and award or subtract points accordingly. This keeps the generation logic simple and makes it easier to control how many correct and incorrect items appear in each round.

Thanks so much for the quick responses!

I guess I’m getting stuck on even more basics than what I originally asked. Now I can’t get anything to appear for even one array. I was trying to draw out sprites per each item, so I was thinking preloading the scenes for each would work? ‘Shuffled’ isn’t printing so I now think I’m incorrectly trying to call on the make_grid function?

extends Node2D
@export var width: int;
@export var height: int;
@export var x_start: int;
@export var y_start: int;
@export var offset: int;

var fruits = [
preload(“res://scenes/foods/apple.tscn”),
preload(“res://scenes/foods/banana.tscn”),
preload(“res://scenes/foods/pineapple.tscn”),
preload(“res://scenes/foods/watermelon.tscn”),
preload(“res://scenes/foods/tomato.tscn”)
]
var vegetables = [
preload(“res://scenes/foods/cauliflower.tscn”),
preload(“res://scenes/foods/greenbellpepper.tscn”),
preload(“res://scenes/foods/lettuce.tscn”),
preload(“res://scenes/foods/potato.tscn”),
preload(“res://scenes/foods/redbellpeper.tscn”)
]

func make_grid():
for i in width:
fruits.shuffle()
print(“shuffled”)

func _ready() → void:
print(“running”)
make_grid()

Format your code properly.

Here, I did it for him.

extends Node2D
@export var width: int;
@export var height: int;
@export var x_start: int;
@export var y_start: int;
@export var offset: int;

var fruits = [
preload(“res://scenes/foods/apple.tscn”),
preload(“res://scenes/foods/banana.tscn”),
preload(“res://scenes/foods/pineapple.tscn”),
preload(“res://scenes/foods/watermelon.tscn”),
preload(“res://scenes/foods/tomato.tscn”)
]
var vegetables = [
preload(“res://scenes/foods/cauliflower.tscn”),
preload(“res://scenes/foods/greenbellpepper.tscn”),
preload(“res://scenes/foods/lettuce.tscn”),
preload(“res://scenes/foods/potato.tscn”),
preload(“res://scenes/foods/redbellpeper.tscn”)
]

func make_grid():
  for i in width:
    fruits.shuffle()
    print(“shuffled”)

func _ready() → void:
  print(“running”)
  make_grid()

The issue seems to be that you’re iterating over an int. Try for i in range(width)

I did try. I had it formatted and when I hit submit it went back to unformatted.

```
[paste code direcly from editor here]
```

Thank you very much for trying to help. I am pasting directly from Godot to here:

func make_grid():
for i in range(width):
fruits.shuffle()
print(“shuffled”)

func _ready() → void:
print(“running”)
make_grid()

It does not format. For i in range(width): still didn’t make ‘shuffled’ print.

I will see if I can go back to Godot docs and try some simpler things. Thank you all for the help.

You need to paste between three backticks. Or use other formatting methods described here under “Format your code”

Did you assign a value to width in the inspector? If not it will have the default value of 0 and the loop won’t run.

Got it thank you very much! Yup that was it. I had unattached and reattached the script when testing so now at least ‘shuffled’ is printing as it should.

As a next step you should try to replicate what @ratrogue did in their code example.