Game Development Community

access element of a vector

by Brian Carlson · in Torque 2D Beginner · 03/27/2016 (12:53 pm) · 4 replies

Hello everyone,
I've been having trouble with vectors for a while now, and haven't found what I want from the TorqueScript guides.

For example, if I have
%inn.Position = "20 10";
I haven't been successful at using

%inn.getX = %inn.Position[0];
%inn.getY = %inn.Position[1];

This would be useful to get the position of some sprite on the screen.
To get around this, to access a sprite's x and y position, I have had declare the x and y positions first, using them to make the position.

%inn.PosX = 20;
%inn.PosY = 10;
%inn.Position = %inn.PosX SPC %inn.PosY;
...later
%otherSprite.Position = (%inn.PosX - 2) SPC (%inn.PosX + 10);

It would be nice to be able to access the %inn.Position variable directly.

Thanks in advance!

Brian Carlson

#1
03/27/2016 (1:58 pm)
T2D has string accessors for delimited words as mentioned in an old T2D release announcement:
String Accessors
You can now access a string using:
  • .x .y .z .w (index 0 to 3)
  • .r .g .b .a (index 0 to 3)
  • .width .height (index 0 and 1)
  • ._n (index n)
  • .count (get a count of words equivalent to "getWordCount(...)")

So in your example you could do:
%inn.Position = "20 10";

echo(%inn.Position.count) // prints: 2

echo(%inn.Position.x) // prints: 20
echo(%inn.Position.y) // prints: 10

echo(%inn.Position.r) // prints: 20
echo(%inn.Position.g) // prints: 10

echo(%inn.Position.width) // prints: 20
echo(%inn.Position.height) // prints: 10

echo(%inn.Position._2) // prints: 10
#2
03/27/2016 (4:50 pm)
Great! Thank you! That's a big help
#3
03/27/2016 (6:38 pm)
Not to mention:
%spaceSeparated = "10 20";
%first = getWord(%spaceSeparated, 0);
%second = getWord(%spaceSeparated, 1);

// tab or newline separated
%tabSeparated = "10" TAB "20"; // because tabs don't work in the forum editor
%first = getField(%tabSeparated, 0);
%second = getField(%tabSeparated, 1);

// newline separated
%newlineSeparated = "10" NL "20"; // for obvious reasons
%first = getRecord(%newlineSeparated, 0);
%second = getRecord(%newlineSeparated, 1);
#4
03/28/2016 (5:50 am)
Thank you, Richard. Those are also great to know!