Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (dr_panther, AndrewAMD, Ayumi, TedMar), 1,033 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Arrays, pointers, vectors, oh my! #304953
01/12/10 03:59
01/12/10 03:59
Joined: Sep 2008
Posts: 68
T
Tai Offline OP
Junior Member
Tai  Offline OP
Junior Member
T

Joined: Sep 2008
Posts: 68
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?

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 Offline
Junior Member
GorNaKosh  Offline
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:
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.

Re: Arrays, pointers, vectors, oh my! [Re: GorNaKosh] #304966
01/12/10 09:23
01/12/10 09:23
Joined: Sep 2003
Posts: 929
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 929
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.

Re: Arrays, pointers, vectors, oh my! [Re: Spirit] #305029
01/12/10 17:08
01/12/10 17:08
Joined: Sep 2008
Posts: 68
T
Tai Offline OP
Junior Member
Tai  Offline OP
Junior Member
T

Joined: Sep 2008
Posts: 68
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.

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 Offline

Moderator
Tobias  Offline

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
T
Tai Offline OP
Junior Member
Tai  Offline OP
Junior Member
T

Joined: Sep 2008
Posts: 68
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.

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 Offline
Serious User
Nidhogg  Offline
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
T
Tai Offline OP
Junior Member
Tai  Offline OP
Junior Member
T

Joined: Sep 2008
Posts: 68
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);
	}
}



Re: Arrays, pointers, vectors, oh my! [Re: Tai] #305219
01/13/10 16:36
01/13/10 16:36
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
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)

Re: Arrays, pointers, vectors, oh my! [Re: Widi] #305221
01/13/10 16:44
01/13/10 16:44
Joined: Sep 2008
Posts: 68
T
Tai Offline OP
Junior Member
Tai  Offline OP
Junior Member
T

Joined: Sep 2008
Posts: 68
But if I define it that way, I get a struct error because it can't convert the pointer to a vector?

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1