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.

Code Snippets / [GDK] - FreeObject

Author
Message
Seppuku Arts
Moderator
19
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 12th Aug 2012 18:55 Edited at: 14th Aug 2012 21:26
Didn't see a Dark GDK version of 'FreeObject' in here, so now that I've written my own, I'll just post it in here.



Sergey K
20
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 13th Aug 2012 21:20 Edited at: 13th Aug 2012 21:20


make it without any range..
why do you need a range anyways?

Advanced Updater for your games!
Seppuku Arts
Moderator
19
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 14th Aug 2012 19:10 Edited at: 14th Aug 2012 19:11
I didn't originally have a range, but found the code I did have was buggy after I posted (it was returning '1' for two separate objects), so I edited in a range whilst I fixed it so that there was something functional . Now I've managed to fix it, so I can edit it in.




In use:


Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Aug 2012 19:49
My C++ might be a little rusty, but wouldn't the following throw a compiler error on the second statement since you already defined 'id'?

int id = 1;
int id = 0;

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Seppuku Arts
Moderator
19
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 14th Aug 2012 21:24 Edited at: 14th Aug 2012 21:27
That was bad editing on my part (when I edited the post to update it). You'll notice in the second post I didn't have 2 int ids. Cheers for spotting that mistake. It should cause no errors now, or at least it hasn't in VC++ 2010.

To explain the error:
one version had id = 1 with id++ being called at the end of the loop. Then the one I meant to post was with id = 0 and id++ at the start of the loop.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 15th Aug 2012 02:53 Edited at: 17th Aug 2012 14:32
The good thing about FreeObject is that it's a very simple and straightforward function to use, but it's not very efficient since it has to loop every time, so I came up with my own version of it that requires a class for persistent data.

Here's the rough outline (untested as I'm using OSX):



As you can see, allocating an object is as simple as incrementing an integer, however, you might have empty slots when you delete objects, so, when you delete an object, add it to the free list, and if, when you're trying to request an object, the free list has items in it, simply remove the last one and use it. This way everything is done in constant time.

Usage:


It's obviously a bit more verbose but you could make a FreeObject function that wraps that. Or you could do everything with static members but I don't really like that.

"everyone forgets a semi-colon sometimes." - Phaelax
Seppuku Arts
Moderator
19
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 15th Aug 2012 13:54 Edited at: 15th Aug 2012 13:55
Hehe, a more complicated version, but more efficient. Groovy.

For some more usage, you could use 'FreeObject()' to have a more object orientated approach. For example, I have a class called 'object':

It's incomplete, but it's just one way you could use a FreeObject() command.

I stuck my command into a non-static 'FreeData' class.


So you could use the above commands like this:



WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 15th Aug 2012 20:03
I usually use something like this:



Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Mister Fuzzy
13
Years of Service
User Offline
Joined: 27th Feb 2011
Location: XNA
Posted: 17th Aug 2012 02:03 Edited at: 17th Aug 2012 02:04
Interesting. I usually need a range on these, so I do something more akin to:


It's been fun: So long, TGC!
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 17th Aug 2012 13:59 Edited at: 17th Aug 2012 14:32
Yeah I have a similar system to yours Seppuku, but slightly different. Using the singleton model I used up there means you don't have to use up extra space for every object pointing to the allocator. Also, if you use inheritance you can automatically get an object id in the 'object' constructor, and then in the 'cube' class constructor make the cube. The 'object' constructor will be called before the 'cube' constructor anyway so the id will be ready.

Again, untested:



So the 'Object' constructor creates the id and the 'Cube' constructor creates the actual cube after returning from the 'Object' constructor. Notice also the destructor will automatically delete the object and reclaim the id for next time.

Here's how it might be used:


"everyone forgets a semi-colon sometimes." - Phaelax

Login to post a reply

Server time is: 2024-04-25 14:42:54
Your offset time is: 2024-04-25 14:42:54