How do I check if an array contains some words?

Get help using Construct 2

Post » Sat Nov 29, 2014 5:44 am

Hi there,

I'm not too good with arrays (yet... ;) ), so I hope someone can help me out here...
I'm working on a project in which a player inputs some words; these words are then added to an array.
Then a second player adds new words, but he has to use all the words that are already added to the array, therefore I want to check if all of the words in this array are used in some textfield.

The textfield may contain other words, but ALL of the words in the array should be used in the textfield.
I hope I'm making sense here... ;)

Thanks in advance!
Image
B
54
S
24
G
12
Posts: 96
Reputation: 10,316

Post » Sat Nov 29, 2014 9:30 am

https://copy.com/DKdeYAd8omHJzITq

Image

add adds the words to the array
finish and compare will finish the "sentence" and compare if all words from the last finish and compare were present
note: you might want to add a lowercase conversion to not differentiate between "word" and "Word"
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Sun Nov 30, 2014 7:27 am

Hi mindfaQ,

thanks a lot for the work you've put into this example!!
I've been able to reproduce the project (simply by typing what YOU typed :D )
Now I'll have to try to figure out what's really happening here...as I mentioned in my post: I'm not too good at arrays...

Any tutorials or 'reading stuff' you could suggest on arrays?

Thanks again!!
Image
B
54
S
24
G
12
Posts: 96
Reputation: 10,316

Post » Sun Nov 30, 2014 12:52 pm

In this example the array is used just like a spread sheet. So imagine one.

First column = word
Second column = number of times it was present in the previous sentence
Third column = number of times it is present in the current sentence
(remember: first column has the Y-index 0)
Each word has its own row

Function add_words
* event 2: checks if the word can be found in any cell in the spread sheet (was already present in the previous sentences, so to say)
* event 3: if it does, it loops through all rows (X) and looks in the first column, if the word matches (this is to find out the row's index, which contains the word)
then it adds 1 to the word count in the third column
* event 4: if the word is not contained, the word gets a new row for its own (that's what the push does); since it is a new word, it was not contained in the previous sentence, so the word count in column 2 is 0 and in column 3 is 1

* event 5-6: just loops through the textbox and runs the add_words function for each word; use lowercase(tokenat...) to prevent discerning between Word, word and WORD.

* event 7 and following will do the check, if all words from the previous sentence are contained in the new sentence
* event 8: loops through each row (= each word)
* event 9: if the word was contained at least as many times in the new sentence as in the old one -> everything is fine, the values are set in a way, that the word count of the new sentence becomes the word count for the old sentence
* event 10: if a word was missing one or more times, it adds the amount of times it was missing to the variable wrong
* event 11: checks if no word was missing and throws a success message
* event 12: if at least 1 word was missing, it throws an error message


check the expressions for arrays, if you don't understand one of the actions: https://www.scirra.com/manual/108/array
most important are imo array.at(x,y,z) to read values from the array and, when looping, array.curx, array.cury and array.curz. Width, heigth, depth can help too, but were not really needed in this case.
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 787
Reputation: 3,786

Post » Wed Dec 03, 2014 10:04 am

Hi mindfaQ,

many thanks again for your reply!
At the moment I'm working on an other project.
As soon as I revert to this project again, I'll try to follow/understand what you're doing in attached project.

THANKS!
Image
B
54
S
24
G
12
Posts: 96
Reputation: 10,316


Return to How do I....?

Who is online

Users browsing this forum: Google [Bot], jkmalau, MoscowModder, terence951, vegapomme27 and 11 guests