Any help to make random result?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Syl

Greets!

I’m tinkering to get random enconters through the roll_a_dice function, and i managed that so far:

extends Node2D

func _ready():
	randomize()

func _physics_process(_delta):
	if Input.is_action_pressed("ui_right"):
		_on_action_pressed()

func _on_action_pressed():
	roll_a_dice(1, 100)

func roll_a_dice(minimum, maximum):
	var roll = randi() % (maximum-minimum+1) + minimum
	print(roll)
	if roll <= 50:
		print("ok")
	if roll > 50 and roll <= 75:
		print("nope")
	if roll > 75 and roll < 100:
		print("okokok")

It works almost fine, but for the fact that i got unlimited serie of printed roll and result in the output. What should i add or remove to get only one?

Cheers!

:bust_in_silhouette: Reply From: jgodfrey

So, I guess you’re getting “roll” output as long as you hold the key down, right? Assuming you want only 1 “roll” per keypress, change this:

if Input.is_action_pressed("ui_right"):

to this:

if Input.is_action_just_pressed("ui_right"):

That’ll only fire your code once, in the frame where the key was first pressed. To fire it again, you’ll have to release the key and press it again.

That’s it! Cheers!

Syl | 2020-02-15 22:03