Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (Akow), 1,361 guests, and 9 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19055 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
dynamic array in structs [solved] #416870
02/05/13 15:13
02/05/13 15:13
Joined: Mar 2012
Posts: 44
A
Abarudra Offline OP
Newbie
Abarudra  Offline OP
Newbie
A

Joined: Mar 2012
Posts: 44
hi, me again with another question about structs.

In my attempt to create a fully customisabel turret script, i got this (for me) strange behaviour;

Code:
var testarray[2] = {1,39};

typedef struct
{
   [...]
   var harpoint_counter;   // Max number of weapons for the turret
   var* vertex_numbers[0]; // Array for the vertex of the model
   [...]
} TURRET;

TURRET* Test;

init_structs_startup()
{
   Test = sys_malloc(sizeof(TURRET));
   Test.hardpoint_counter = 2;
   Test.vertex_numbers = testarray;
   [...]
}


But "Test.vertex_numbers[0/1] becomes 0,2 and 0,0039 instead of 1 and 39.

Later i would use the information this way

Code:
[...]
var i=0;
for(i=0;i<Test.hardpoint_counter;i++)
{
    my.skill[4+i] = Test.vertex_numbers[i];
}
and use vec_for_vertex and ent_create to attach the guns.



But currently they are not placed at the right position, because of the wrong numbers above. Maybe i'm still to stupid for this kind of programming and the code above cant work at all.

I hope someone can help me to fix this problem or tell me to stop cause i follow a dead end. Tank you in advance.

regards

Re: dynamic array in structs [Re: Abarudra] #416872
02/05/13 15:18
02/05/13 15:18
Joined: Mar 2011
Posts: 3,150
Budapest
sivan Offline
Expert
sivan  Offline
Expert

Joined: Mar 2011
Posts: 3,150
Budapest
if you want to sys_malloc vertex_numbers:

var* vertex_numbers[0]; ---> var* vertex_numbers;

or if it is constant sized:

var* vertex_numbers[0]; ---> var vertex_numbers[10];


and I think you need memcopy to copy an array to another array.


Free world editor for 3D Gamestudio: MapBuilder Editor
Re: dynamic array in structs [Re: sivan] #416880
02/05/13 15:59
02/05/13 15:59
Joined: Mar 2012
Posts: 44
A
Abarudra Offline OP
Newbie
Abarudra  Offline OP
Newbie
A

Joined: Mar 2012
Posts: 44
Thank you for your response, but how would i have to use sys_malloc?
and if i remove [0] form vertex numbers "my.skill[4+i] = Test.vertex_numbers[i];"
produces an error message: "subscript requires array or pointer type".

Re: dynamic array in structs [Re: Abarudra] #416881
02/05/13 16:14
02/05/13 16:14
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
It's "(Test->vertex_numbers)[i]". Remember the operator precedence!

There is a sample concerning the usage of sys_malloc in the manual:

http://www.conitec.net/beta/ent_nextvertex.htm


Always learn from history, to be sure you make the same mistakes again...
Re: dynamic array in structs [Re: Uhrwerk] #416887
02/05/13 16:53
02/05/13 16:53
Joined: Mar 2012
Posts: 44
A
Abarudra Offline OP
Newbie
Abarudra  Offline OP
Newbie
A

Joined: Mar 2012
Posts: 44
Thank you Uhrwerk,

the manual is normlay my first place for advice but i missed this one, my bad.
Just one question in the example in the manual they use char instead of int or var for the array. Can i copy this without problems? And if i use char for this (i dont need big numbers there anyway) would this be correct?
Code:
Test->vertex_numbers = (char*)sys_malloc(Test.hardpoint_counter*sizeof(char));


Re: dynamic array in structs [Re: Abarudra] #416892
02/05/13 17:19
02/05/13 17:19
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
Yes and no. The code is correct and will do what you want. Please ensure then that the member "vertex_numbers" in the struct "TURRET" is char* as well.

But I wouldn't recommend using chars for this purpose. char implies that you want to use letters and char* is normally used to as an array of letters and hence a string (not a game studio STRING!). You can use short or int values for the purpose as well. Please note that his not about correctness or technical limitations but just about aesthetics and code readability.


Always learn from history, to be sure you make the same mistakes again...
Re: dynamic array in structs [Re: Uhrwerk] #416914
02/05/13 21:31
02/05/13 21:31
Joined: Mar 2012
Posts: 44
A
Abarudra Offline OP
Newbie
Abarudra  Offline OP
Newbie
A

Joined: Mar 2012
Posts: 44
I'm sorry that i reply so late (played some rounds BF3 with my friends laugh.

I will test the code tomorrow. Its kinda funny. I use Gamestudio for almost 10 years now (startet with A5 Extra) and i have still so much to learn, but its still fun and motivating.

I have to say you are awesome, i'm so glad that this forum has so many helpfull members. I hope that someday i will be able to contribute something to the community. Before then have a nice day Sir.

Re: dynamic array in structs [Re: Abarudra] #416919
02/05/13 21:38
02/05/13 21:38
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
I'm glad I could help! Thank you!


Always learn from history, to be sure you make the same mistakes again...
Re: dynamic array in structs [Re: Uhrwerk] #417121
02/08/13 17:26
02/08/13 17:26
Joined: Mar 2012
Posts: 44
A
Abarudra Offline OP
Newbie
Abarudra  Offline OP
Newbie
A

Joined: Mar 2012
Posts: 44
So, finally i was able to test it and it is working now. laugh
But not for long frown.
My Struct also contains a pointer to another Struct(array):
Code:
typedef struct
{
   [...]
   var harpoint_counter;   // Max number of weapons for the turret
   var* vertex_numbers; // Array for the vertex of the model
   [...]
   WEAPON* t_guns;
   [...]
} TURRET;



I use this part of code to allocate enough memory for all WEAPON Pointers
Code:
for(i=0;i< Basisturret.harpoint_counter;i++)
{
	(Basisturret.t_guns)[i] = setup_template_weapon(Basiswaffe); 
}

"setup_template_weapon" creates a new WEAPON struct and uses memcpy to copy the stats of Basiswaffe and finally returns the pointer of the new created struct.


This for loop should create one independant copy of "Basiswaffe" for each hardpoint and copy its pointer to the Structarray "(Basisturret.t_guns)[i]".

I use this line to allocate enough memory for the structpointerarry:
Code:
Basisturret.t_guns = (WEAPON*)sys_malloc(Basisturret.t_gun_amount*sizeof(WEAPON));


But now i get the Error Message "Can not convert 'Pointer' to 'struct Weapon'" for the line within the for loop.
(Basisturret.t_guns)[i] = setup_template_weapon(Basiswaffe);
I think I'm making a "simple" mistake here but i cant figure out what.

Thank you

Re: dynamic array in structs [Re: Abarudra] #417122
02/08/13 17:34
02/08/13 17:34
Joined: Jan 2002
Posts: 4,225
Germany / Essen
Uhrwerk Offline
Expert
Uhrwerk  Offline
Expert

Joined: Jan 2002
Posts: 4,225
Germany / Essen
What does setup_template_weapon() look like exactly?


Always learn from history, to be sure you make the same mistakes again...
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