Thank you, I appreciate it. I followed your advice and although I have made progress, I’m still struggling to write code that displays changing dice faces from the atlas region to give the illusion that the dice are rolling. Below is the code I have so far:
extends Control
var num_of_columns: int = 2
var num_of_rows: int = 2
#Width of image in atlas
var atlas_x:int = 16
#Height of image in atlas
var atlas_y:int = 16
#Atlas coordinates and x_coordinate range
#1st entry in the array is the max x_coordinate
#2nd entry in the array is the y_coordinate
var dice_atlas_coordinates = {
"D2": [1, 14],
"D3": [2, 13],
"D4": [3, 12],
"D5": [4, 11],
"D6": [5, 10],
"G2": [1, 9],
"G3": [2, 8],
"G4": [3, 7],
"G5": [4, 6],
"G6": [5, 5],
"P2": [1, 4],
"P3": [2, 3],
"P4": [3, 2],
"P5": [4, 1],
"P6": [5, 0],
}
var ghost_dice_bag
var duration: float = 0
@onready var grid_container: GridContainer = $CenterContainer/GridContainer
@onready var texture_rect: TextureRect = $CenterContainer/GridContainer/TextureRect
# Called when the node enters the scene tree for the first time.
func _ready():
ghost_dice_bag = Globals.dice_bag.duplicate()
for i in range(1, num_of_columns * num_of_rows):
grid_container.add_child(texture_rect.duplicate())
place_dice()
func place_dice():
if ghost_dice_bag.is_empty() == true:
#Assigns atlas coordinates that are empty
texture_rect.position.x = 64
texture_rect.position.y = 64
else:
var die = ghost_dice_bag.pick_random()
texture_rect.position.x = dice_atlas_coordinates[die][0] * atlas_x
texture_rect.position.y = dice_atlas_coordinates[die][1] * atlas_y
ghost_dice_bag.erase(die)
func _on_roll_dice_pressed():
while duration < 1.0:
for h in range(1, num_of_columns * num_of_rows):
for i in range(0, Globals.dice_bag.size()):
texture_rect.position.y = dice_atlas_coordinates[Globals.dice_bag[i]][1] * atlas_y
var j = randi_range(0, dice_atlas_coordinates[Globals.dice_bag[i]][0])
texture_rect.position.x = atlas_x * j
duration += 0.1
duration = 0
My Globals are as follows:
#Stores dice that are available for rolling
var dice_bag = [
"P2",
"P2",
]
Even though there are two dice in the dice bag, I still see 4 dice shown on the screen:
