2 registered members (AndrewAMD, VoroneTZ),
1,258
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Vectors - why does my code work ???
#244320
01/04/09 00:48
01/04/09 00:48
|
Joined: Jun 2008
Posts: 24 germany --> nrw --> aachen
Davidus
OP
Newbie
|
OP
Newbie
Joined: Jun 2008
Posts: 24
germany --> nrw --> aachen
|
Greetings, my problem is already solved, but i have absolutely no idea why it works, and that's why i ask (i know this is a 'lil bit 'reverse' compared to normal questions... ) The non working code:
VECTOR* target = vector(mouse_cursor.x,mouse_cursor.y,800);
The working code
VECTOR* target = vector(0,0,0);
target.x = mouse_cursor.x;
target.y = mouse_cursor.y;
target.z = 800;
"Target" is pretty self explaining, i think - it is later computed into an angle for a projectile to fly where the player has pointed to with the mouse. (using vec_diff and then vec_to_angle)
|
|
|
Re: Vectors - why does my code work ???
[Re: Davidus]
#244328
01/04/09 01:14
01/04/09 01:14
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Neither of these can be trusted to work inside functions IMHO. Inside functions (ie LOCAL variable) the vector should be like so. function DoSomeThing()
{
...
//BADBADBAD VECTOR target = vector(mouse_cursor.x,mouse_cursor.y,800); BADBADBAD//
VECTOR target;
vec_set(target,vector(mouse_cursor.x,mouse_cursor.y,800);
...
} But if you are defining it as a GLOBAL (outside a function), you should use VECTOR* target = { x=0; y=0; z=0; }
...
function DoSomeThing()
{
...
vec_set(target, vector(mouse_cursor.x,mouse_cursor.y,800));
...
} Vectors are buggers to work with, I think it may be poor documentation, or maybe Im just a bit thick. I had lots of troubles with them too. But once you get to grips with them, its not a problem. I expect both your examples to fail eventually because both create a pointer to the vector() function result area, which is only a temporary data location. Look at the remarks under the vector function in the manual.
Last edited by EvilSOB; 01/04/09 20:39. Reason: Code corrected - Tobias is right.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Vectors - why does my code work ???
[Re: EvilSOB]
#244334
01/04/09 02:30
01/04/09 02:30
|
Joined: Jun 2008
Posts: 24 germany --> nrw --> aachen
Davidus
OP
Newbie
|
OP
Newbie
Joined: Jun 2008
Posts: 24
germany --> nrw --> aachen
|
okay, very interesting, i didn't knew this way to create permanent global vectors, but is there also a way to create permanent local vectors ? like something i can store inside an entity, for example a place where the entity has to move to? or does that only work by doing:
VECTOR* tmp = vector(0,0,0);
my.skill1 = tmp.x;
my.skill2 = tmp.y;
my.skill3 = tmp.z;
I would like to avoid the problem that skill1-3 are changed permanently because their value is connected to the tmp-vector...
|
|
|
Re: Vectors - why does my code work ???
[Re: Davidus]
#244337
01/04/09 02:59
01/04/09 02:59
|
Joined: Nov 2007
Posts: 1,143 United Kingdom
DJBMASTER
Serious User
|
Serious User
Joined: Nov 2007
Posts: 1,143
United Kingdom
|
you can just define 3 local variables inside the entity's action...
int my_x = 0; int my_y = 0; int my_z = 0;
But remember these are only available to this action, and statements inside this action. You cant set something like "my_x = 10;" from another function because these are local variables. For things like that, lookup pointers.
If you want to learn more about storing a collection of variables, or creating custom data sets then lookup structs in the manual.
Last edited by DJBMASTER; 01/04/09 03:00.
|
|
|
Re: Vectors - why does my code work ???
[Re: DJBMASTER]
#244341
01/04/09 03:30
01/04/09 03:30
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Yes Davidus, and very easily too. All the 'skills' in 3dgs, that is entity, particle, or materials skills, are already being stored in vector type format, so you can simply place a skill# reference in any vector function, and that skill and the following two will be processed as if it WAS a vector. As an example, BOTH of the following code chunks do the same thing. //safely set_vector to 10,20,30
vec_set(my.skill30, vector(10,20,30) );
//safely set_vector to 10,20,30 the long way
my.skill30 = 10;
my.skill31 = 20;
my.skill32 = 30;
//either of which may be followed by
vec_scale(my.skill30, 0.1); //divide vector by 10
//which makes my.skill30=1, my.skill31=2 and my.skill32=3 This "synthetic vector" type of access can be used pretty much anywhere that real vectors are used. And which skill number you use is un-important as long as there are two skills left following it. This is an invaluable way of attaching vector data to entites. Is this what you mean?
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Vectors - why does my code work ???
[Re: EvilSOB]
#244372
01/04/09 10:53
01/04/09 10:53
|
Joined: Aug 2000
Posts: 1,140 Baunatal, Germany
Tobias
Moderator
|
Moderator
Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
|
Both versions wont work, and the first version by EvilSOB doesnt work either. Reason is wrong use of the vector(...) function: http://manual.conitec.net/avector.htm This is a temporary vector only, it exists only on the stack and will get overwritten by another vector(...) call later and then your content is destroyed. Here is how to do it right: function DoSomeThing()
{
...
VECTOR vtarget;
vec_set(vtarget,vector(mouse_cursor.x,mouse_cursor.y,800));
...
} This will work also: function DoSomeThing()
{
...
VECTOR* vtarget = { x=0; y=0; z=0; }
vec_set(vtarget,vector(mouse_cursor.x,mouse_cursor.y,800));
...
} For filling a vector you use vec_set and not '=', and dont name a vector "target" because that name is already reserved by an engine variable.
|
|
|
|