Help with random player ID generator/assigner

Hi folks, I’m relatively new to coding and using twine and I’ve hit a brick wall with an Idea I want to impliment.

I want to impliment a system that generates a random ID tag for the player that will be used to address them throughout the story. I want the player to be able to input their own name (probably using the prompt macro) and then be assigned a randomised 4 digit number. The outcome I’m trying to achieve is something like this, player inputs the name Joe Bloggs and is given the ID tag of JB1234.

I suppose what I am struggling with is how to only assign the names initals to the ID and not the rest of the name. And also saving it as a variable that can be used throughout the story without the numbers changing every passage. I’m also using Harlowe to write the code.

Apologies if I am asking a simple question but I have little experience in coding and my research hasn’t resulted an answer.

Thanks in advance!

1 Like

Please use the optional tags field of the New Topic form to indicate the name and version number of the Story Format you are using, as answers can vary based on this information. You stated you are using Harlowe, I will assume that you are using the 3.x series of that story format.

You can use the (random:) macro to generate a four digit integer between 1000 and 9999 (inclusive).

(set: _number to (random: 1000, 9999))

The following is an example of a possible solution to your requirements, it uses Temporary variables to store the interim values because they don’t need to be persisted in the History system.

{
(set: _firstname to "Joe")
(set: _familyname to "Bloggs")

(set: _fullname to _firstname + " " + _familyname)
(set: _initials to (uppercase: (substring: _firstname, 1, 1) + (substring: _familyname, 1, 1)))

(set: _number to (random: 1000, 9999))
(set: _IDtag to _initials + (str: _number))
}\
Name: _fullname
Initials: _initials
ID Tag: _IDtag

uses: the (uppercase:) macro, the (substring:) macro, and the (str:) macro.

1 Like