Name Input - Arrays - Conditional Branches 'Oh my!'

Hi guys,

My Javascript handicap run me into a wall. I'm currently trying to create an in-game search engine where the player can access a kind of 'internet' to search up terms and get a response. I'm trying to use the Name Input command to do this. With some guidance, I've been advised to create two arrays then have the first one refer to the second one. This way, a player types in Dog and gets a text response with the info they need. As far as I know, I've created both arrays correctly, however my struggle is the input. I've created a dummy actor that'll be using, but I'm not sure how to use the name input function with conditional branches.
Why do you need two arrays?
help.png



On the third page of conditional branch, you can type in a specific name to check against an actor's name

Although this wouldn't work with an array.
Without using full JS, I can't imagine actually using an event Conditional branch for this.
You could do it with the script box on the fourth page, and ask if the array with the names of all the search words .contains(.contains($gameActors.actor(id)._name))

e.g. if your dummy actor is actor 4, and your array of searches is in Variable 5, your condition could be
$gameVariables.value(5).contains($gameActors.actor(4)._name)
Why do you need two arrays?
The first array contains keywords (ie. dog, cat, rat)
The second array contains the definitions (ie. dog blah blah blah, cat blah blah blah, rat blah blah blah)
So you have two arrays in parallel, one for names and one for descriptions, right? [Edit: ninja'd!] In that case I'd suggest an approach much like standardplayer suggests, except using indexOf so you can return the appropriate description entry, e.g.
Code:
var index = $gameVariables.value(1).indexOf($gameActors.actor(6).name()); $gameVariables.value(2)[index] || ""
^ This can go in a Control Variables > Script command. Basically it gets the index of the search term (name of actor 6) from an array stored in variable 1, then returns the corresponding description from an array stored in variable 2 (or a blank string if no description exists for that index). Change the numbers as needed~ 🙂

Alternatively, here's a text-transcript of a spruced-up event example:
Spoiler: Event example
Code:
◆Comment:Initialise arrays
:       : 1) search terms, all in lowercase because we'll force input to lowercase too
:       : 2) descriptions, for the returned values
◆Control Variables:#0001 v1 = ['apple', 'bob', 'cat']
◆Control Variables:#0002 v2 = ['A fruit', 'Clumsy', 'Meow']
◆Comment:Get search term
◆Text:None, Window, Bottom
:    :Please enter your search term!
◆Change Name:SearchTerm, 
◆Name Input Processing:SearchTerm, 10 characters
◆Comment:Seek corresponding index
◆Control Variables:#0003 x = $gameVariables.value(1).indexOf($gameActors.actor(6).name().toLowerCase())
◆If:x ≥ 0
  ◆Comment:Result found!
  :       :Get corresponding description and display to player~
  ◆Control Variables:#0004 desc = $gameVariables.value(2)[$gameVariables.value(3)]
  ◆Text:None, Window, Bottom
  :    :Requested info:
  :    :  \V[4]
  ◆
:Else
  ◆Comment:Result not found, display "error".
  ◆Text:None, Window, Bottom
  :    :Requested topic not found!
  :    :\.Beep-boop.
  ◆
:End
Note this only works for "full" matches, i.e. "cat" will only match "cat", not "cat and dog", "catch", "staccato", etc. Also, there's no "closest A-Z result" or stuff like that, it either matches a term or doesn't. Depends how in-depth you want to go with this.
A bunch of helpful stuff

Hey thanks, I can't say for sure if it'll work for me since it's complex and I'm going to take a while pouring over it but I appreciate the effort. I'll make sure to post and let everyone know, just in case others are interested in using this method.
Hope it works out! 🙂
Rather than using two Arrays and needing to keep them synced, I'd recommend using a data structure called a Dictionary in some languages (or a Map in others), which seems like it's exactly what you're making -- you're basically making a dictionary.

In a plugin (just make a .js file and put it in your plugin manager), you can make the global dictionary.

Code:
var $dictionary = new Map([
   ['dog', 'A canine animal']
   ['cat', 'A feline animal']
   ['rat', 'A rodent animal']
])
The $ in front of the name is just a convention that other RPG Maker objects follow to let you know that it's global (you can use it anywhere).

Then inside a Conditional Branch Event Command, with the script option, check if the item is in the dictionary.

Code:
$dictionary.has($gameActors.actor(6).name().toLowerCase())

That'll be true if the player entered a word that's in the dictionary.

Then you can get the value into a variable, using the Control Variables Event Command and the script option.
Code:
$dictionary.get($gameActors.actor(6).name().toLowerCase())

Now you can display that in a Show Text Event Command like normal.
There's also the good-old hashtable approach:

Code:
var $myData =
{
   "sword":"A weapon with a long blade",
   "dagger":"A weapon with a short blade",
   "spear":"A stick used for stabbing things",
}

// test if an entry exists by player input
!!$myData[$gameActors.actor(6).name().toLowerCase()]

// retrieve an entry by player input
$myData[$gameActors.actor(6).name().toLowerCase()] || "No data found";

// two ways to add an entry (use all lower case for the key (staff)
// so toLowerCase() can work is magic
$myData["staff"] = "A weapon invoking magic...or just walking";
$myData.staff = "A weapon invoking magic...or just walking";

I'd recommend either this approach or the dictionary approach used above. Two arrays though? That sounds a bit messy and overly complex.
Sorry, I think I wasn't clear on a few points. I'm not making a dictionary, I'm making a search engine. The player-character can type a word in and learn a bit about it. I'll be using it as an auxiliary way of learning about the lore of the game world. So for example, if the player-character is curious about something in the game (such as why there are Orc Shamans on a spaceship) then you can just walk up to a terminal, activate it, type in 'Shaman' and you'll get some text explaining some history.
What they meant by 'dictionary' is a special kind of array, where instead of using a numbered index to access each value, a 'key' is assigned.
Same thing with a hashtable, it's just a different way that's cleaner than using two arrays.

Any of these methods can help you get what you're looking for.
It's all just a matter of setting your condition to check for that name input, and writing handlers for what to do for each entry.
First have them type the name in
Then get a hold of the name they typed.
Find the index of that value in the first array.
Use that index as the index to access the second array.

If you type Dog in the Actor name -
-Store that actor's name a variable.
-If using arrays, use the .indexOf function on the array holding the keywords. Capture that index in a variable.
-Check if that variable is -1 first. If it is, that means the search didn't return any matches in the array.
-If the value isn't -1, use it as the index for the second array, the one that contains the search results.
Ok, I'm going to need to play with this for a while and try to understand it. I've only recently started playing with Javascript so I feel like I'm doing cryptography. Many thanks.
Rather than using two Arrays and needing to keep them synced, I'd recommend using a data structure called a Dictionary in some languages (or a Map in others), which seems like it's exactly what you're making -- you're basically making a dictionary.

In a plugin (just make a .js file and put it in your plugin manager), you can make the global dictionary.

Code:
var $dictionary = new Map([
   ['dog', 'A canine animal']
   ['cat', 'A feline animal']
   ['rat', 'A rodent animal']
])
The $ in front of the name is just a convention that other RPG Maker objects follow to let you know that it's global (you can use it anywhere).

Then inside a Conditional Branch Event Command, with the script option, check if the item is in the dictionary.

Code:
$dictionary.has($gameActors.actor(6).name().toLowerCase())

That'll be true if the player entered a word that's in the dictionary.

Then you can get the value into a variable, using the Control Variables Event Command and the script option.
Code:
$dictionary.get($gameActors.actor(6).name().toLowerCase())

Now you can display that in a Show Text Event Command like normal.

I'm trying this method and I'm really facing challenges. Would it be possible to see this in an event editor? Mine isn't working unfortunately.
Here' a method I just tested, using two arrays since you had those to start with.



arrayHelp.png


Inside of the first Control Variable commands, here's what I have in the script box

mySearchTermArray.indexOf($gameActors.actor(6)._name)


Here's what's in the second

mySearchResultsArray[$gameVariables.value(31)]



In a separate JS file, I have the two arrays defined like this:

let mySearchTermArray = ["Dog", "Cat", "Food"];
let mySearchResultsArray = ["Like a cat but doggier", "The best pet to have", "Something you consume so you don't die"];



When I run this, I get the input screen, type in my result and if it matches on of those values in the first array, it returns the result.

EDIT: However, there are a few free plugins I've seen that give you input screens and let you store the result in a variable. Using the input-name screen is kind of a workaround, there are better ways.

https://forums.rpgmakerweb.com/index.php?threads/keyboard-input-dialog.66908/

There are more than just this one, but I've used and for simple input it can get the job done. Like I said, there are others.
Thanks a lot. I'll give it a shot. I've seen a few plugins like this before, but I'm really trying to make more of my own stuff so I can learn more. Though I should probably look at how those plugins are laid out so that I can understand the flow better.

EDIT: Also one of the reasons I've opted for the name input box was because this way the player can continue using the controller. I know how annoying it can be for some players to have to go between keyboard and controller.

EDIT: I just tested it and it works! Thanks again to everyone for your help. I know if can be a bit annoying helping a code-nubbin so I appreciate everyone's advice.
Glad to help, and if that's the case, about wanting to learn on your own and wishing to stick with the Name Input screen....
Take a look in the RPG Scenes file, and if you know enough, try aliasing the Scene_Name class. You could edit your aliased version so that some of the stuff you don't need to happen doesn't, like the background window for example. Or maybe you want to change the text.
Lots of possibilities 😀
Spoiler
array trouble.png

I still have two minor problems. One is case sensitivity. I would like it to ignore case sensitivity. I tried using that 'toLowerCase()' bit of code along with the script I have but it seems like I can't get the format right, or they aren't compatible. Another problem is that when I input an invalid term, I just get a 0 in a text box.
Change the >= -1 to just
>-1
That's actually my mistake from my screenshot, sorry about that.
As for as the .toLowerCase(), that won't have compatibility issues.
It would be easiest make sure all keywords that you manually put in the array are lowercase.
This will cut some steps out.

Make sure that when you're using it, you use it on all strings being returned in your comparison.
Also change your first variable assignment to this:

mySearchTermArray.indexOf($gameActors.actor(2)._name.toLowerCase())
Everything is working perfectly now. Thanks a million! I owe you one. This will really help to enrich the game and also add another layer of depth.

Original thread: https://forums.rpgmakerweb.com/threads/name-input-arrays-conditional-branches-oh-my.114926/