Godot-4.3
Heya, I’m new here and am trying to learn Godot from scratch. I’ve watched and followed a couple of videos on GDScript but am still trying to wrap my head around some things. I’ve made a very rough and dry Dice Rolling script attached to a scene that I want to use across multiple other scenes of a game I am making that will use the dice for different mechanics. But that is where my first issue has come about. In a puzzle scene, I’m trying to reference a function in the dice script where the number is decided and use that number to determine whether the puzzle is solved or not. The rolling works fine for testing at least but I cant seem to get the puzzle script to receive that number. Here are the 2 scripts written so far:
Dice
extends Node2D
#Sets the number of dice rolled
@export var dice_amount: int = 1
@onready var dice_result: AnimatedSprite2D = $DiceResult
#Rolls the set number of dice
func _on_button_pressed() -> void:
for n in dice_amount:
var roll = randi_range(1, 6)
print(roll) #For Dubbuging
#Changes dice sprite to match number
if roll == 1:
$DiceResult.play("roll1")
elif roll == 2:
$DiceResult.play("roll2")
elif roll == 3:
$DiceResult.play("roll3")
elif roll == 4:
$DiceResult.play("roll4")
elif roll == 5:
$DiceResult.play("roll5")
elif roll == 6:
$DiceResult.play("Roll6")
Puzzle
extends Node2D
#Sets puzzle difficulty
@export var difficulty: int = 1
@onready var difficulty_label: Label = $DifficultyLabel
@onready var outcome: Label = $Outcome
#Sets the text to match the difficulty
func _ready() -> void:
if difficulty == 1:
difficulty_label.text = "You need " + str(difficulty) + " pass to succeed"
else:
difficulty_label.text = "You need " + str(difficulty) + " passes to succeed"
#Determines if player passed or failed
func _attempt():
var Pass = false
if $Dice.roll() >= 5:
Pass = true
outcome.text = "You have passed"
else:
Pass = false
outcome.text = "You have failed"