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.

DarkBASIC Professional Discussion / Matrix1 - Make Freelist commands

Author
Message
Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 21st Sep 2014 04:26
Hey people,

I can't find any examples or theory on the Matrix1 Freelist commands on these forums. Has anyone used them before? Does anyone have time to put together a small example, and touch on their advantages/disadvantages? I'm curious in the ways that I can start using them, possibly for object properties & action queues... but I'm not too sure.

Maybe an example that works with lists/queues of colours that change the colour of an object? Not too sure...




Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 21st Sep 2014 11:40 Edited at: 21st Sep 2014 11:44
A freelist is used to return an unreserved identity number from a sequencial list. These identity numbers can be used to provide unique identities for similar resources in your software. These numbers can also be used to represent other numerical representations, where such numbers can be reserved or consumed.

Freelists share similarities with the Matrix1 'Reserve Free' functions, the difference is that freelists are not exclusive to DarkBASIC resources such as images or sprites.

A freelist can contain numbers from 1 to the highest number the freelist can return which is 2147483646. Freelists can also be limited to a smaller range from the lowest and highest figure you specify.

A freelist identity number is either free or reserved. When a freelist ranging from 1 to 10 has no reserved identities, the following command returns 1:


GET FROM FREELIST( FREELIST_ID ) --> 1


When a freelist ranging from 1 to 10 contains no reserved identities, the following command requests any available number from 5 with the range set by the range parameter, starting with just 1 number. Since all numbers are free, it will return the first number tested which is 5


GET FROM FREELIST( FREELIST_ID, 5, 1) --> 5


The identity number you obtain using the GET FROM FREELIST command is reserved; therefore you will not get this ID number again until it is returned to the list. The following commands return different numbers because of this.


GET FROM FREELIST( FREELIST_ID ) --> 1
GET FROM FREELIST( FREELIST_ID ) --> 2
GET FROM FREELIST( FREELIST_ID ) --> 3


If you had a freelist limited with the range of 1 to 2, the third time you run the previously demonstrated code you would not have the number 3 returned because it is out of range, and all numbers have been reserved.

When all numbers have been used; a default number is returned instead, which is called the 'Not Found' number. The default number will be zero, unless you specify otherwise with the MAKE FREELIST command.

You can return a reserved identity back into the freelist using the RETURN TO FREELIST command.



I use the freelist to generate identities for my 3D entities in Sports Fiction; I am planning to use it to make random pools for returning random consumable numbers for fictional sport features.

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Sep 2014 00:12
I see, so, I can use the Freelist set of commands for grabbing a unique number from a range to then assign for use within my application. Such as, when calling Windows DLL files, I assign them manually like;



Instead, I'm thinking that could simply create a freelist function to manage the index of my DLLs.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
ShellfishGames
13
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 22nd Sep 2014 13:26 Edited at: 22nd Sep 2014 13:29
I mostly use freelists for dynamic arrays. For instance, if I'm working on a shooter, there's a bullet array - bullets are created and deleted very regularly (we're talking about actually existing bullets here, so ones that don't hit the target instantly but fly around for a few frames until they hit something) and I store them within a bullet() array.

The easiest way to deal with this array would be to use array insert at bottom bullet(), 1 when creating a new bullet and array delete element bullet(), ID when deleting one. However, this is not only slow, it also messes up the IDs, which can be bad if, at some point in your code, you've got a reference to one of the bullets via its index in the array (for example, the weapon that shot the bullet stores a refernce to the last released bullet to be able to determine whether it has hit anything yet).

Alternatively you could add an exist flag to the bullet type. When a bullet is deleted, bullet(ID).exist is set to 0, and all the update and draw functions for bullets will only be executed for bullets with .exist = 1.
When creating a new bullet, you would look through the whole bullet array for a non-existing bullet, set its exist flag to 1 again and set its properties.
This way all the IDs stay constant and you don't have to mess around with the actual array.
Still, this method is pretty slow because for every new bullet you have to look through the whole array.

In this case, it's not a bit deal given that in most games there won't be more than a couple of bullets existing at any given moments, hence searching the array will be very quick - however, there might be situations where this is not the case, and you've got hundreds of elements of some kind. In this case a freelist comes in quite handy as it reduces the complexity of finding a free element to O(1) - it basically works instantly, no matter how big your array is.

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Sep 2014 14:04
Chris & Shell, I can't thank you people enough for helping. My initial thoughts were correct, in that Freelists are great for managing... well, possibly everything! Below is my first crack at using them, and they work a treat. Now I've got to get rid of my old naming scheme, as there aren't any #Constants in use any more!



I also had a bit of a lawl when coding "Find Free Freelist()", very inception. It's also got me REALLY thinking too, clearly my "Load DLL" routine above is crying for a function to run 3 times... HMMMmmmm.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Stab in the Dark software
Valued Member
23
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 22nd Sep 2014 18:18
Just a suggestion why not use the Matrix1 command "Find Free DLL()"?



Just out of curiosity what are you using those dlls for?

[img][/img]


WindowsXP SP3,Vista,Windows 7 SP1, DBpro v7.7RC7
Stab In The Dark Editor
The coffee is lovely dark and deep,and I have code to write before I sleep.
Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 23rd Sep 2014 00:12
Yes, I'm putting lots of thought into whether I use the specific "Find Free *" or just use the Find Freelists for everything. I suspect both methods are probably equal, maybe a subtle difference here or there.


As for my DLL usage, I'm using them for;

Desktop Mouse Coords and click detection
Running DBPro App window in a processor friendly state
Keyboard detection to handbrake the application in CTRLALTDLT event, and eventually restore without crashing
Drag & Drop of folder & file data from Windows explorer into the application.

All of the above works great! All leading up to a game engine that won't need to be recompiled, but rather, just update the game mechanic script/table, drag & drop it into the app, and done!

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 23rd Sep 2014 20:08
Nice idea Shellfish games!

Login to post a reply

Server time is: 2026-07-18 07:32:06
Your offset time is: 2026-07-18 07:32:06