Setting player name from two variables

Twine Version: 2.5.1 (browser)
Story Format: Sugarcube 2.36.1

Hello, everyone. Before I get to my problem, I must beg for any mercy you can show me.

I used to build some basic HTML websites for some forum-based roleplay games I played in high school, but never caught onto JavaScript and gave up relatively quickly. Fast forward 25 years, I’ve found a new interest that has brought me back to coding.

My wife and I are fans of fiction/fantasy/history, and I am a fan of interactive/survival/non-linear games, so during a conversation we were talking about old-school choose-your-adventure books and the challenge of semi-text-based games like Oregon Trail.

This, clearly, led me to Twine.

Now, some more quick context: I don’t actually have a computer at the moment. I haven’t had a need, and considering that I am just using my current project to learn, it’s not a priority. So, I have been building everything from a mobile device. I’m aware of the risks of losing my data.

Now to my problem, I have been scavenging pieces of code and trying to learn new things as I go, albeit slowly and not altogether competently, imho. Many guides are either not what I’m looking for (such as the Sugarcube Documentation, which did not exactly address an easily accessible solution, not dumbed down to a level I can understand, or outdated. Even many forums are shut down or Read Only.

So, apologies for being so late to the party.

I’ve found that using $object.properties ( has been the best (most common) way to code a player and the details that entails. However, every example I can find has been a single Variable for the player name. My thought was to have the first and last name be separate variables so that they can be called back separately, which is where my problem stems from.

Here’s my $player code from my StoryInit passage:

<<set $player = {
 name : "",
 health : 100,
 xp : 0,
 money : 0,
 race : "",
 gender : "",
 str : 0,
 int : 0,
 per : 0,
 cha : 0,
 hyg : 0,
 men : 0,
 arm : "",
 dmg
 }>> 

I used Text Boxes to create the player’s name on my Character Creation passage:

First Name: <<textbox "$pfname" "John">>
Last Name: <<textbox "$plname" "Doe">>

What I want to do, is assign $pfname and $plname to $player.name to prevent having to use both codes constantly, but being able to do so when it’s necessary.

The only method I have found to even print $player.name with both first and last names leaves a , between them: (StoryInit passage)

 <<set $player.name = ["$pfname"], [$plname] >>

What would be the correct way to go about doing this? I’m also interested in any resources for RPG elements that anyone has access to. Thank you in advance for any help you can provide.

1 Like

Hello!

First, an important note re: this:

Twine (desktop and browser) has the “publish” (in Build > Publish to File) function which lets you export the html of your story and save it on your device. You can then import the game back to Twine even if it gets wiped from the editor. So, I really, really recommend publishing often and making backups!

About the player name:

  1. You can totally “glue” two text variables together. The StoryInit is not the place to do that, though (it only gets executed at the start of the game, so the variable won’t be updated with the names your player chose). You have to put the code after the player has made the input (for instance, in the next passage). Then you can assign the variables:
<<set $player.name to $pfname + " " + $plname>>
  1. To print the values of the variables in the passage, you can just use the “naked variable”:

My name is $player.name

As for resources: I don’t know of any Twine-RPG specific knowledge base, but you can find a lot of good information, and even some great ready-made systems, on the Internet if your search is specific enough (always use your story format in the search too, as ways to do stuff in Twine are format-dependent. So, searches like “Sugarcube 2 how to make random NPCs” or “Sugarcube 2 inventory system” are potentially good).

If you don’t find the information you need, you can always ask here or on the Twine Reddit. Both here and there, people are in general very helpful.

Re: Sugarcube 2 documentation: As someone with virtually no previous coding/html experience, I was very intimidated by the docs when I first started learning Twine, but they’re actually a great resource. You can often learn a lot by searching with Ctrl+F for the thing you want to do, and by copying the examples provided, then fiddling with them.

You can also play some rpgs made with Twine (if you haven’t already) to get an idea of what’s possible (and, believe me, a lot is!). For starters, here’s a list of games tagged “twine” and “rpg” on the IFDB: https://ifdb.org/search?sortby=rand&searchfor=tag%3Atwine+tag%3Arpg
(full disclosure: I made some of the games on that list). I’m sure there are some as well on itch and elsewhere, which aren’t listed here.

Hope this helps. Good luck with your project!

1 Like

You are currently using Story Variables as receiver variables of your two <<textbox>> macro calls, as you state your intention is to actually store the combination of those two values within the Generic Object you’re storing in the $player variable I suggest storing both the First and Last name there too.

End-users have a habit of doing things other than the expected when it comes to supplying requested information. For this reason it is generally a good idea to validate the information that they supplied, or didn’t for that matter. And this is generally done within the body of a <<link>> or <<button>> macro call that you ask the end-user to select once they have supplied the requested information.

First Name: <<textbox "_first" "John">>
Last Name: <<textbox "_last" "Doe">>

<<button "Save Names">>
	/* Remove all leading and trailing SPACE characters. */
	<<set _first to _first.trim()>>
	<<set _last to _last.trim()>>

	/* Only continue if the end-user actually supplied a value. */
	<<if _first isnot "" and _last isnot "">>
		/* Proper case the word(s) within the two variables, there may be more one. */
		<<set $player.first to setup.toProperCase(_first)>>
		<<set $player.last to setup.toProperCase(_last)>>
		<<set $player.name to $player.first + " " + $player.last>>

		/* What to do now?, maybe move on to the "next" Passage? */
		<<goto "Library">>
	<</if>>
<</button>>

The above Passage code relies on a pre-defined setup.toProperCase() function, which can be created by placing the following JavaScript code within your project’s Story JavaScript area…

setup.toProperCase = function (str) {
	return str.replace(/\w\S*/g, word => {
		return word.charAt(0).toUpperCase() +
			word.slice(1).toLowerCase();
	});
};

note: Not all cultures break their Full Name into a First (Given) plus Last (Surname) name pair…
eg. John Handcock.

…some have the Family name as their 1st name, which is then followed by their Individual name…
eg. Lee Jun-fan (aka Bruce Lee)

…some have required middle names, like some India Personal Names.

For this reason it is often easier to just supply a single field into which the end-user enters whatever name they want to use, in whatever format makes sense to them.

Name: <<textbox "_name" "John Doe">>

<<button "Save Name">>
	/* Remove all leading and trailing SPACE characters. */
	<<set _name to _name.trim()>>

	/* Only continue if the end-user actually supplied a value. */
	<<if _name isnot "">>
		/* Proper case the word(s) within the variable, there may be more one. */
		<<set $player.name to setup.toProperCase(_name)>>

		/* What to do now?, maybe move on to the "next" Passage? */
		<<goto "Library">>
	<</if>>
<</button>>

Thank you, both, for the help. I am very grateful.