0 votes

Greets!

I wanna check a skill from a variable between 0 and 100. What would be the syntax please?

in Engine by (321 points)

1 Answer

0 votes
Best answer

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()).

by (10,327 points)
edited by

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)

Yes, but i got 'the method rolladice isn't declared in the current class'...

That's my code tryin to declare the method rolladice, but then it's "too few argument for onlookpressed()": 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.
    `

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")

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. :)

Ok, managed it at last, with a lil trick, but that's ok. Cheeeeeeeeeers! :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.