Gamestudio Links
Zorro Links
Newest Posts
Purchase A8 full licence version
by ukgamer. 04/29/26 18:09
Z9 getting Error 058
by k_ivan. 04/25/26 19:13
ZorroGPT
by TipmyPip. 04/25/26 16:09
Stooq now requires an API key
by jcl. 04/13/26 09:42
Strange "Alien" Skull created with >Knubber<
by NeoDumont. 04/10/26 18:58
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
0 registered members (), 4,296 guests, and 26 spiders.
Key: Admin, Global Mod, Mod
Newest Members
ukgamer, valino, juergenwue, VladMak, Geir
19210 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Dynamic Array of Structs #335172
07/27/10 06:16
07/27/10 06:16
Joined: Jul 2010
Posts: 2
A
aappbb Offline OP
Guest
aappbb  Offline OP
Guest
A

Joined: Jul 2010
Posts: 2
Is it possible to create a dynamic array of structs?

This kind of result.

class Tile
{
...
};

vector<Tile> myTile(100);

Last edited by aappbb; 07/27/10 06:19.
Re: Dynamic Array of Structs [Re: aappbb] #335174
07/27/10 06:36
07/27/10 06:36
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Sure, although the syntax is quite different...
Code:
#include <acknex.h>
#include <default.c>

typedef struct
{
var width;
var height;	
} 
Tile;

void main()
{
	int tile_number = 100; // the number of elements in the array.
	
	Tile* tile_array = (Tile*)malloc(tile_number * sizeof(Tile)); // allocate space for 100 Tiles
	
	int i=0;
	for(i; i<tile_number; i++) // loop through the array
	{
		// set the values of each struct here...
		tile_array[i].width = i; // for testing set it to the current iteration.
		tile_array[i].height = i;
	}
	
	// test to see if it is ok..
	
	i = 0;
	
	for(i; i<tile_number; i++)
	{
		error(str_for_num(NULL,tile_array[i].width));
	}
}



Re: Dynamic Array of Structs [Re: DJBMASTER] #335176
07/27/10 06:42
07/27/10 06:42
Joined: Jul 2010
Posts: 2
A
aappbb Offline OP
Guest
aappbb  Offline OP
Guest
A

Joined: Jul 2010
Posts: 2
Thanks.

But how do I add or delete an element.

Last edited by aappbb; 07/27/10 06:43.
Re: Dynamic Array of Structs [Re: aappbb] #335389
07/28/10 08:28
07/28/10 08:28
Joined: Dec 2008
Posts: 271
Saturnus Offline
Member
Saturnus  Offline
Member

Joined: Dec 2008
Posts: 271
Quote:
But how do I add or delete an element.

It's not as easy as with C++ vectors.

You could use the ArrayList from the wiki, which behaves like a vector. However, this ArrayList was written to handle primitive data types like var, int or pointers. It can't handle structs, so you would have to store pointers to your structs instead. Storing pointers can have both advantages and drawbacks.

Re: Dynamic Array of Structs [Re: Saturnus] #335391
07/28/10 08:53
07/28/10 08:53
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
What about linked lists?

Re: Dynamic Array of Structs [Re: Saturnus] #335392
07/28/10 08:55
07/28/10 08:55
Joined: Feb 2005
Posts: 3,687
Hessen, Germany
T
Tempelbauer Offline
Expert
Tempelbauer  Offline
Expert
T

Joined: Feb 2005
Posts: 3,687
Hessen, Germany
thats right

you can create an array of structs by allocating new memory for it, like djbmaster said.
for a more comfortable use of arrays, you can use the implementation saturnus mentioned: http://www.opserver.de/wiki/index.php/ArrayList

to add or delete elements you can use the ll_arraylist_insert and ll_arraylist_remove functions. on the wiki is the complete documentation


Gamestudio download | 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