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.

Dark GDK / Best way to control bullets/AI/ etc..

Author
Message
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 5th Nov 2008 23:31 Edited at: 5th Nov 2008 23:33
Hiya, so I've been using Dark Basic for a while ( I just switched to DarkGDK ), and I got to the stage in my game where I apply physics and crap to all of the spaceships, and handle all of the bullets, etc.

I was wondering if there was a more efficient way to do this, other than using a for loop. Right now i have something like this



I am wondering if there is a better way to do this? This is the way I've always done it, but I can't believe that big games like FEAR or something just use a for loop to run through all the AI, etc.

Is there some way to just refer to all the elements in an array without using a for loop?

The problem is that often, bullet[25] or something will hit a wall, and there might be say, 100 bullets active at that moment. This means that the program will still run through 100 loops, even though bullet #25 doesn't need to be controlled. In a more drastic case, say bullets 0 through 99 are dead, but bullet[100] is alive. My loop would still have to run from 0 to 100, which is 99 wasted cycles.

How do you guys handle bullets and AI and things like this?

Thanks

IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th Nov 2008 00:16
Quote: "but I can't believe that big games like FEAR or something just use a for loop to run through all the AI"

Why not? That's exactly what they do.

However, they will probably loop through a linked list of active items rather than an array of all items where some of them may be active.

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 6th Nov 2008 03:11
Huh, interesting xD.

When you say a linked list of active items, are you talking something like a separate array which points to the active objects?

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 6th Nov 2008 03:12 Edited at: 6th Nov 2008 03:14
I usually use a handler class that has a std::vector of all the objects under it, and has functions named "CreateNew()" and "Update()".

When you call Update(), it just updates all the objects in the vector array. When CreateNew is called, it inserts a new object to the vector. The vector has to be a vector of pointers, though, not just the object. At least, with my method. You have to use "new" to assign them, or they get deleted when the CreateNew method ends.
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 6th Nov 2008 03:54
std::vector you say, I don't know much about c++, I'll have to research that, it sounds nifty.

sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 6th Nov 2008 04:05
It all depends upon how complex you want to make the code.
Some other very simple alternatives can be like these.

The simplest way is to expect the game to run at full speed with every object active.
Therefor there is no requirement to not to process inactive objects.

Another slightly more complex way is to have an active/inactive data entry for every object in its data structure.
Although you would still scan through each object data structure, you first test if it is active before you process its actions. If it is inactive you just jump to the next object. I tend to use this approach for things like tables for projectiles (machine guns etc).

Then you can migrate to things like linked lists that have a little more overheads but provide better gains in saving on processing of data.
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 6th Nov 2008 14:28
Hmmm.

Linked lists are provided by the STL (specifically the double-linked list).

You have one list that contains the items that are active, and another list that contain the inactive objects that you don't want to worry about ('dead' objects) - it's pretty cheap to move objects between lists (using the splice member function). Or you can forget about the inactive list and swallow the costs of memory allocations when you create new items for the active list.

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 6th Nov 2008 18:19
Cool Thanks guys!

I'm gonna go study up on c++ some more so that I can understand this, it sounds like it's what I want though.

Login to post a reply

Server time is: 2024-11-25 05:17:36
Your offset time is: 2024-11-25 05:17:36