Game Development Community

How to access elements in a vector? How to return a variable? [SOLVED]

by RJAG Entertainment · in Torque 2D Beginner · 03/17/2013 (7:09 pm) · 4 replies

I browsed through the entire syntax and wiki, but was unable to find this information.


Under the section "VECTORS", it says it automatically creates a Vector2D if given two numbers, like so:

%position = "25 32";

^becomes a 2 element vector when created.



How do you access the different elements though?

Something like,

%ptX = %position.1stElement;
%ptY = %position.2ndElement;



In C++ it would look something like:

Point ptPOS;
int iX = ptPOS.X;
int iY = ptPOS.Y;




Also, how do you return a variable?
Searching through the syntax wiki it says
[return var;]
but this crashes T2D for me. However, a normal
return var;
doesn't crash.

#1
03/17/2013 (7:42 pm)
In order to retrieve elements of a vector you use getWord
Ex:
%pos = "10 0";
%posX = getWord(%pos,0);
%posY = getWord(%pos,1);

returns of variables are the same as C++, not sure what the brackets would be in the wiki syntax, as it isn't the way it works.

So your second example is correct. Though usually it would have a % or $ in front of the variable for the type (local vs global)
#2
03/17/2013 (7:45 pm)
Nevermind, I wasn't running the updated version of T2D MIT and also forgot to create the variable as a Vector2D as well.

(%ptMap is a Vector2 already, like "5 5" or "10 10")

%ptPlot = "0 0"; //this is important, as it wouldn't work without first making %ptPlot a Vector2D.
	%ptPlot.x = %ptMap.x + %FancyEquation;
	%ptPlot.y = %ptMap.y + %FancyEquation;

I guess I had an older version that didn't allow you to do .x, .y.




I found out the problem accidentally, when I went to the most recent news on T2D.
Quote:
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(...)")

#3
03/17/2013 (7:46 pm)
Oh, thank you Doc308. Didn't know that would work as well. That is extremely useful to know getWord.
#4
03/18/2013 (2:29 am)
Worth checking out the global functions here. Some useful stuff.