Some questions about struct arrays

Started by PMick, Fri 20/07/2012 02:00:06

Previous topic - Next topic

PMick

My apologies if any of this is redundant.  I DID run a search for some of my questions regarding structs and arrays and I'm still not entirely certain of the best way to do what I'm trying to do.  If I've made any mistake in posting this, I hope you'll all chalk it up to newbie inexperience.

I am in a bit of confusion about the best way to do what I'm trying to do given the way array sizes work. What I want in my game is this: there is a specific type of game entity (we'll call it a Beef, since I like beef). Now, each Beef has a set of variables that are essentially randomized when a Beef is created. At the beginning of the game, there's only one Beef. However, more Beefs will be created through the player's actions.  That is to say, the player has the power to tell the game to generate a new Beef from time to time. In addition, from time to time Beefs may be destroyed, either by the player or by the game.  This means (a) I as the programmer don't know beforehand how many Beefs will be in any given game, and (b) the number of Beefs will change very often in any given game. After browsing through the AGS help file, it seemed to me that an array of structs was the best way to go. However, as far as I can tell, a normal array in AGS code has a set size declared upon creation. I read the bit about "dynamic arrays", but according to the help file these cannot include structs, and the data within them gets erased any time you change the array size anyway.

So, what is the best way to handle this situation? One thought that occurred to me was just to make the array size massive right from the start--i.e. "Beef beefs[10000000];", and then leave all unused Beef variables empty until new Beefs are generated by the player.  What would be the effects on memory if I did this?  Would this create a huge unwieldy bit of data that would slow down game speed, for example?  Also, this method still has a flaw--although the max is very very high, a max still exists. Ideally I'd like it to be possible for the game to go on forever if desired, so that a player could (in theory) create Beef after Beef after Beef in a very Beef-filled universe (ok, now I'm hungry...)  Is there anyway I can do what I'm trying to do and have there NOT be a limit (beyond the player's machine's computing ability, anyway)?

Again, my apologies if this post is redundant or if the information I need could have been obtained some other way.  I appreciate any help the more experienced folks here could give me--thanks in advance!

DoorKnobHandle

What you're looking for is a dynamic array (most notable the std::vector class in C++ for example). Unfortunately this is one of the biggest shortcomings of the AGS engine at this point: it doesn't support dynamic arrays for non built-in types.

So you are indeed stuck on setting up an upper limit on how many objects you want - how much this impacts the memory depends on your class and its members of course. If you're just storing a handful of ints and strings and maybe a few float values then it's not going to be a problem unless you wanna have extremely many objects (but then, you couldn't ever render all these in any way, shape or form) - when you start storing maybe big dynamic sprites, those all of the sudden do indeed take significant amounts of memory away and - I believe - AGS has its own memory consumption limit that you might hit.

So in a nutshell, what you're looking for is indeed extremely important for making a 'real' game, AGS script doesn't support it yet so you have to work-around - and how to do that exactly will vary on a case-by-case basis.

PMick

Thank you, you've been a very big help!  Let me be a little more specific and maybe you (or anyone) can tell me if I'm going to start running into memory problems...

I would actually need to do this multiple times. For most of the structs I need to create, it would simply be a set of ints and strings--anywhere from 3 or 4 to maybe 10 or 15 depending on the particular struct. I don't THINK I will need to include any DynamicSprites in any of the structs or arrays--all variables should be ints and strings.  But there would be several different structs and an array for each of them--probably about 7 or 8 different structs that would all need arrays.  Each of these arrays would need to at least be CAPABLE of storing a few hundred of their structs. So, I guess we're talking about a game that could potentially have...I dunno, we'll say a hundred of each struct within each array, multiply that by 10 arrays (just to be safe), and maybe 10 variables per struct...that comes out to about 10,000 variables, mostly of the int and short variety, but with probably at least one String per struct instance.  Can AGS handle this, or should I look for either a different engine or a different solution?

Khris

10,000 variables is a piece of cake for AGS. Don't use shorts btw, they don't really have any advantage over ints. They might decrease the size of your savegames, but the difference is marginal.

PMick

That's excellent news!  Is there really any reason to use ints when shorts will do the trick, though?  I don't anticipate my variables will ever have to get very huge.

Khris

The only reason is speed; IIRC int calculations are slightly faster.

monkey0506

Khris is correct for a 32-bit system an int will be faster than a short, in terms of CPU cycles. Most likely you wouldn't see a drastic performance impact either way, but unless memory consumption is of huge concern (since ints take up twice the space in memory) then int is more efficient.

SMF spam blocked by CleanTalk