#I have a dice roll script but its occasionally skips a process.

1 messages · Page 1 of 1 (latest)

rotund crypt
#

i have a script that i started as a bunch of if and elifs to compare the random integer to then put a specific corresponding animated sprite frame up on the screen. I modified it to use a mondo (%) and sort by integer and got rid of all the if and elif. it works perfectly except about every 5 or 6 clicks of the button it does not generate the random number. then i click again and it works fine. i added a print output to trace the issue but when it fails nothing prints in the output box either. so it looks like it fails to do the randomize and generate the rnd variable. i even changed the range from 1 to 100 to be 0 to 99 and i still got the occasional failure. Script below.

extends Node2D
#preload asa resource the dice animations
@onready var reddice: AnimatedSprite2D = $reddice
@onready var bluedice: AnimatedSprite2D = $bluedice
#set up the dice variables integer as a zero
var die1: = 0
var die2: = 0

func _on_button_pressed():
$reddice.play("reddice")
$bluedice.play("bluedice")
await reddice.animation_finished
await bluedice.animation_finished
randomize()
var rng: = randi_range(1, 100)
print(rng)

var tens: = (rng/10)%10
var ones: = (rng)%10
$reddice.frame = tens
$bluedice.frame = ones
oak gazelle
# rotund crypt i have a script that i started as a bunch of if and elifs to compare the random ...
  1. Those variables aren't preloading as a resource. It's probably best to just use the dollar sign to access them
  2. You don't need to call randomize() for randi_range()
extends Node2D
#set up the dice variables integer as a zero
var die1: = 0
var die2: = 0

func _on_button_pressed():
    $reddice.play("reddice")
    $bluedice.play("bluedice")
    await $reddice.animation_finished
    await $bluedice.animation_finished
    var rng: = randi_range(1, 100)
    print(rng)

    var tens: = (rng/10)%10
    var ones: = (rng)%10
    $reddice.frame = tens
    $bluedice.frame = ones 
#

regarding your issue, it may be a problem with await

#

either that, or it's a conflict with another script

rotund crypt
# oak gazelle 1. Those variables aren't preloading as a resource. It's probably best to just u...

Thanks for looking at this for me. The var die1 and 2 are not in play yet i was setting them up for a future thing. i should coment them out i guess until im ready for them. The only other script i have is the standard top down player movement script with animation changes based on direction button pressed....nothing fancy. So this may be a moote point. i opened it up this am to take another look and when i ran it it ran for 25 flawless dice rolls before i closed it. i wonder if there was some wierdness after cleaning up my code last night and all it took was a closing an opening it up again... strange.

oak gazelle
rotund crypt
# oak gazelle So are you saying that previously the dice animation wouldn't play sometimes?

so the animation always would play. it was first up in the on button pressed call. But the random number didnt always get generated it would most of the time but not always. and i added the print command to verify it in the output window. when it failed the animation would play all the way through to the last frame and there was no number in the output window . when it worked most of the time the animation would play through but then it would go to the corresponding frame of the animation and pause there, also in the output window the random integer would be printed.

rotund crypt
# oak gazelle So are you saying that previously the dice animation wouldn't play sometimes?

the issue came back but i think i have it narrowed down to something breaks in the await reddice.animation_finished. becasue i moved the var rng and the print(rng) above the await commands and i see the number gets generated and then the animations play to the last frame and dont go to the frame that they are suposed to. and not all the time just occasionally. also not on any speciffic numbers i had it fail on 77 and also work perfectly on 77 so im a bit wtf.

oak gazelle
rotund crypt
round forum
oak gazelle
rotund crypt
#

i actually am doing a preload i had removed it as testing . here is the current code i have in for that aspect.

#

extends Node2D
#preload as a resource the dice animations
@onready var reddice: AnimatedSprite2D = $reddice
@onready var bluedice: AnimatedSprite2D = $bluedice
#set up the dice variables integer as a zero