I'm trying to make it so that first when the game starts, in each Location of Locations the Sprite Dice1 is duplicated as a child. Then, when the RollButton sprite is pressed, each Location's dice is randomly generated and the sprite of each is updated to the one randomly chosen. I know it's a lot to ask, but I'd appreciate any help. I'm quite new to coding with gdscript and have some previous experience with Python so even pointing towards the correct functions and stuff to use would be super helpful!!
#I'm so lost
1 messages · Page 1 of 1 (latest)
Is this going to be some kind of Yahtzee board of sorts?
And I assume "AvailableDices" is supposed to be some kind of template repository?
e.g. if I roll 2, you'll duplicate "Dice2"?
Yes to all :DD
are you ever going to have more than one dice per location?
No, I'll just have all the dices be ones at the start and then for now when you roll, they all roll into different numbers
So theres always 5 dices on the board
No more no less
I'd use a slightly different approach then, that's why I'm asking.
For sure! This is my first project in godot and game making overall so ofc
Something like this:
- Create a new scene based on
AnimatedSprite2D, call itDiceor something like this. - Add your dice images as frames: frame 0 should be an empty image (i.e. no dice or just some icon or something like that), frame 1 is number 1, frame 2 is number 2, etc.
- Attach a script:
var rng = RandomNumberGenerator.new()
func roll():
self.frame = rng.randi_range(1, 6)
return self.frame
- Add instances of this new
Dicenode to your scene. - Your code should then be able to do something like calling
$Dice1.roll()and it should roll a new number.
Doing it this way makes the whole thing encapsulated, so you can more easily reuse it multiple times, there are no dependencies, etc.
Thank you so much!
I did this from memory, try it first, thank me later, if it actually works 😄
This will only make a dice for one location though right?
You could even extend it later, e.g. making it so it calls roll() whenever you click on it, etc.
Once this scene is saved, you can add instances of this new scene just like you've added nodes.
Ahhh I see now
so rather than adding multiple Sprite2D, you'd add Dice
I've done that now, how do I call the roll function now with the RollButton sprite?
you'll need a reference to the node in your tree, something like```swift
@onready var dice1 = $"Dices/Dice1"
And later in your code:```swift
dice1.roll()
In the rollbutton script I detect the press of the sprite like this:
func _input(event):
if event.is_action_pressed("lmb"):
if is_pixel_opaque(get_local_mouse_position()):
How can I do the same in an AnimatedSprite2D (the dice) since the is_pixel_opaque() gives an error?
@lyric kernel
Does it have to be that accurate?
Not really, it's just the way I was shown before and I don't know another way to do it
open your Dice scene
and then add a TextureButton inside, make sure it covers the whole sprite (or where you want it to be clickable)
Done
~~then open the Script attached to the Dice scene and add a signal:
signal pressed()
And add a new function:
func _pressed():
pressed.emit()
```~~
Had to look this one up but that should work.
Oh, wait 🙂
way overcomplicated
and that won't work that way anyway, we didn't add the script to the button after all
So you've added the button and opened the script.
Inside the _ready() function you'd add a line like this instead:
$TextureButton.pressed.connect(self.roll)
```That should do the trick
you might have to adjust the name for the texture button, though.
I'm planning on having you like pressing the dices you want to keep, then when you press the roll button, it will roll all the ones not selected
You could probably just add a property enabled and then use that to control whether the button is enabled.
It's too late for me my brain is fried, I'll continue tomorrow 😅