![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Syl |
Greets!
I wanna check a skill from a variable between 0 and 100. What would be the syntax please?
![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Syl |
Greets!
I wanna check a skill from a variable between 0 and 100. What would be the syntax please?
![]() |
Reply From: | njamster |
Assuming that both min
and max
are positive integers (like in your example):
func roll_a_dice(minimum, maximum):
var roll = randi() % (maximum-minimum+1) + minimum
return roll
To change the seed used by the random number generator, make sure you call randomize()
when starting the game (usually done by including it in _ready()
).
Thxs, but i’m expected an identifier for an argument on the first line when copyin that script. Put 0 and 100, but that doesn’t make it.
Syl | 2020-02-10 16:56
My bad, min()
and max()
are built_in functions, which is why they cannot be used as a variable identifier. Didn’t test it before posting. Edited my answer, should work now.
njamster | 2020-02-10 17:01
Thxs, how could i print the roll, see if that works?
Syl | 2020-02-10 17:17
This will roll a D100 for five times and print the result:
func _ready():
randomize()
var roll = -1
for i in range(5):
roll = roll_a_dice(0, 100)
print(roll)
Of course you could add the print-statement inside the roll_a_dice
-function as well:
func roll_a_dice(minimum, maximum):
var roll = randi() % (maximum-minimum+1) + minimum
print(roll)
return roll
njamster | 2020-02-10 17:39
Look, here is my code:
func _ready():
randomize()
func roll_a_dice(minimum, maximum):
var roll = randi() % maximum + minimum
print(roll)
if roll <= GlobalP.look:
print("ok")
else:
print("nope")
but nothin is printed, without errors…
Syl | 2020-02-10 18:00
You forgot to call the function, e.g. in ready()
:
func _ready():
randomize()
roll_a_dice(0, 100)
njamster | 2020-02-10 18:04
Here we are! Cheeeers!
Erh… Would you know why the console doesn’t print my GlobaPlook sometimes? It’s the average of two global stats, and i change them to check the result, sometime it’s printed, sometime not. I’ve added this line:
print(GlobalP.look)
Syl | 2020-02-10 19:12
Or just, how would you put the roll_a_dice function under another function of button_pressed?
Syl | 2020-02-10 20:45
Not sure, if I can follow. You mean, how to execute roll_a_dice()
each time a button is pressed?
njamster | 2020-02-11 09:06
Yes, look my code:
func _ready():
randomize()
roll_a_dice(1, 100)
func _on_Look_pressed():
get_node("AnimationPlayer").play("look")
func roll_a_dice(minimum,maximum):
var roll = randi() % maximum + minimum
print(roll)
if roll <= GlobalP.look:
print("ok")
print(GlobalP.look)
else:
print("nope")
func _on_AnimationPlayer_animation_finished(anim_name):
pass # Replace with function body.
I’d like the func roll_a_dice to be in the func _on_look_pressed, bein activated only when the button is pressed.
Syl | 2020-02-11 10:16
Like this? (“ui_accept” is by default mapped to the Space-key)
func _physics_process(delta):
if Input.is_action_just_pressed("ui_accept"):
_on_Look_pressed()
func _on_Look_pressed():
get_node("AnimationPlayer").play("look")
roll_a_dice(1, 100)
njamster | 2020-02-11 11:09
Yes, but i got ‘the method roll_a_dice isn’t declared in the current class’…
That’s my code tryin to declare the method roll_a_dice, but then it’s “too few argument for on_look_pressed()”: expected at least one…
extends Button
func _ready():
randomize()
func _physics_process(delta):
if Input.is_action_just_pressed("ui_accept"):
_on_Look_pressed()
func _on_Look_pressed(roll_a_dice):
get_node("AnimationPlayer").play("look")
roll_a_dice(1, 100)
var roll = randi() % maximum + minimum
print(roll)
if roll <= GlobalP.look:
print("ok")
print(GlobalP.look)
else:
print("nope")
func _on_AnimationPlayer_animation_finished(anim_name):
pass # Replace with function body.
`
Syl | 2020-02-11 12:41
Well the error says it all: Your declaration of _on_Look_pressed
expects an argument roll_a_dice
. But you don’t pass an argument, thus the error.
func _ready():
randomize()
func _physics_process(delta):
if Input.is_action_just_pressed("ui_accept"):
_on_Look_pressed()
func _on_Look_pressed():
get_node("AnimationPlayer").play("look")
roll_a_dice(1, 100)
func roll_a_dice(minimum, maximum)
var roll = randi() % (maximum-minimum+1) + minimum
print(roll)
if roll <= GlobalP.look:
print("ok")
print(GlobalP.look)
else:
print("nope")
njamster | 2020-02-11 15:15
Also, please note that I edited the formula used to calculate the roll slightly. This way it’s more general and applicable to other scenarios than just a D100.
njamster | 2020-02-11 15:19
Ok, managed it at last, with a lil trick, but that’s ok. Cheeeeeeeeeers!
Syl | 2020-02-11 18:23