Game Development Community

getSubStr -issue with args! (fixed)

by Christian S · in Torque 3D Professional · 08/24/2010 (9:08 pm) · 6 replies

Ok, I was roaming the forum for a good solution to move a gui-icon due to spent mana.
I ended up merely grapping the current icons position, and then changing the y value.

If I do as described in the TorqueScript .Chm
// toDo - add the parameter/variable for iconMovement based on mana casting value
function animateRatatoskr() {
 %oldPos = iconLevel.getposition();
  %climb = getSubStr(%oldPos, 4, -1);
   if (%climb > 29) {
    %newPos = %climb-5;
   }
   if (%climb < 26) {
    %newPos = 125;
   }
 iconLevel.setposition (280,%newPos);
}

I get the following error;
getSubStr(...): error, starting position and desired length must be >= 0: (4, -1)


If I ignore my initial thought of recieving some sort of 'out of bounds' due to the number going below 3 digits, thus making the string < 7 chars...
// toDo - add the parameter/variable for iconMovement based on mana casting value
function animateRatatoskr() {
 %oldPos = iconLevel.getposition();
  %climb = getSubStr(%oldPos, 4, 3);
   if (%climb > 29) {
    %newPos = %climb-5;
   }
   if (%climb < 26) {
    %newPos = 125;
   }
 iconLevel.setposition (280,%newPos);
}

it works, despite y going below 100 and thus making the string 6 chars only...

Aint that somehow wrong?

#1
08/24/2010 (9:19 pm)
getWord would be a better choice. It looks for words or numbers separated by a whitespace character.
getWord(text, index)

// toDo - add the parameter/variable for iconMovement based on mana casting value
function animateRatatoskr() {
 %oldPos = iconLevel.getposition();
 %posX = getWord(%oldPos, 0);
 %climb = getWord(%oldPos, 1);
   if (%climb > 29) {
    %newPos = %climb-5;
   }
   if (%climb < 26) {
    %newPos = 125;
   }
 iconLevel.setposition (%posX,%newPos);
}
#2
08/25/2010 (6:01 am)
Seems they both does the trick, yet youre right; getWord looks cleaner since it parse per word and as such ignores the 'size' of each coordinate.

getSubStr should chew on its args proper though :/
#3
08/25/2010 (1:21 pm)
getSubStr works as it should. It is used when you have a string of a known length/layout and need to pull parts out. For example, I have 3 objects that have been created. I have them named for the type of object (using a 3 character abbreviation) and a 5-digit serial number. So their names are:
box10001
cyl10002
sph10003

I could use the following to get the parts:
%type = getSubStr(%itemName,0,3);
%serial = getSubStr(%itemName,3,5);


In the case of position values you can't be sure of exactly where the second value will start because the first value could be 1 or more characters in length:
"1 100" (second value starts at index 2)
"10 100" (second value starts at index 3)
"100 100" (second value starts at index 4)

In this case, you would use getWord to get the value you were looking for. But I couldn't use getWord on my typeSerial string because there is no whitespace character. Different tools for different tasks.
#4
08/25/2010 (3:28 pm)
Well, I dig the different tools for different tasks, and agree. And have understood the difference, and my goal was to extract from pos 4 to the end of the string! From the Script manual...

string getSubStr ( string str,
int start,
int numChars = -1
)

Return a substring of str starting at start and continuing either through to the end of str (if numChars is -1) or for numChars characters (except if this would exceed the actual source string length).

-1 gives me an error
As the description tells of the use of -1, and the console throwing me an error, I wouldent claim 'it works as intended'.

Anyways, I got the issue fixed by using either the getWord (thanks much :) ), or ignoring getSubString grabbing too much of the end.
#5
08/25/2010 (3:43 pm)
I just checked and the use of -1 works... in 1.1B2. But it does not work (gives an error as you indicated) in 1.0.1. Good to know they've made that change. Thanks for pointing that out. That will be useful when i can finally use 1.1.
#6
08/25/2010 (4:29 pm)
my bad also, was not remembering to add the version number. I use 1.1 for our project.

*bash self -add ver number to posts ;)

PS: glad to know it works now.