I’m currently messing around trying to make a mini game exercise using dice.
I have currently gone ahead and made a simple dice roller script with a points system that tallies all your dice rolls.
What I want to do is make it a flexible and adjustable dice rolling system. Eventually I want to make it be an incremental game where I can adjust the amount of dice and add bonus values to the dice rolled. How could I go about making the change to my current script?
extends Node2D
@onready var points_total: Label = %PointsTotal
@onready var dice_total: Label = %DiceTotal
var Points := 0
var DiceResult := 0
# Sets points and dice total to defaults.
func _ready():
points_total.text = "Damn, you broke..."
dice_total.text = "Roll your first dice"
# Determines the dice result and adds points
func _roll_dice():
var Dice = [1, 2, 3, 4, 5, 6]
DiceResult = Dice.pick_random()
dice_total.text = "You rolled a " + str(DiceResult)
Points += DiceResult
points_total.text = "$" + str(Points)
# Rolls the dice
func _on_button_pressed() -> void:
_roll_dice
You can make it modular and flexible by putting something like n_sides and special effects in a dice class. Then instance whichever dice you want at runtime.
It’s a bit much to break down here. You should look into general OOP; and classes, resources, and instancing in godot. This is a good place to start for a godot implementation. You can look for other tutorials using the keywords in this reply.
The point is, you code the blueprint in a resource, and use that resource to easily create many different dice for you game. A dice resource could look something like:
dice_stats.gd (attached to dice_stats.tres)
extends Resource
# n_sides
@ export var n_sides: int
# bool here, but could be an array or dictionary.
# look into separate tutorials that do inventory systems
# if your multipliers are something you can collect, or enable/disable
@ export var bonus_val: bool # turn on or of special powers
func _init(par_n_sides = 6, par_bonus_val = false): # default values when creating a dice instance
n_sides = par_n_sides
bonus_cal = par_bonus_val
Then if you want to create for instance a d20 without any special abilities, you add a reference to that resource in your script and write:
d20.gd
extends Node2D
# lets you drag in a resource from the inspector
@export var: Resource
func _ready():
if dice_stats: # if there's a reference to the resource
dice_stats.n_sides = 20
This is the best workflow if you want to create tailored content for your game. If instead you’re looking to create dice at runtime, you can create a custom class and instance at runtime like:
dice.gd (attached to the root node of a dice scene)
extends Node2D
class_name Dice
# reveal to inspector with default 6
@export var n_sides: int = 6
# reveal to inspector with default false
@export var bonus_val: bool = false
main.gd
# preload scene
var DICE = "res://scenes/dice.tscn"
# call this whenever you need to instance a dice
func create_dice(n_sides, bonus_val) -> void:
# takes the scene and makes an instance
var new_dice = DICE.instantiate()
new_dice.n_sides = 20 # sets value to 20
add_child(new_dice) # adds the dice instance to the scene tree