0 votes

How can I make it so that somewhere on the screen (in the game, not in the console) a new random text appears every time that I ask? I want to do something like a splash system like in minecraft. Thank you very much if you write me options!

RUS:

Как я могу сделать так,что бы где нибудь на экране (в игре,не в консоли) появлялася каждый раз новый рандомный текст который я задам? Я хочу сделать что то вроде системы сплешов как в майнкрафт. Спасибо большое если вы мне напишите варианты!

Godot version 3.3.1
in Engine by (49 points)

1 Answer

+1 vote
var text_variants : = ["text1", "text2", "text3"]

#option 1
func show_random_text() -> void:
   $Label.text = text_variant[randi % text_variants.size() - 1]

#option 2
func get_random_text() -> String:
   return text_variant[randi % text_variants.size() - 1]

This should do the trick.
The variable text_variant holds all the messages you may want to show the player.
The first function applies one of the strings onto a label.
The second function returns one of the strings which you can then use however you like.

Hope this helped!

by (20 points)

Sorry, but this is throwing an error in your code. I have to ask:
1. Should the function be bound to the "Label" element?
2. Do I need to bind any nodes to the script?

Right! Indeed, if you add this script to a label element then instead of "$Label.text = " you just write "text ="

You've got a lot of options of how to go about this overall. If you add this script to a label node then you don't need to add any other nodes. Otherwise you would want to add a label node as a child to the node holding this script for instance. But it depends on the architecture of your project! Have fun.

This function still does not work for me. I attached a script to the "Label" element in my 2D scene and replaced "$ Label.text =" with the usual "text =". I don’t know what this is connected with.

Here is the code:

extends Label


var text_variants: = ["text1", "text2", "text3"]

func show_random_text () -> void:
    text = text_variant [randi% text_variants.size () - 1]

Throws an error at 7.23
I'm not that long at godot yet.

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 Frequently asked questions and 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 [email protected] with your username.