This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

How can i generate a Random abbreviation?

With random letters and numbers.


Godot version 3.2.3
in Engine by (86 points)

Hi, what do you mean by random abbreviation? Are these letters based on words? Or do you just want to choose random characters?

hey, I mean a word that is said but not really a word: D is a randomly generated word such as e.g.

89HA7l0 or something ... If you play clash of clans, you probably know that there is a code there, so an abbreviation. You can use it to find players. This system uses e.g. Discord. Your name is generated using a combination of numbers and letters. e.g. Max # L90t72

Do you understand what I mean?

I don't really. "89HA7l0" doesn't appear to be an abbreviation for anything. But it looks like you want to generate a unique random string of letters and numbers?

Yeah, thats what i mean :)

I don't know the best way to do this. You'll probably have to look it up yourself. Maybe you can store all strings an array and check that array every time you generate a new ID? Is this for an online game?

Yes, it's for a online game and i have 3 years experience with godot.
I know how it works a random number generator but with letters i don't know.

Thanks :D

Couldn't find a function to generate a random character so just store every character in a string and pick an index at random like "abcdefg" or something.

If you want to use this to allow players to find each other online, you might want to generate the random code on the server, to ensure it is unique (and also associate it with the player's account).

Also, if there is a chance that these random codes will be typed or written (as opposed to being exclusively copied-and-pasted), you might want to avoid using characters that look similar, like zero (0) and upper-case o. Same with one, upper-case i, and lower-case l (L). exuin's suggestion to use an array makes it easy to remove characters you want to avoid.

It might also make sense to avoid using both upper and lower-case letters, since mixing up case is easy to do.

All that does mean you might need to make the random codes longer to ensure there are enough unique values.

1 Answer

0 votes
Best answer

Implementation of the method proposed by exuin. This script generates and outputs 25 random strings when it starts.

extends Node

var ascii_letters_and_digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func gen_unique_string(length: int) -> String:
    var result = ""
    for i in range(length):
        result += ascii_letters_and_digits[randi() % ascii_letters_and_digits.length()]
    return result

func _ready():
    for i in range(25):
        print("#", gen_unique_string(8))
by (90 points)
selected by
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.