Well, it's time to start random

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ZeralldGames

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:

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

:bust_in_silhouette: Reply From: lentsius-bark
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!

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?

ZeralldGames | 2021-05-27 12:16

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.

lentsius-bark | 2021-05-27 13:24

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.

ZeralldGames | 2021-05-27 15:50