0 votes

So I'm trying to make this game of chance and the function to put the number on the board and I'm having trouble to set the text as a number (Its a 3D game btw). I'm using a 3D text plugin to get some text on the board.


extends StaticBody

onready var text1 = get_node("board/MeshInstance2/Label3D")
onready var text2 = get_node("board/MeshInstance3/Label3D2")
onready var text3 = get_node("board/MeshInstance4/Label3D3")
onready var text4 = get_node("board/MeshInstance5/Label3D4")
onready var rolled_num1 = RandomNumberGenerator.new()
onready var rolled_num2 = int(rolled_num1)

func _ready():

func _input(event):
    if event is InputEventMouseButton:
            text1.text = int(rolled_num2)
        text2.text = int(rolled_num2)
        text3.text = int(rolled_num2)
        text4.text = int(rolled_num2)
in Engine by (64 points)
edited by

Post edited to fix code formatting. In future, please use the "Code Sample" button to format your code properly.

Thanks! I tried but didn't know how..... :)

2 Answers

+2 votes
Best answer

Looks like you have a number of issues going on here...

First, this:

onready var rollednum1 = RandomNumberGenerator.new()

That does not return a random number as you seem to think. Instead, it returns an instance of a random number generator. That's an object that can generate random numbers...

So, I'd store that in a more descriptive variable. Something like:

onready var rng = RandomNumberGenerator.new()

You probably want to call randomize once on the new rng object. So, this:

func  _ready():

Now, you can use your new random number generator object to generate random numbers whenever you want like this:

var rand_int_1 = rng.randi_range(0, 10) # random int between 0 and 10
var rand_int_2 = rng.randi_range(5, 100) # random int between 5 and 100

Note that you can also generate random float values using rng.randf_range().

Finally, if you want to convert an numeric value to a string, you want the str() function, like this:

text1.text = str(rand_int_1)
by (12,475 points)
selected by

Thank you! It's fully fixed now! I can now continue to make the first ver. of my game!

+1 vote

RandomNumberGenerator.new() creates a RandomNumberGenerator object. Your error comes because you can't pass this to int().

To generate a random number from this object, you need to use one of its appropriate methods, such as randi() or randi_range().

Please see the docs for RandomNumberGenerator, where more examples are shown.

by (20,923 points)
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.