How do I isolate numbers in a string of words?

0 favourites
  • 11 posts
From the Asset Store
Simple but fun word game. Restore an original word from shuffled letters.
  • I'm trying to make a game with an input for lines of code, and I want the system to recognize a number in the string, isolate it, and then turn that into a global variable.

    For example, if I had the line of code that needed to be input read "Player.Angle.40" I would want that 40 to be turned in to a variable, then change the angle of the player to the value of that variable.

    I figured out how to set a variable to what is inputted into the text box, but I don't know what to do from there to isolate just the number.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If the format doesnt change then "Player.Angle.40" is basicly a string of tokens with "." being the seperator, so you could do -

    set variable to int( tokenat( mytextbox, 2, ".") )

    where mytextbox is the text box holding the string, 2 is the 3rd token in the string (starting at 0), and "." is the seperator. The int() then turns the "40" text into a number.

  • If the format doesnt change then "Player.Angle.40" is basicly a string of tokens with "." being the seperator, so you could do -

    set variable to int( tokenat( mytextbox, 2, ".") )

    where mytextbox is the text box holding the string, 2 is the 3rd token in the string (starting at 0), and "." is the seperator. The int() then turns the "40" text into a number.

    And If I want to set multiple things, like "Player.Action.Time.30" For the player to perform an action for a variable amount of seconds. While being able to distinguish between the 2 commands?

    Edit: Or if I wanted to have something with 4 tokens separated by "." how could I distinguish that from the one with the numbers

    Example: Player.Movement.Horizontal.Right

  • I guess you would have to check for a command then when a command is recognised then act on it.

    mytextbox.text = "Player.Angle.40"

    set player.angle to int(tokenat(mytextbox,2,".")

    mytextbox.text = "Player.Movement.Horizontal.Right"

    set player.x to player.x+1*60*dt

    etc.

  • Example: Player.Movement.Horizontal.Right

    I guess you would have to check each token and make conditions about what to do.

    For example.

    compare 2 values

    tokenat(mytextbox,0,".") = "Player" then Pick player sprite.

    sub event (1)

    tokenat(mytextbox,1,".")= "Movement"

    sub sub event (2a)

    tokenat(mytextbox,2,".")= "Horizontal"

    sub sub sub event (3a)

    tokenat(mytextbox,3,".")= "Right" then move player sprite.x+1

    tokenat(mytextbox,3,".")= "Left" then move player sprite.x-1

    sub sub event (2b)

    tokenat(mytextbox,2,".")= "Vertical"

    sub sub sub event (3b)

    tokenat(mytextbox,3,".")= "Up" then move player sprite.y-1

    tokenat(mytextbox,3,".")= "Down" then move player sprite.y+1

    When I attempt to put it in as written (with the items changed to the correct terms and add a .text after it) I get a message saying "Type mismatch, this parameter does not take a number." I have the line as

    tokenat(ChatTest.Text,0,".") = "Lander"

  • I guess you would have to check for a command then when a command is recognised then act on it.

    mytextbox.text = "Player.Angle.40"

    set player.angle to int(tokenat(mytextbox,2,".")

    mytextbox.text = "Player.Movement.Horizontal.Right"

    set player.x to player.x+1*60*dt

    etc.

    While the second one works, the top one would only allow me to input that value, IE the command only works and sets the value if it is 40

  • > I guess you would have to check for a command then when a command is recognised then act on it.

    >

    > mytextbox.text = "Player.Angle.40"

    >

    > set player.angle to int(tokenat(mytextbox,2,".")

    >

    > mytextbox.text = "Player.Movement.Horizontal.Right"

    >

    > set player.x to player.x+1*60*dt

    >

    > etc.

    >

    While the second one works, the top one would only allow me to input that value, IE the command only works and sets the value if it is 40

    Ah yes, you are right. if the inputted text is "Player.Angle.40" we could check the command without the .40 part so you could do,

    system object, compare 2 values,

    left(mytextbox,len("Player.Angle")) = "Player.Angle" , set player.angle to int(tokenat(mytextbox,2,".")

    so waht you are doing is getting the characters in the string starting from the left for the length of the text without the ".40" and seeing if it matches the command witout the number.

  • I was just doing that off the top of my head, but it was easier just to make a quick example.

    The token method was easier.

    https://www.dropbox.com/s/o1kis95q3sp05xf/commands.c3p?raw=1

  • >

    > > I guess you would have to check for a command then when a command is recognised then act on it.

    > >

    > > mytextbox.text = "Player.Angle.40"

    > >

    > > set player.angle to int(tokenat(mytextbox,2,".")

    > >

    > > mytextbox.text = "Player.Movement.Horizontal.Right"

    > >

    > > set player.x to player.x+1*60*dt

    > >

    > > etc.

    > >

    >

    > While the second one works, the top one would only allow me to input that value, IE the command only works and sets the value if it is 40

    >

    Ah yes, you are right. if the inputted text is "Player.Angle.40" we could check the command without the .40 part so you could do,

    system object, compare 2 values,

    left(mytextbox,len("Player.Angle")) = "Player.Angle" , set player.angle to int(tokenat(mytextbox,2,".")

    so waht you are doing is getting the characters in the string starting from the left for the length of the text without the ".40" and seeing if it matches the command witout the number.

    Thanks for the help, it works well. However, is there a way to make the "Player.Angle" bit case insensitive?

  • Perhaps turn the event into an OR event and add the same condition again except check the string in lowercase.

  • Sorry i made the example quickly and there was a couple of mistakes, i've fixed it and added it so commands work in lowercase.

    https://www.dropbox.com/s/o1kis95q3sp05xf/commands.c3p?raw=1

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