How do I isolate numbers in a string of words?

Get help using Construct 3!

Post » Wed Oct 04, 2017 11:04 pm

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.
B
2
Posts: 5
Reputation: 160

Post » Thu Oct 05, 2017 7:57 am

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.
B
12
S
4
G
1
Posts: 56
Reputation: 1,029

Post » Thu Oct 05, 2017 4:31 pm

mOOnpunk wrote: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
B
2
Posts: 5
Reputation: 160

Post » Thu Oct 05, 2017 7:46 pm

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.
B
12
S
4
G
1
Posts: 56
Reputation: 1,029

Post » Thu Oct 05, 2017 7:59 pm

mOOnpunk wrote: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"
B
2
Posts: 5
Reputation: 160

Post » Thu Oct 05, 2017 8:09 pm

mOOnpunk wrote: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
B
2
Posts: 5
Reputation: 160

Post » Fri Oct 06, 2017 1:51 pm

Doveda wrote:
mOOnpunk wrote: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.
B
12
S
4
G
1
Posts: 56
Reputation: 1,029

Post » Fri Oct 06, 2017 2:45 pm

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
B
12
S
4
G
1
Posts: 56
Reputation: 1,029

Post » Fri Oct 06, 2017 2:56 pm

mOOnpunk wrote:
Doveda wrote:
mOOnpunk wrote: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?
B
2
Posts: 5
Reputation: 160

Post » Fri Oct 06, 2017 3:02 pm

Perhaps turn the event into an OR event and add the same condition again except check the string in lowercase.
B
12
S
4
G
1
Posts: 56
Reputation: 1,029

Next

Return to How do I...?

Who is online

Users browsing this forum: No registered users and 2 guests