Searching for endofline character in a string?
by Jacob Wagner · in Torque Game Builder · 02/20/2006 (7:47 am) · 3 replies
I am implementing a scripting system inside of my torque 2d game. The user inputs some code which gets saved to a file.
I then convert that file into a string which ends up looking like this from the echo function:
"if( getRotation < 180)^rotate(getRotation() - 10);^else^rotate(getRotation() + 10);"
i assume that the ^ marks the end of line characters.
I am adding the robot object name in front of all robot method calls so it should eventually look like this:
if( robot1.getRotation < 180)^robot1.rotate(getRotation() - 10);^else^robot1.rotate(getRotation() + 10);
so far I'm coming along good, but the problem I have is I can't search a string for the end of line character. If I set $text to the above string and try:
echo(strpos($text, "^"));
i get -1 which means there was no match.
so, I'm assuming there's some special escape character for 'end of line' which I can enter into strpos to find
that character.
Anyone know?
I then convert that file into a string which ends up looking like this from the echo function:
"if( getRotation < 180)^rotate(getRotation() - 10);^else^rotate(getRotation() + 10);"
i assume that the ^ marks the end of line characters.
I am adding the robot object name in front of all robot method calls so it should eventually look like this:
if( robot1.getRotation < 180)^robot1.rotate(getRotation() - 10);^else^robot1.rotate(getRotation() + 10);
so far I'm coming along good, but the problem I have is I can't search a string for the end of line character. If I set $text to the above string and try:
echo(strpos($text, "^"));
i get -1 which means there was no match.
so, I'm assuming there's some special escape character for 'end of line' which I can enter into strpos to find
that character.
Anyone know?
#2
thanks for the help :P
02/20/2006 (12:29 pm)
I figured out what was going on, turns out that the ^ character actually represented a tab , so strpos($text, "\t") would find it. I didn't have any newlines in the string at all, but now I know that I can add them when creating the string with \n, so I'm all set!thanks for the help :P
#3
getField(%string, %index)
setField(%string, %index, %replacement)
removeField(%string, %index)
getFieldCount(%string)
which manipulates strings that are delimited by tabs. Great for packing/unpacking strings with multiple fields, just be careful if the data can contain user-entered tabs. :)
02/20/2006 (12:33 pm)
In that case, you can also use the built-in helper functions:getField(%string, %index)
setField(%string, %index, %replacement)
removeField(%string, %index)
getFieldCount(%string)
which manipulates strings that are delimited by tabs. Great for packing/unpacking strings with multiple fields, just be careful if the data can contain user-entered tabs. :)
Torque 3D Owner Luke D
Default Studio Name
Edit: When you echo the line to the console window, you aren't seeing the lines on separate rows, so I'm not sure if \n is going to work. If a string contains a \n (which in reality usually represents the 'LF" control character, for linefeed) then in my T2D console it actually echos out on different lines, and doesn't show ^ for anything.
Perhaps that ^ isn't LF. If searching for \n doesn't work, I'd suggest trying \r (CR, carriage return). \n and \r are used in different combinations on different OS's to represent newlines, so it may be either/or or both together. If all else fails, you might want to try to walk the string a character at a time and find out what the hex value is of that character and then you'll know for certain. Only problem with that is I don't believe Torque has a method for returning the numerical value of a char, so it might require some creativity.