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 / [DBP / DBC] Finding an unused object/image/etc number

Author
Message
Lucy
17
Years of Service
User Offline
Joined: 19th Apr 2007
Location: Roanoke, VA USA
Posted: 6th Sep 2007 20:45 Edited at: 6th Sep 2007 20:50
Here's the function itself. It returns a 0 if it doesn't find a free object (Which should technically never happen, but you can never be too careful.)



If you want to adapt it for image numbers, sound numbers, or whatever, just change getnewobjnum() to something like getnewimgnum() or getnewsndnum() or whatever kind of casing and spelling you want on the function. Then change "object exist(i)" to "image exist (i)" or "sound exist (i)" etc. And now you never have to worry about object/image/sound/memblock/etc management ever again! Just make sure you clean up after yourself by properly unloading everything when you're done with it.

Here's how to use it


Nothing I say is intended to be rude. My autism means that I do not know what is rude and what isn't rude. I apologize if I seem rude. It is not my intention.
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 7th Sep 2007 16:11
Hmmm.. no offense but I think there are "hundreds" of this functions in the codebase, and even included in one of IanM's (Right?) Plugins. Also your function looks a bit complicated.
Most versions look somehow like this:



or "more advanced":



.. which will return the ID of the first of Count objects, searching from First to Last. (Didn't test it..)

However... I don't really see how your function was able to return the value 0 when nothing is found, because it searchs for a free object until something is found, and doesn't leave the loop before. Sorry if your function has another feature I just didn't notice, so that it's better then the others, and also sorry, that my post sounds so negative.. so, finally -> good work.

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 7th Sep 2007 16:22
Here's how I would have used it.


I don't understand the second part as you say "newobjnum# = getnewobjnum()", why would the function return a real number, and also this variable name doesn't help the user as the "new object" could be anything.

I'm only being so negative because I've seen other stuff from you and it was much better than this

In programming, nothing exists
_Octopus
17
Years of Service
User Offline
Joined: 29th Aug 2007
Location:
Posted: 7th Sep 2007 21:12
Hi just reading through your post and it gave me an idea. I've made similar finding first object etc. functions before, but I just suddenly realised what you can do with this if you turn a few things around.

With a second function you can make DB (well Pro definatley, not sure about classic) act almost in an object orientated fashion!

_Octopus
17
Years of Service
User Offline
Joined: 29th Aug 2007
Location:
Posted: 7th Sep 2007 21:31
Oh and obviously you can easily mke a set of those for making the different primatives, or loading objects, as well as other sets. (images, sounds etc.)

The best thing about it is you can set an array of enemies, and refer to them as such - enemy(1), or enemy(1).obj if your using types.

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 7th Sep 2007 22:46
Some time ago I wrote some functions for DBP, making it "more objectorientated". You could create primitives, load objects etc. with such functions and apply a function for each object, which is checked with a select-case-block..

So basically you could do things like this:





However... umm... Maybe we should get back to topic.

I read some months ago, that the function might be faster, when you do it like this:



_Octopus
17
Years of Service
User Offline
Joined: 29th Aug 2007
Location:
Posted: 7th Sep 2007 22:55
Ah yeah, the thing I wrote seems to just be a combination of the 2 (I assume the first paramter of MakeCube() was the object number to use)

The last thing you mentioned with the rnd(30) is interesting. It will definately find a free object quicker, but you'll end up with large gaps, and using higher object numbers. If you run a couple of quick timer tests, you can see that if you make one object in slot 1, the program runs (slightly) faster than if you make the same object in slot 50000. I think it's cos the has to then spin over more blank objets before it finds one to render or something.
_Octopus
17
Years of Service
User Offline
Joined: 29th Aug 2007
Location:
Posted: 7th Sep 2007 23:02
Oh sorry, getting back to topic...
Forgot about that bit

I think that must be the fastest basic way. The only othe thing I can think of is having a array of assigned objects (which would mean you could carry some data with them - name,type etc.) and running through that.

Depends whether object exists, or the prescence and use of the array are slower I guess.. hmmm.. I'll run some tests.

Off the top of my head though I think object exist will always take the same amount of time to perform. With the array method, the more objects you have, the slower (and more memory hungry) it is..
Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 9th Sep 2007 16:41 Edited at: 9th Sep 2007 16:55
I used to use a system like this:



until I bought eXtends, which handles all that for you. I'm not sure about the exact algorithm, but for example when you call the eXtends b3d make object cube function, it increments a global variable and returns it, and because that value has never been returned it means the object never existed. It's also faster than checking if objects 1 through x exist.

I use object oriented programming like _Octopus's example, except I put all entities in a single array, including the player and allies. An idea I took from eXtends is that you can make object oriented systems that hold a list of objects to constantly update. In my current project I have a system that will put a shadow underneath all objects in the list, all I have to do is call shadow_new(object) and the shadow system will constantly position a shadow underneath the object. I try to keep it as automatic as possible, for example it's safe to delete the object casting the shadow because on the next update the shadow system will see that the object doesn't exist anymore so it gets removed from the list and deletes the shadow. When calling shadow_new() the system will also figure out how big the shadow needs to be.

Login to post a reply

Server time is: 2024-11-22 18:52:24
Your offset time is: 2024-11-22 18:52:24