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

**URL:** <https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910>\
**Category:** Coding Questions\
**Created:** [September 17, 2020, 4:15am UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910 "2020-09-17T04:15:28Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 17, 2020, 9:27pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/21 "2020-09-17T21:27:50Z")

</div>

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:

```auto
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

```auto
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:

```auto

"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**

> [@Creating game dialogue](https://discourse.processing.org/t/creating-game-dialogue/23594):
>
> Hi I am in the process of developing my first game. It is an adventure platformer comprised of various missions that lead you to the overall objective (growing a flower). I am currently trying to develop a system to implement dialogue, however, I have not found many resources online specific to P5. Trying to follow tutorials in Java and C# is rather confusing as I do not have much experience with those languages and they also include functions not available to P5. I guess my question is where …

---

<div class="post-metadata">

**Author:** ![DariusKempton](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@DariusKempton](https://discourse.processing.org/u/DariusKempton)\
**Post date:** [September 17, 2020, 10:06pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/22 "2020-09-17T22:06:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DariusKempton](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@DariusKempton](https://discourse.processing.org/u/DariusKempton)\
**Post date:** [September 17, 2020, 10:11pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/23 "2020-09-17T22:11:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 18, 2020, 7:12am UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/24 "2020-09-18T07:12:43Z")

</div>

> [@DariusKempton](#):
>
> might use your idea later on

Yeah, I would do it now, but never mind

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 24, 2020, 4:34pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/25 "2020-09-24T16:34:50Z")

</div>

> [@Chrisir](#):
>
> `if (CurrentArea == "StarterBeach" && inputValue == "A")`

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

---

<div class="post-metadata">

**Author:** ![DariusKempton](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@DariusKempton](https://discourse.processing.org/u/DariusKempton)\
**Post date:** [September 24, 2020, 4:52pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/26 "2020-09-24T16:52:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 24, 2020, 7:43pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/27 "2020-09-24T19:43:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chigoz](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@Chigoz](https://discourse.processing.org/u/Chigoz)\
**Post date:** [September 25, 2020, 1:21pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/28 "2020-09-25T13:21:10Z")

</div>

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;

> **[p5.js Web Editor](https://editor.p5js.org/Chigoz/sketches/RMBk4fqo-)**

This is the reference for create input and button.

> **[examples | p5.js](https://p5js.org/examples/dom-input-and-button.html)**

  
This is the reference for create button.  

> **[reference | p5.js](https://p5js.org/reference/#/p5/createButton)**

  
This is the reference for create input.  

> **[reference | p5.js](https://p5js.org/reference/#/p5/createInput)**

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 25, 2020, 2:12pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/29 "2020-09-25T14:12:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://discourse.processing.org/u/rajmb)\
**Post date:** [October 27, 2020, 5:08pm UTC](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910/30 "2020-10-27T17:08:03Z")

</div>

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:

```auto

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

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

```

[Previous page](https://discourse.processing.org/t/help-with-using-input-values-with-variables-along-with-if-else-if-code/23910.md?page=1)
