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.

Newcomers DBPro Corner / Best way to find array index number?

Author
Message
Deet
14
Years of Service
User Offline
Joined: 15th Mar 2010
Location:
Posted: 16th Mar 2010 04:52
When comparing objects in a collision, I found that I wanted the array index number associated with the colliding objects so I could use that parameter in functions. I came up with this method, which doesn't seem as direct or quick as it could be.



The part that finds the array index number (saved as Omega) is super-indented, the rest is so it runs as a program. It works fine. Is there a better way to do this though?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th Mar 2010 15:24
You should probably use a for/next loop instead of a repeat/until loop. You could also put the search into its own function.

Something like this:


Deet
14
Years of Service
User Offline
Joined: 15th Mar 2010
Location:
Posted: 16th Mar 2010 21:46
Thanks. I was wondering if there was a more direct way than scanning through the entire array, guess not.
Masqutti
14
Years of Service
User Offline
Joined: 8th Jan 2010
Location: insanity
Posted: 18th Mar 2010 12:43 Edited at: 18th Mar 2010 12:46
Well this isn't "universal" But if you have such a calculation going on that determines values inside the array and is always constant, you can avoid all the checking and just calculate what's the Index Number of a raw value.

As you determine index number's value to be Index number * 5, you can just:

Raw Value = Alpha
Index Number = Alpha / 5

This work in your example and erases alot of CPU usage (even more if the array is huge). Sometimes you could try to overcome the issue by doing things backwards. Let's say there's a situation where you need 100 bullets. You make an array for them like

Next step is to determine the object id for every bullet.

Now this works, but if you have to search the object id inside the array You'll have to do exactly what you did on your example, or how IanM told. That's ok if you have 100 array items. What if you have 10000 or even more. You could have an array holding 100000 items, then looping that thru in 1 frame would make noticeable FPS jamming for a while. I would forget the array "raw value" that holds the ID, and using the actual Index number to be the value you're looking for. Like this(similar to the first solution I gave):



Like this, also made possible because the objects are sorted. Ofc this isn't possible anymore if the values are not in order. Well.. That's why I'm trying to use an order and sort out everything that's possible. a computer is a calculator, so do things by calculating them ;D I also find that sorted info is the best way to approach basicly everything

hmmmh.. that didn't compile
Deet
14
Years of Service
User Offline
Joined: 15th Mar 2010
Location:
Posted: 18th Mar 2010 22:55
Sorted information is good like that, but I'm using functions that create and delete objects, and the object numbers won't always be the same inside the array. It makes more sense to me to have one object cloned and the clones deleted after use than to have a dozen models ready to go and be recycled.

Login to post a reply

Server time is: 2024-11-24 18:05:05
Your offset time is: 2024-11-24 18:05:05