Topic was automatically imported from the old Question2Answer platform.
Asked By
Nothing
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.
CODE:
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():
pass
func _input(event):
if event is InputEventMouseButton:
rolled_num1.randomize()
text1.text = int(rolled_num2)
text2.text = int(rolled_num2)
text3.text = int(rolled_num2)
text4.text = int(rolled_num2)
Post edited to fix code formatting. In future, please use the “Code Sample” button to format your code properly.
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():
rng.randomize()
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)
Thank you! It’s fully fixed now! I can now continue to make the first ver. of my game!