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.
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])
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.
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”)
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.