Arrays, pointers, vectors, oh my!

Posted By: Tai

Arrays, pointers, vectors, oh my! - 01/12/10 03:59

So, I define the following
Code:
var crowd_num = 0;
VECTOR* crowd_pos[100];
ENTITY* crowd[100];



I assign the crowd number with a the crowd_num variable which I add to at the start of the action
Code:
my = crowd[crowd_num];


And then I do this...
Code:
vec_set(crowd_pos[0],my.x);



It crashes! Why?
Posted By: GorNaKosh

Re: Arrays, pointers, vectors, oh my! - 01/12/10 09:10

I think you want to store the vector over a longer time. For this you have to allocate some memory to hold the values:
Code:
VECTOR* crowd_pos[100];

void main() {
     
     //initialize the vector
     crowd_pos[0] = malloc(sizeof(VECTOR));
     
     //now the struct is ready to use
     vec_set(crowd_pos[0], vector(1,2,3));
}



For saving vectors temporary (manipulation in one function or passing them to another function etc) you can use this:
Code:
VECTOR* crowd_pos[100];

void main() {

     //assign a temporary vector
     crowd_pos[0] = vector(1,2,3);
     
     //...
}

from the manual:
The vector pointers have a limited lifetime because there are only 64 different vectors available for this function, that are used cyclic.So use this only for passing temporary vectors to functions, but not for permanent vector pointers.
Posted By: Spirit

Re: Arrays, pointers, vectors, oh my! - 01/12/10 09:23

Correct is:

VECTOR crowd_pos[100]; // without the * !

When you add a * after the VECTOR, it's a pointer, and crashes of course when it does not point to something.
Posted By: Tai

Re: Arrays, pointers, vectors, oh my! - 01/12/10 17:08

Well defining the vector like this worked,
Code:
my_pos = my.x;
crowd_pos[0] = my_pos;


But then when I try a comparison, like so
Code:
if(vec_dist(my.x, crowd_pos[0]) < 10)


It crashes. I know I'm comparing two of the same vectors; my.x and crowd_pos[0] are the same, but if do this,
Code:
my_pos = vector(0,0,0);


And compare them it still crashes.
Posted By: Tobias

Re: Arrays, pointers, vectors, oh my! - 01/12/10 17:17

No, thats correct. When you assign something to the pointer it wont crash, unless you did something else wrong in the code, for instance my doesnt exist or someting like that.

Pointers are explained in the tutorial in one workshop, also look here under "Confusion with pointers":

http://manual.3dgamestudio.net/aAnhang_events.htm
Posted By: Tai

Re: Arrays, pointers, vectors, oh my! - 01/12/10 23:54

Interesting.
Code:
if(vec_dist(my_pos, crowd_pos[0]) > 10)


This compiles, but I get an invalid argument error instead of a straight crash in the function.
Posted By: Nidhogg

Re: Arrays, pointers, vectors, oh my! - 01/13/10 06:43

Just a stab in the dark but, maybe because crowd_pos is a vector.
Have you tried crowd_pos.x ?
Posted By: Tai

Re: Arrays, pointers, vectors, oh my! - 01/13/10 16:28

I have the same problem doing that. Here is my entire relevant code.

Code:
int crowd_num = -1;
VECTOR* crowd_pos[100];
ENTITY* crowd[100];


Code:
action test_flocker()
{
	crowd_num++;
	VECTOR* my_pos;
	my_pos = vector(my.x,my.y,my.z);
	var crowd_counter = -1;
	my = crowd[crowd_num];
	crowd_pos[0] = my_pos;
	while(1)
	{
		if(vec_dist(my_pos.x, crowd_pos[0].x) > 10)
		{
			c_move(me,vector(0,10*time_step,0),nullvector,GLIDE);
			wait(1);
		}
		wait(1);
	}
}


Posted By: Widi

Re: Arrays, pointers, vectors, oh my! - 01/13/10 16:36

don`t use the "*" after VECTOR.
VECTOR* crowd_pos[100]; // is wrong
VECTOR crowd_pos[100]; // use this

The Engine supports only 64 VECTOR Pointer, so you don`t can define 100 Pointers (*), define the VECTOR directly(without *).

(as Spirit says in his post)
Posted By: Tai

Re: Arrays, pointers, vectors, oh my! - 01/13/10 16:44

But if I define it that way, I get a struct error because it can't convert the pointer to a vector?
Posted By: Quad

Re: Arrays, pointers, vectors, oh my! - 01/13/10 16:47

Originally Posted By: Widi

The Engine supports only 64 VECTOR Pointer


wrong. there is no limit on that. thing about 64 is this:

vector funtion returns a temporary VECTOR* right? if you call them more than 64 times it starts returning same vector* s again, so you should only use it for calculations and not vector* init.
Posted By: Tai

Re: Arrays, pointers, vectors, oh my! - 01/13/10 16:54

Could I retrieve vector positions from the entity array?
Posted By: Tobias

Re: Arrays, pointers, vectors, oh my! - 01/13/10 17:42

Sure, with vec_set you can copy one vector to another.
Posted By: Widi

Re: Arrays, pointers, vectors, oh my! - 01/14/10 10:20

@Quadraxas: Maybee i am understand something wrong with the limit of 64 VECTOR Pointers.
In the Manual (--> Vector calciulation, --> vector) they say:

The vector pointers have a limited lifetime because there are only 64 different vectors available for this function, that are used cyclic.So use this only for passing temporary vectors to functions, but not for permanent vector pointers.

Can you explain me this thing? Thanks.
Posted By: JibbSmart

Re: Arrays, pointers, vectors, oh my! - 01/14/10 12:09

G'day,

The thing with the vectors is when you use something like, "vec_set(my.x, vector(1, 2, 3));" -- that vector function can only return a pointer to one of 64 internal vectors.

This'll rarely be an issue, unless you pass a pointer to a temporary vector to a function, and that function has 64 or more "vector(...)" calls in it before it is finished with the information in the temporary vector you passed to it.

I hope that makes sense laugh

Jibb
© 2024 lite-C Forums