Vectors - why does my code work ???

Posted By: Davidus

Vectors - why does my code work ??? - 01/04/09 00:48

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... laugh )

The non working code:
Code:
VECTOR* target = vector(mouse_cursor.x,mouse_cursor.y,800);

The working code
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)
Posted By: EvilSOB

Re: Vectors - why does my code work ??? - 01/04/09 01:14

Neither of these can be trusted to work inside functions IMHO.

Inside functions (ie LOCAL variable) the vector should be like so.
Code:
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
Code:
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.
Posted By: Davidus

Re: Vectors - why does my code work ??? - 01/04/09 02:30

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:
Code:
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...
Posted By: DJBMASTER

Re: Vectors - why does my code work ??? - 01/04/09 02:59

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.
Posted By: EvilSOB

Re: Vectors - why does my code work ??? - 01/04/09 03:30

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.
Code:
//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?
Posted By: Tobias

Re: Vectors - why does my code work ??? - 01/04/09 10:53

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:

Code:
function DoSomeThing()
{
   ...
   VECTOR vtarget;
   vec_set(vtarget,vector(mouse_cursor.x,mouse_cursor.y,800));
   ...
}


This will work also:

Code:
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.


© 2023 lite-C Forums