Trying to code in player-typed commands?

So I’m trying to use the *input_text feature to make a game in the style of the old text adventure games where the player types in commands like “go north” “explore cave”, etc. My first simple experiment with whether it would work using variables and an if/else statement seemed to work, but when I tried to do one with more *if statements, the *else for unaccepted statements just defaulted to a blank page instead of my *else “that input doesn’t work” dialogue. Here’s my test that I did to see if it would work:

To progress through the game, you must input commands to control Curtis’ actions. Most of these commands will involve having to Enter, Exit, Go to, Talk, Examine, or Investigate a subject. For example, to proceed from this scene, type the phrase, exit bus. (if the command is denied, check capitalization–case sensitivity may be an issue with the code)
*input_text command
*if(command=“exit bus”)
*goto Arkham
*else
*goto StartCommandRedo

*label Arkham
He steps off the bus and it speeds off, leaving him standing on the street in the early morning light. He takes a deep breath, scoping out his surroundings and planning his next move in the town.

Curtis: “Okay… I guess I’m doing this. I should probably find somewhere to stay. But it’s early, so I could probably have a look around.”
*line_break
*line_break
*goto_scene arkham

*label StartCommandRedo
Try again! Make sure you type it exactly like this: “exit bus”
*input_text command
*if command=“exit bus”
*goto Arkham
*else
*goto StartCommandRedo

That ran okay when I tried it out in a browser, so I tried moving on to the starting point of my game, which is an intersection with multiple paths the main character can choose to take. I made a variable for each path and set up the command under the text like this:

*input_text command1 or command2 or command3 or command4 or command5
*if(command1=“go to university”)
*goto UniversityBranch
*if(command2=“go to mansion”)
*goto MansionBranch
*if(command3=“go to shops”)
*goto ShopsBranch
*if(command4=“go to harbor”)
*goto HarborBranch
*if(command5=“go to boarding house”)
*goto BoardBranch
*else
*goto MainRoadUnSup

*label MainRoadUnSup
Unsupported text! Try using go toand one of the locations in bold!
*goto_scene arkham

It runs, and when I go to the label I’ve written details for (UniversityBranch) it shows the right information. But if I input random gibberish in the text input, it doesn’t show the “unsupported text” message. Am I doing something wrong here? I felt like I was getting the hang of it for a second and then I wasn’t.

1 Like

Sharing your code in a preformatted text format would make our job easier to help you. you can hit “ctrl + shift + C” if you’re using windows or the equivalent for anything else. Or hit </> this symbol in the bar above the text box, or put three of these ` in one line: ``` and three on the line below, and write in between them.

So, I don’t know if

*text_input command1 or command 2 or command3 or command4 or command5 

is valid. Instead I created only one command var, called command1.

Also,

This will not go to label Arkham, if it is not the first label in a scene called arkham. If this is not the case use

*goto arkham

This code should work considering you have command1 string variable.



*label arkham
*input_text command1 


*if (command1= "go to university")
    *goto UniversityBranch
*if (command1= "go to mansion")
    *goto MansionBranch
*if (command1= "go to shops")
    *goto ShopsBranch
*if (command1= "go to harbor")
    *goto HarborBranch
*if (command1 = "go to boarding house")
    *goto BoardBranch
*else
    *goto MainRoadUnSup
    
    

*label MainRoadUnSup
Unsupported text! Try using go to and one of the locations in bold!

([i]the author can remind locations in bold here[/i])
*line_break

*goto arkham

And put the other labels like BoardBranch etc. below MainRoadUnSup. If there is anything that doesn’t work for you here I’ll be glad to try helping more. Hope this solves your issue.

5 Likes

Another thing that will help is changing how you check the variable. If you only check for lowercase spellings, then any capital letters will cause the input to fail. An easy way to avoid this is to capitalize every letter of the checked phrases and the input when checking the input.

*label travel_input
Where do you want to go?
*input_text input1

*if $!!{input1} = “UNIVERSITY”
    *goto university
*elseif $!!{input1} = “BAR”
    *goto bar
*else
    You can’t do that. Enter something else.
    *goto travel_input

3 Likes

The *label Arkham and the *scene are actually separate with similar names; the label is a little paragraph of flavor text but the *scene goes to the main file for the whole game. It’s a little confusing in a preview, I didn’t think of that when I copied the code earlier, sorry.

About the “command1” variable: you’re saying I should be able to create just that variable and use it for all of the inputs? I’m a little bad at the coding aspects. So like this:

*create command1 "filler text"

*input_text command1
*if(command1="go to university")
  *goto UniversityBranch
*if(command1="go to mansion")
  *goto MansionBranch
*else
  *goto MainRoadUnSup

For some reason I was under the impression that I needed a separate command variable for every possible input, but if I can use the same one that simplifies things a bit.

Yes you can use the same one since whatever the player puts there replaces the “filler text” so the variable will change to the player input.

This should work unless you have other plans like tracking the input of player. And for that I would suggest something like this:

*create command1 ""
*create command2 ""
*create command3 ""
.
.
.
*create mainCommand ""

*input_text mainCommand

*if (mainCommand = "go to university")
    *set command1 "go to university"
*if (mainCommand = "go to mansion")
    *set command1 "go to mansion"
.
.
.
*comment This will track the input, so the first command that player 
*comment texted would be going to university, Then you can do:

*if (mainCommand = "go to university")
    *set command2 "go to university"
*if (mainCommand = "go to mansion")
    *set command2 "go to mansion"

*comment This will be the second player input. 

*comment Although, if you are tracking for some reason; 
*comment say, displaying it later on, you would put something like:

*if (mainCommand = "go to university")
    *set command1 "At first, you went to the university."

*comment Then for example the player is finished in university 
*comment and you want to give one more input as to where they would 
*comment go. This time you can do:

*if (mainCommand = "go to mansion")
    *set command2 "And secondly you went to search the mansion."

*comment We are doing this to be able to display, so let's do that:

*label justForExample
Standing in the yard of Arkham asylum, you consider your next move. ${command1} ${command2} However, you couldn't find a d*mn thing related to the case. She is always one step ahead of you. In order to catch Barbara Kean, you'll have to be more clever than that.

*comment Just an example of how you might wanna use more than one variable in this situation, otherwise it is not needed.

I have a question about this: will using this format cause it to accept both the capitalized and lowercase input? Sorry if that’s a dumb question a lot of this is still pretty new for me

As long as the words match exactly, capitalization won’t matter. “university,” “UnIVerSiTy,” and “UNIVERSITY” will all be checked as “UNIVERSITY.”

So now I have a new problem. I’m getting this error when I try to type anything into the input with the code changed:

startup line 71: Invalid expression, couldn't extract another token: $!!{command} = "EXIT BUS"

All I did was change the line *if(command=“exit bus”) to *if$!!{command}=“EXIT BUS” . Did I mistype something?

With being unsure, I believe you can’t use “$” display with a command like *if. Also I’m not sure what was your aim to change it to that. At the very least what you can do is:

*set command "$!!{command}" 

This will make it all capitalized, but then if you want to decapitalize it, I don’t know if there’s a way to do that. So this may not be the best solution.

If I understand what Trev said here, It’d be fine if you don’t even set it to full capital, and just check for full capitalized, soo:

*if (command = "EXIT BUS") 

would work for all capitalizations, is that correct @trevers17 .

1 Like

My aim isn’t for the text to display in all caps, it was for it to read text as if the letters were also typed in capitals. So that if someone used capitalizations or not it wouldn’t mark an entry as wrong. Is there a way to do that other than just typing *if(command=“every combination”)?

*if $!!{command} = “UNIVERSITY”
    *goto university

This is the correct syntax. The other thing about capitalization is correct.

It won’t display in all caps. The capitalization is only for checking the variable to direct the code to the correct *goto label. $!!{command} would only display the text in all caps if you displayed it as text the player could see. In my example, the variable is being used as an array.

3 Likes

When I try using this syntax I keep getting the error “couldn’t extract another token” though. I kept everything else exactly the same, I’m not sure what I’m doing wrong. Literally all I changed was the suggested “$!!{ }” to include capitalized entries?

Let me do some quick testing. Something might’ve changed or I wrote it wrong.

Okay, I wasn’t too far off. I actually forgot to mention that you have to test the input by comparing the variable as a string, not an array. So:

*label travel
Where would you like to go?
*input_text input

*if "$!!{input}" = "UNIVERSITY"
	*goto university
*elseif "$!!{input}" = "BAR"
	*goto bar
*else
	Invalid command. Try again.
	*goto travel

*label university
You went to university.
*choice
	#Go back.
		*goto travel

*label bar
You went to the bar.
*choice
	#Go back.
		*goto travel

*finish

I tested this with multiple different capitalization variations and they all worked.

1 Like

It worked! Thanks so much! I’m still really new to the coding side of stuff so a lot of this I have to get really hands on to get the hang of it

1 Like

No worries! This is actually a little advanced and harder to figure out. Most people don’t even know you can do this with string variables haha

1 Like

Question resolved!