Help with using input values with variables along with if, else if code

I think you plan a kind of text adventure.

That’s very nice!

You might want to read the Wikipedia article about this.

I want to mention something:

You start here:

var Story =
["You wake up with the sun shining in your eyes. \nWhat do you do? \n\nA.Stand up \nB.Sit up \nC.Check yourself for injuries" 
];

Then you evaluate this

if (CurrentArea == "StarterBeach" && inputValue == "A")

		{
			Story("You stand up, and look around.");
			input.value("");
		}

		else if (CurrentArea == "StarterBeach;")
		{

			Story("Please type an answer into the box at the bottom and click the submit button");

		}

So after he chose A, B or C he / she is on anther screen (3 other screens in fact).

There he has to choose again A,B, C (for each of the 3 screens).

And again. And again. So it’s a big tree.

Your way to go there with if will lead into confusion. Try it out for a few hours. Good luck.

Idea

The reason is basically that you mix your data (your text) with the program logic (your ifs and elses).

It is wise to separate data and program logic.

Can you think of a data structure to help you with that?

I will suggest this:

Data structure:

Each sentence has a line number. Each sentence has 2 or 3 options to choose from. These lead in turn to (the line number of) new sentences.

program logic

You have ONE variable holding the current line number. Depending on the choosing of the user, we change the line number to the new line number and show the new sentence (for the new line number) and its 2-3 options.

Example

You can for each screen have ONE String in an array.

You can use the command split() to split at “#”. You get an arrayOneLine

Now the first item in arrayOneLine is the line number. The 2nd choice one. The 3rd line number to go to when choise one has been selected and so on:


"0#You wake up with the sun shining in your eyes. \nWhat do you do?# A.Stand up #1#B.Sit up #2#C.Check yourself for injuries#3"
"1#"You stand up, and look around.#....#4#....#5#"

(or simplify:
"0#Text \nwith choices\n.....#target line number 1#target line number 2#target line number 3"

)

See

yeah, i am planning a text adventure game, and im currently in grade 12 in high school, so ive got a bunch of people who can try out the game and help test it or give suggestions. i might use your idea later on, but as this is the very beginning of the game, i just want to set up a simplified start to work out kinks and figure out the overall structure of the game. there are a lot of features i want to add, such as an automatically updating map, a custom crafting system, custom magic, and save ids you can enter in on the main menu to load a previous save, so its going to be a lot of work, especially when im the only one working on it

1 Like

this forum has helped a lot with figuring out how to fix problems, and i have already learned a lot just from having more context to go with the examples on the p5.js website. thanks again. i will probably end up posting again soon enough about yet another problem if any pop up in the near future. is there any discord server or anything for processing where i can talk to people through a voice chat?

1 Like

Yeah, I would do it now, but never mind

== doesn’t work with String, use CurrentArea.equals( "StarterBeach" )... instead for both

so what do you mean by that? should i change everything to .equals?

as far as I know, you should, when you experience problems with that

I need help with parsing a string commas separated numbers into an array of numbers. The numbers will be typed into typed into a text box and the parsed into an array when a button is clicked.

I need help with parsing a string of commas separated numbers into an array of numbers. The numbers will be typed into a text box and then parsed into an array when a button is clicked.
This is what I’ve done so far;

This is the reference for create input and button.


This is the reference for create button.

This is the reference for create input.

@DariusKempton : could you please share your code with the submit button

I was trying to capture the enter key in a input field and came across this post. Attaching the solution that I worked out for everyone’s reference:


inp = createInput('');
inp.elt.addEventListener('keyup', ({key}) => {
    if (key === "Enter") sendMessage() });

function sendMessage() {
   // blah...
}
2 Likes