Jump to content
  • Advertisement
Sign in to follow this  
Jemburula

Game theory

This topic is 4723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I wondered for a little while how linked lists and a dynamic number of enemies in my programs would work. Say the user clicks a button to create a new unit and the new unit is created... But how do you do this? How do you have an unpredictable number of units. I thought vectors or using a linked list but I didn't really understand linked lists and thought there must be another perhaps simpler mainstream way people do this. So if someone could enlighten me or post some linked list tutorials it would be great. Thanks for your help much appreciated :) -Jemburula

Share this post


Link to post
Share on other sites
Advertisement
Create a new object yes... but how would the program know what to call it if it isn't some array. That's where I get confused!

Share this post


Link to post
Share on other sites
Put it in a linked list then? [grin]

Use recurison to go through and update all objects alive.

You could also do it in a circular array assuming the object has a life span and dies eventually.

This may be less ideal then a linked list; im pretty sure thats the only way it would be done?

With the exceptions of a stack or something simliar to a linked list.

Share this post


Link to post
Share on other sites
Ok so with a linked list the next object is pointed to by reference? Therefore they do not need a variable name?

Share this post


Link to post
Share on other sites
Although you could use std::list<>

eg->
This is off the top of my head, apologies if its wrong.

#include <list>
#include <iostream>


struct myObject {
int data;
};

int main()
{
std::list<myObject*> objList; //list of game objects.

for(int i = 0; i < 100; i++)
{
myObject *obj = new myObject;

objList.push_back(myObject);
}
}




The above will create new objects in a list, 100 of them.

You could write a function, to add objects to a list, iterate through that list and render the new objects, or update them.


Hope this helps.

Share this post


Link to post
Share on other sites
Oh it's ugly, but sometimes there's nothing more gratifying than a global array of enemies. :)

It all depends on your game design, etc, but global static variables are people too in this crazy OO world we live in..

You may find that there's an upper bound of enemies that slow your frame rate to a crawl...

hth

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!