Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / An elementary problem

Author
Message
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 6th Feb 2012 21:59
Hi all, my first post here after I downloaded the AppGameKit trial earlier in the week (I'm really impressed so far).

One problem I've found though concerns arrays. I had a look at the DIM command, and I follow how it works, however, I see that it can only be used to create static arrays. I've come from a background in Lua which handles dynamic arrays (i.e. where you can add/delete elements from arrays at will) really well - in addition to having worked with C many years ago where the user allocates memory and maintains dynamic data structures themselves.

How does one use dynamic arrays in AppGameKit? For example, if I make a game with particle effects, how do I allocate storage for particle effects on the fly, and remove them where necessary?
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 6th Feb 2012 23:10 Edited at: 6th Feb 2012 23:11
If you use tier 2, you can use all of C++'s array handling. I don't think tier 1 basic can handle dynamic arrays in that manner.

You can however use static arrays as a kind of lookup. When you create a new effect (or whatever) scan through the array until you find an unused element then use it. When you've finished, just remove the reference to that array.

For example, you could use a typed array like so (All with example variables):



Then when you need an element, just create a function to scan through the array and return an unused element like so...



My signature is NOT a moderator plaything! Stop changing it!
kamac
13
Years of Service
User Offline
Joined: 30th Nov 2010
Location: Poland
Posted: 6th Feb 2012 23:13 Edited at: 6th Feb 2012 23:14
You shall make an array, which has maximum of the elements of this type visible.

Such as:



And set all their values to, say, -999999. Then when looping through them, check if _x or _y equals -999999, if yes, it's not being drawed. You might also create another array to store their visibility.

You could do something like:



It's logic.


Woopsie. Miliseconds, but Mobiius was faster.

JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 6th Feb 2012 23:26
Unfortunately, it's not possible in the Basic version. The best you can do is re-dimension the array on the fly, so you could produce a linked list by extending the array by one element and obviously doing the index links for forward and back in the relevant structures.

You can do pretty much what you like in the Tier 2 C++ version. I happen to think a Lua interface would be a major step forward, but nobody else seems to be interested.

-- Jim
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 6th Feb 2012 23:31
So I assume for Tier 1, the idea is to try and allocate as many elements as you would ever need at the start of the program, then, if you need more, you'd copy all that data to a temporary array, delete the old array, create a new, longer one (maybe 100 elements longer than you need) then copy the data in the temp array back to that longer one, and delete the temporary array? And use a similar process if you ever wanted to make it shorter?

I thought that kind of practice would be wasteful. Then again, I suppose with the amount of RAM we're dealing with these days, and the speed to allocate/re-allocate memory, maybe I don't have to be quite as severe in my memory management as I was taught way back when.
Hockeykid
DBPro Tool Maker
17
Years of Service
User Offline
Joined: 26th Sep 2007
Location:
Posted: 7th Feb 2012 01:32 Edited at: 7th Feb 2012 01:34
You can re-dim an array at a higher value and it will retain the old data. Problem with this is it only works with single dimension arrays.

The example below will show how multi-dimension arrays seem to loose data:





The only work around to this that I've found is not exactly one that I'm a fan of. It's to Dim a temporary array at the old size and collect the data from your main array. Dim your main array to its new size and then loop through your main array and add the data from the temporary array back into your main array. This is show in the code below:






Sean

Alien Menace
AGK Developer
19
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Earth (just visiting)
Posted: 7th Feb 2012 02:40
I've requested a couple of times for dynamic type list support to be added to AppGameKit so you can manage typed elements easily in a FOR EACH loop. I was pretty surprised when AppGameKit was released without this... extremely surprised actually... seems like such a no-brainer to me. Lee did seem receptive to this request at the time though but no idea if it will ever come to fruition. I think this should be a top priority.

This is a must IMHO for a serious game creation language, very difficult (and needlessly so) to keep track of enemies, bullets, etc without them and super simple with do with them. Old C-64 style arrays work fine for many things but are cumbersome for arcade game object management. Come on, it's 2012!

Intel i7-2600k - Asus P67 Sabertooth - 16GB RAM - 2x GTX 460 OC SLI - Lexar 128GB SSD Drive - 2TB ATA/600, Windows 7 x64. iPad, iPad2, iPhone 3GS & 4, HTC Inspire, Mac-Mini. Apps published: 3
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 7th Feb 2012 11:21 Edited at: 7th Feb 2012 11:23
I agree that a "for each" loop would be great but it's not so hard to mimic this. Just store the current size of the array in the first slot (zero) and redim the array when you add or remove an item. I do this all the time and it works fine.


It only gets a little complicated if you have to remove an object from the array while you're in a "for each" loop since you have to reduce "x" in this case to re-run the current value of x but it's a pretty simple idea really.

EDIT: forgot to redim... I tend to create functions for this.

Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Feb 2012 13:45
I don't think a 'FOR EACH' command is essential as I can create my own code for this very simply with a FOR/NEXT loop. It'll be a useful command if implemented however.

I'd prefer arrays in types first. :p

My signature is NOT a moderator plaything! Stop changing it!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 7th Feb 2012 16:02
For anything non-time critical I just redim(). This (if it works like DBP) moves the array from one part of memory to another part of memory, where it is extended at the same time.


For anything time critical I used oversized arrays that will accommodate my needs during the critical period.

Alien Menace
AGK Developer
19
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Earth (just visiting)
Posted: 7th Feb 2012 18:58
Your points are well taken, however I think it would be much better just do this:

FOREACH item in items
If item.health<1 THEN DELETE item
NEXT

The idea of course is to make things easier to manage.

Intel i7-2600k - Asus P67 Sabertooth - 16GB RAM - 2x GTX 460 OC SLI - Lexar 128GB SSD Drive - 2TB ATA/600, Windows 7 x64. iPad, iPad2, iPhone 3GS & 4, HTC Inspire, Mac-Mini. Apps published: 3
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Feb 2012 20:39

Pretty much the same thing with pure code.

My signature is NOT a moderator plaything! Stop changing it!
Alien Menace
AGK Developer
19
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Earth (just visiting)
Posted: 7th Feb 2012 22:21
There is a difference between removing an item from a list and just writing a null value to it. The method you are suggesting has the FOR/NEXT loop running through a bunch of dead elements which will continue to grow over time. This becomes an issue when you have to loop through thousands of fired bullets, dead particles, dead enemy ships, used explosions, expired bonuses, etc. just to be able to "find" and process the current active objects. What is more efficient? Processing a loop with that contains 5000 elements of which only 10 are active or running through a loop of just the 10 active elements? To me, It's the difference between taking the trash out to the bin or letting it pile up in the corner.

Cheers

Intel i7-2600k - Asus P67 Sabertooth - 16GB RAM - 2x GTX 460 OC SLI - Lexar 128GB SSD Drive - 2TB ATA/600, Windows 7 x64. iPad, iPad2, iPhone 3GS & 4, HTC Inspire, Mac-Mini. Apps published: 3
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 7th Feb 2012 22:43
The for each function does the same thing, just 'on the inside'
It's not a big deal to do it, I do it this way myself in both AppGameKit and DBP and have never had any issues.

My signature is NOT a moderator plaything! Stop changing it!
Alien Menace
AGK Developer
19
Years of Service
User Offline
Joined: 11th Jan 2005
Location: Earth (just visiting)
Posted: 8th Feb 2012 02:42 Edited at: 8th Feb 2012 02:45


Intel i7-2600k - Asus P67 Sabertooth - 16GB RAM - 2x GTX 460 OC SLI - Lexar 128GB SSD Drive - 2TB ATA/600, Windows 7 x64. iPad, iPad2, iPhone 3GS & 4, HTC Inspire, Mac-Mini. Apps published: 3
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 8th Feb 2012 09:36 Edited at: 8th Feb 2012 09:37
You can use a data shuffle idea the way I showed in my code so that when you remove an item what you actually do is take the last item from the array and insert it where the removed item is, then just reduce the size of the array... then you don't skip over ANY dead items.



I was going to touch on arrays in an upcoming AppGameKit Bitesize tutorial. Maybe this would be worth covering?

KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 12th Feb 2012 15:01
So after asking this, I set to trying to make something before my trial of AppGameKit runs out later this month (so I can decide whether to buy it). I've attached what I have onscreen so far.

What I've done is create some tile images, all 64x64 pixels in size, and use iPhone 4 virtual resolution (so I can get 15x10 tiles onscreen to fill the screen).

I created an array, 15 elements by 10 elements of type "floortile", which is a type with two integers - a sprite reference and a "valid" value (which is 1 if the sprite should be drawn, and 0 if not). So this way I'm able to go into the program and set my (currently hardcoded) array so as to change what I have onscreen.

The "ninja" tile, which is the player, currently drifts at a set speed (using the frame time) just as an exercise in getting a sprite to move.

Trying to work out what to do next. I could make the player sprite fully controllable like a character in a platformer, and do collision with the wall tiles. Alternatively, I'm thinking of making a simple "endless runner", so I'm trying to work out mentally how to store the level - I mean an endless runner has a potentially infinitely long level, so I can't just load it into memory.

I'm also not sure how to make the level "move" when the character moves. In my current system all of those tiles have locations that are calculated by multiplying the tilesize (64) by the position of the tile in the array. If I move them, they'll "snap" from one grid position to another. I suspect I'm going about this totally the wrong way.

Attachments

Login to view attachments
Paul Johnston
TGC Developer
22
Years of Service
User Offline
Joined: 16th Nov 2002
Location: United Kingdom
Posted: 13th Feb 2012 18:22 Edited at: 13th Feb 2012 18:23
Instead of moving the level you can move the view with SetViewOffset, or zoom with SetViewZoom. When the view moves you could reposition the tiles that are no longer visible to the other side of the screen that has just come into view to make it look like the level continues. There would need to be some overlap over the edge of the screen so the user doesn't notice the tiles getting moved.

As for the infinite level problem, if you truly want it to be infinite then it will have to repeat at some point, or be generated procedurally using something like perlin noise.

Login to post a reply

Server time is: 2024-11-23 04:22:56
Your offset time is: 2024-11-23 04:22:56