Help with text to array

This forum is currently in read-only mode.
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hi all

    I would like to convert a paragraph with letter to array. Like this;

    "It was a beatiful saturday morning, birds were singing, sun was shining"

    Array x1=I x2=t x3=space x4=w x5=a and goes on like that.

    I guess I can do this with simple loop maybe from a text file to array, but I can't produce any idea now. I don't want to spend time with writing it letter to letter by myself, beceause I may change my paragraph later.

    Reason of my wish is;

    After I filled an array with that paragraph, I will want user to write that paragraph as fast as they can without any mistakes or with some mistakes. I think I will use an second array line for user's inputs. And than I will compare user's array and paragraph's array. Than handle score system with mistakes and time.

    I feel sorry beceause of asking probably very simple thing on here. Still I try to solve this by myself until someone helps :) thank you vey much.

  • Hm... I don't think you need to use array at all. There are special system functions that seem to do the job you'd want.

    Some functions you should know:

    -Left(string,N)

    -Right(string,N)

    I'll explain these later.

    So what I've read you want something like "here's the text, now copy it".

    Ok. First the variables:

    Paragraph = "Bla Bla Bla Bla"

    TheLetter = ""

    Now this is the logic. I don't know how you'll fit it into your game but you'll manage. Using Left() you can find the very first letter of any string chosen:

    TheLetter = Left(Paragraph,1)

    This function Left() will cut down the string Paragraph to the number (ie, in this case the very first letter of Paragraph). Once we cut down Paragraph it will be something like "B" and will be the variable TheLetter.

    Ok so you'll need to detect the key pressed which will equal TheLetter. This is a trick I do not know, at least for now.

    Once the key pressed equals TheLetter the game should remove that letter by using Right() which works the other way than Left().

    Paragraph = Right(Paragraph,Len(Paragraph)-1)

    This will remove the first letter of Paragraph and will leave it with "la Bla Bla Bla".

    Then you can make it cycle again.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you very much for your quick help jangsy5

    Well actually I prepared something with arrays. I don't know if it's stable or right way to do it but results are seem ok.

    Thank you for letting me know about left() and right() expressions. They helped me a lot.

    I used this;

    on start of layout -> Array: Set size to Numtokens(Source.text)*1*1

       For each element -> Array: Set index(Array.CurretX,Array.CurrentY) to Gettoken(Source.text,Array.CurrentX)

       For "count" from 1 to Array.sizeX -> Text2:Set text to Text2.text+Array((Loopindex),1)

    On text changed -> Text3:Set text to Text3.text+Right(EditBox.Text,1)

    Array2:Set index global("increase") to Right(Text3.Text,1)

    System: Add 1 to global variable"increase"

    Always Text4: Set text to Array(global("increase")-1)

    Text5: Set text to Array2global("increase")-1)

    Text4 compare Text 5 if true Text6: Set text("great")

    else Text6: Set text ("be careful")

    -------------------------------------------------------

    Yep I did something like that. First I added a text element which named source. And I wroted a text like this: "I,t, ,w,a,s, a, ,b,e,a,t,i,f,u,l, ,d,a,y"

    I used numtoken to count every character which seperated with comma. And set size with it.

    After that I used for each element loop to put those every character to x array line like x1=I x2=t x3="" x4=w etc.

    Than I used for "count" from 1 to array.sizeX write every character to text object. So people can see that paragraph without any commas.

    Than I added an edit box for player to write on.

    I used "on text changed" and used another text object to write to. So If player writes something(edit box text changed) than it also writes on text3 object. Than Array2 takes from that text 3 object's right() character to Array2's x line. I used a global variable "increase" for increase on every character input it takes that character to Array2 x1 than x2 than x3 etc.

    I used "always" write latest arrayX and array2X character. ArrayX character for (which character had to be written) Array2X character for (which character written)

    Than I compare those charactes with each other. If they are equal text 6 object returns "great" else "be careful".

    It seems eveything works as I want for now. Now I'll prepare some graphical animations for nice looking. I'll try to add score system(if they are equal add 1 to score) something like that.

    Still I don't know if I did it true way. Maybe your way was way better and easier for after edit or update. Probably there are better solutions to get same result. For example it's irritating to prepare your paragraph letter to letter with commas. Maybe there may be a loop which automatically puts commas between each letters. I don't know.

    Oh and I guess I should inhibit user to backspace(delete what he/she wrotes) to prevent arrays mixing. But I guess on edit box object I can't inhibit it. Or I should way to find if user deletes what it wrotes than I should delete latest arrays and decrease array x.

    Whatever thank you very much. Sorry I wrote too long :)

  • Now I thought about how can I handle if player deletes. And I guess I can use "on text changed" len(text3.text). If I use a variable to count the lenght of text. Than on text changed if lenght increased do regular codes. But if lenght decreased so user deleted something. Now do something about arrays and scores. <img src="smileys/smiley24.gif" border="0" align="middle" /> omg I should fix many things on deleting.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)