|
Arrays, pointers, vectors, oh my!
#304953
01/12/10 03:59
01/12/10 03:59
|
Joined: Sep 2008
Posts: 68
Tai
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2008
Posts: 68
|
So, I define the following
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 And then I do this...
vec_set(crowd_pos[0],my.x);
It crashes! Why?
|
|
|
Re: Arrays, pointers, vectors, oh my!
[Re: Tai]
#304965
01/12/10 09:10
01/12/10 09:10
|
Joined: Feb 2009
Posts: 84 Deutschland/Niedersachsen
GorNaKosh
Junior Member
|
Junior Member
Joined: Feb 2009
Posts: 84
Deutschland/Niedersachsen
|
I think you want to store the vector over a longer time. For this you have to allocate some memory to hold the values:
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:
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.
|
|
|
Re: Arrays, pointers, vectors, oh my!
[Re: Spirit]
#305029
01/12/10 17:08
01/12/10 17:08
|
Joined: Sep 2008
Posts: 68
Tai
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2008
Posts: 68
|
Well defining the vector like this worked,
my_pos = my.x;
crowd_pos[0] = my_pos;
But then when I try a comparison, like so
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, And compare them it still crashes.
|
|
|
Re: Arrays, pointers, vectors, oh my!
[Re: Tai]
#305034
01/12/10 17:17
01/12/10 17:17
|
Joined: Aug 2000
Posts: 1,140 Baunatal, Germany
Tobias
Moderator
|
Moderator
Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
|
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
|
|
|
Re: Arrays, pointers, vectors, oh my!
[Re: Tobias]
#305125
01/12/10 23:54
01/12/10 23:54
|
Joined: Sep 2008
Posts: 68
Tai
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2008
Posts: 68
|
Interesting.
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.
Last edited by Tai; 01/12/10 23:55.
|
|
|
Re: Arrays, pointers, vectors, oh my!
[Re: Tai]
#305145
01/13/10 06:43
01/13/10 06:43
|
Joined: Dec 2006
Posts: 1,086 Queensland - Australia
Nidhogg
Serious User
|
Serious User
Joined: Dec 2006
Posts: 1,086
Queensland - Australia
|
Just a stab in the dark but, maybe because crowd_pos is a vector. Have you tried crowd_pos.x ?
Windows XP SP3 Intel Dual Core CPU: E5200 @ 2.5GHz 4.00GB DDR3 Ram ASUS P5G41T-M LX PCIE x16 GeForce GTS 450 1Gb SB Audigy 4 Spyware Doctor with AntiVirus
|
|
|
Re: Arrays, pointers, vectors, oh my!
[Re: Nidhogg]
#305216
01/13/10 16:28
01/13/10 16:28
|
Joined: Sep 2008
Posts: 68
Tai
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2008
Posts: 68
|
I have the same problem doing that. Here is my entire relevant code.
int crowd_num = -1;
VECTOR* crowd_pos[100];
ENTITY* crowd[100];
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);
}
}
|
|
|
|