#where to put timer node settings? it does not work

1 messages · Page 1 of 1 (latest)

wispy hemlock
#
var is_ready: bool = true

func _ready():
    buy.button_down.connect(_on_buy_down)
    timer.timeout.connect(_on_timer_timeout)
    
func get_rarity():
    if buy.pressed and is_ready:
        is_ready = false
        $Timer.start()
    rng.randomize()
    
    var weighted_sum = 0
    
    
    for rarity in rarities.keys():
        weighted_sum += rarities[rarity]["weight"]
    
    item = rng.randi_range(0, weighted_sum)
    
    for rarity in rarities.keys():
        item -= rarities[rarity]["weight"]
        if item < 0:  
            rarities[rarity]["count"] += 1  
            return rarity




func _on_buy_down():
    
    var rarity = get_rarity()
    congrats.text = rarity + ", " + str(rarities[rarity]["weight"] / 10) + "% chance!"  # Display chance
    congrats.modulate = rarities[rarity]["color"]  # set the color based on rarity
    

    # update the text
    match rarity:
        "Common":
            common_fish.text = str(rarities[rarity]["count"])
        "Uncommon":
            uncommon_fish.text = str(rarities[rarity]["count"])
        "Rare":
            rare_fish.text = str(rarities[rarity]["count"])
        "Very Rare":
            veryrare_fish.text = str(rarities[rarity]["count"])
        "Legendary":
            legendary_fish.text = str(rarities[rarity]["count"])
        "Elite":
            elite_fish.text = str(rarities[rarity]["count"])
        "Mythical":
            mythical_fish.text = str(rarities[rarity]["count"])

func _on_timer_timeout():
    is_ready = true
#

what i want is the button (buy) have a cooldown of 1 sec
but it doesnt work
sometimes its automatic, or not 1 second for some reason

remote pebble
#

when is_ready is false and the button is clicked, you don't refuse anything?

#

intuitively I'd put something like

if not is_ready:
    return
#

at the top of func _on_buy_down():

#

also pressed signal is more suitable for button presses rather than button_down signal

#

I'd also move this logic:

    if buy.pressed and is_ready:
        is_ready = false
        $Timer.start()

into _on_buy_down():

#

doesn't really have anything to do with rarity

#

and the buy.pressed should then probably be omitted as that logic runs on button presses in the first place

#

so in summary:

func _on_buy_down():
    if not is_ready:
        return

    is_ready = false
    $Timer.start()

   ...
wispy hemlock
#

thanjs it worked but

wispy hemlock
remote pebble
# wispy hemlock what did you mean by this

well when the button is not ready yet, then clicking it should do nothing right?
so you need to do something like return to prevent the proceeding logic from running.

wispy hemlock
#

so the 'not' is just what it means

#

no programming trickery

remote pebble
#
    if not is_ready:
        return

this means "if is_ready is not true, then stop (return) here"

wispy hemlock
#

so click the button, is_ready becomes false which starts a 1 second timer?