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 / [Tutorial] Handles

Author
Message
Zotoaster
17
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 30th Sep 2007 01:23 Edited at: 1st Oct 2007 20:09
Level: Intermediate/Advanced


Hello again everyone. Welcome to my new tutorial. This one will be covering something called Handles. Handles are basically a way to 'handle' (suprise suprise) aspects of your program while still keeping control. They make strong use of variables, parameters, types, and advanced arrays, and when used properly, you can build a game engine that can easily flourish into a full feature rich game.

Before we begin, lets look at some DBP examples, to understand what handles are and why they would be usefull.
Let's make a simple cube:


Now, to control that cube, you have to remember that it is object number 1. Infact, if you use this system, you are going to have a whole load of remembering to do. Not only that, but when you assign a number to every object in your game, it will be too stiff and unexpandable. I mean, imagine you have all your game set out just perfect, but then you notice you want to add an extra particle emitter here or a new barrel there? Your game will start falling apart from that very moment - and usually, that moment comes pretty early on in the development process.

This is where handles can start coming in to play. Using them, you dont have to worry about object numbers.


To control this cube, you just have to use MyCube. When you want to change that object number, you just change the MyCube variable. OK, this is certainly better than the previous example, because you can always scramble up the order of the objects and you dont have to worry about the rest of your code, but the thing with good handling is that you dont have to worry about the nitty-gritty stuff that lies underneath your engine.

The most common solution in this case is to make a FreeObject() function. It has been made many times, in many different ways. It is extremelly useful, because all you have to worry about is the variable, and the object orders will wort themselfs out. There is no need to worry using the same object twice. FreeObject() functions just use a simple search technique, which is this case is good, but for other handlers, it can get better.

Here is a very, very basic FreeObject() function.


Great, now you can do this:


Or could even be used further for this:


Now that is really awesome... for object handling. You can create objects without worrying about numbers. It's really good!

Now, ask yourself this, what if you wanted to do this?:


That wouldn't work! Cubes(1) - Cubes(5) would all be exactly the same value because an object wasn't created after each FreeObject() call, therefore it will always return the same number.

This is where you have to know some more advanced array/linked-list techniques. This is how it works. You create a boolean array with no elements. When you call FreeObject(), you increase the size of that array by 1. Get the size of the array, and check if an object which has the number of the size of the array exists. If it doesn't, and the current array item isn't set to 1, then, set it to 1. This will essentially "reserve" the object, so next time, it will move up an array item, and not worry about overwriting the previous one.

Here's how to do it:


Now, this example will work perfectly!

(Note: This will only work is you did not use any other method previously of creating objects)


OK, so we have objects sorted (and ofcourse you can use this for images, sprites, etc). But just because objects are single entities doesn't mean they are the only thing that can have handles!


So let's take it a step further. What happens when you want groups of objects? Would you want to go about giving a handle to every object? I shouldn't think so. (Dont get me wrong, FreeObject() will still be used in this)

Lets say you want to make groups of objects, and each group covered a range of objects. The group itself would have the handle. The details of it is quite simple. A zero array for groups, and a type for the group, which will have two fields: a pointer to the first object it covers, and a pointer to the last one.

So, maybe something like this:


Now you can create a group of objects. MyGroup = CreateGroup( 1, 100 ). Simple. It may not seem very useful yet, but, say you wanted to hae a group of objects say, representing vehicles. You might have an array of vehicles, each with an object number, and, if they were created sequentially, they would be numbered sequentially (assuming you were using that FreeObject() function). You can use this group to perform a function such as delete.



This is where things properly start getting useful. You will notice that when I call the DeleteGroup() function, I pass a handle to the group, that is, the integer 'Vehicles'. Think about it, a whole group of objects.. stored in a single integer. If you are like me, you will find this amazing, because when you compress huge complex structures into something as simple as an integer, you have alot more power, control, and strength in your engine, than if you did trying to do everything manually.

Now, lets move out a little more. Forget about objects. You can have a handle for anything. Lets make a handle for people data.


Very basic example, but it's there to prove that you dont need objects to work with them. You can have a handle for simple data.

The reason I used people in this example, is because you can easily build on it. When I was making my Lunar Lander game (which worked with planets rather than flat landscapes) the worlds were built as such: Tiles -> Landscapes -> Worlds. Each world had a certain amount of landscapes, and each landscape has a certain about of tiles, and each tile had some associated data. All had handles. In the same way, you can use people, i.e. people -> town -> country -> planet. This is, where you have handles for people, but you can also have a handle for a town which has people in it, and so on.

I will only go up one level in this (assume I have the previous people code in here still):


This can be further created to incorporate whole countries, then planets, even solar systems! Of course, none of it is visible, but that's not the point. The point is, that if you did infact want to make a game such as this, you would have to have some way of handling all your people. So if you did infact go as far as slar systems, you could infact number every person's object number, but I wouldn't recommend that. You could use an array for each person and use FreeObject(), but I wouldn't recommend that either. You could use this method, where you have control over whole solar systems, just by having access to a single little integer.

I hope you have understood this tutorial, and found it useful, because I dont have the brain capacity to explain how much I have. It is a very useful way of doing things, and all it requires is a little knowledge about types, and some slightly advanced array commands, but, not that advanced

Cheers

C&C Please

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
Sasuke
16
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 30th Sep 2007 12:54
Anthor super tutorial, thanks Zotoaster. This was what I was looking for, I made a button thing awhile and wasn't sure if I was using handles correctly:

The code:


You've given me so many idea's on how to improve this, so thanks again and keep up the great work
Visigoth
17
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 1st Oct 2007 05:14
yes, good tutorial. Good job
Mr Kohlenstoff
16
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 1st Oct 2007 12:55
Good tutorial, but I think you made a little mistake (maybe I'm wrong and just don't get that you already solved the problem, but I don't think so ):

Handles and the Array Delete Element-Command don't work with each other, because all handles, bigger than the deleted element are wrong after deleting one, because the elements the handles point to are all reduced by 1 then. I always use an "exist"-boolean in my UDTs which is set to 0 when I delete an element, and my FreeWhatever-Function first looks for existing array-elements with an exist-flag equal to 0. If the function doesn't find one it creates a new element, otherwise it sets "exist" to 1 and takes the existing element...

Apart from that good job, I often used handles, but never such a Group-Method, it might be quite useful.

Aralox
15
Years of Service
User Offline
Joined: 16th Jan 2007
Location: Melbourne
Posted: 1st Oct 2007 13:12
Very nice tutorial Zotoaster, even as it gets more and more complex its easy to follow. I like it

Zotoaster
17
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 1st Oct 2007 20:00
Mr K - Well, spotted the delibarate mistake eh? ...
Oh well, I rarelyever use deleting for some strange reason But now that you happened to mention it, I will have to improve on that part, thanks alot

Everyone,

Thanks I hope you find them as useful as I have.

"It's like floating a boat on a liquid that I don't know, but I'm quite happy to drink it if I'm thirsty enough" - Me being a good programmer but sucking at computers
qqwy
14
Years of Service
User Offline
Joined: 3rd Jan 2008
Location:
Posted: 7th Jan 2008 10:01
APPLAUSE

thank you for this tutorial, zotoaster.before i was endless thinking of a good nummering for the objects(where youre objectnrs end, and the objectnrs of enemies begin)

this is VERY helpful for me

parachute for sale, once used, never opened, slightly red stain

Login to post a reply

Server time is: 2022-12-01 11:32:18
Your offset time is: 2022-12-01 11:32:18