Jump to content
  • Advertisement
Sign in to follow this  
fshmt

Question regarding game mechanics and dynamic allocation

This topic is 1004 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

Hi everyone, I'm learning C++ and I have a question - 

 

say i'm making an Invaders game, and if the user hits spacebar, a "fire weapon"  function (lets call it fireWeapon(); ) is called.

 

so I guess i'd have to use a new pointer here? say I have a bullet class with coordinates and other stuff, in the fireWeapon() i'd go:

 

Bullet bullet = new bullet, 

 

 

my question is really simple, how to I handle this new bullet? i know that since it's new, it won't go out of scope when the function is ended, but should I be returning the pointer from fireWeapon? or passing it to a different function? 

 

and say the bullet is not deleted, then i'm firing another bullet and there are 2 bullet objects - where should i "store" them after declaring them? i'm sure i'm missing something, its a bit confusing to me :P

 

thanks and sorry for the bad english

Share this post


Link to post
Share on other sites
Advertisement

if you're spawning the bullet from your fire weapon function with the above line then the bullet variable is local and when the function ends will no longer exist (although the bullet you created using new will still exist with no way to access it).  That would be a memory leak unless you destroy the bullet before you exit the function which isn't very useful or practical.  If you create a list of bullets you could add the bullet to it before you exit the function then you could dispose of the bullet at the appropriate time.  That would be one way to deal with it.

Share this post


Link to post
Share on other sites

You could also make the observation that your weapon likely has an ammo capacity, which you could pre-allocate at start up or creation time, and simply fire one out of the list placing it in a "used" or "fired" list of some sort and get a new one next time from the unused/loaded list. Once a used bullet dies, hitting an object or traveling too far, recycle it back into the unused list.

If you need infinite ammo, your weapon probably has a rate of fire, and the bullets probably have a max travel distance limit, you could again use this info to allocate the max bullets you might ever need, and just keep firing and recycling.

What you can do is sorta dependent on what youre making, but it could be as simple as pre allocating an array of bullets, and maintaining a fired/loaded link list of bullets where each bullet has a pointer to the next, where next is either fired or loaded depending on which list the bullet is in.

Share this post


Link to post
Share on other sites

oh, I see.. so if I understand correctly, there's no way to do this without a list?

i mean I guess i could do it with something like Bullets[99] but that would be pretty lame :P and pretty bad i guess if there were >99 bullets on the screen.

 

i guess for some reason I thought there was a dynamic way to do this using pointers only.

 

so saying that in my game status updating class i have a list of bullets, I would need for each game loop to check each bullet instance for coordinates and collision detection, etc, am i getting this? 

 

thanks a lot!

Share this post


Link to post
Share on other sites

You could also make the observation that your weapon likely has an ammo capacity, which you could pre-allocate at start up or creation time, and simply fire one out of the list placing it in a "used" or "fired" list of some sort and get a new one next time from the unused/loaded list. Once a used bullet dies, hitting an object or traveling too far, recycle it back into the unused list.

If you need infinite ammo, your weapon probably has a rate of fire, and the bullets probably have a max travel distance limit, you could again use this info to allocate the max bullets you might ever need, and just keep firing and recycling.

What you can do is sorta dependent on what youre making, but it could be as simple as pre allocating an array of bullets, and maintaining a fired/loaded link list of bullets where each bullet has a pointer to the next, where next is either fired or loaded depending on which list the bullet is in.

 

yep I this also sounds nice! 

I really didn't get too much into lists so I guess it's time to do that..

thanks:D

Share this post


Link to post
Share on other sites


i guess for some reason I thought there was a dynamic way to do this using pointers only.

You can a list of pointers.  I use the term list loosely.  If I were you I'd implement my own linked list class for this just for the experience.

Share this post


Link to post
Share on other sites
Use a std::vector. You don't have to set a maximum amount and you will reduce the number of actual allocations that have to be made.

Calling new inside the game loop can be a performance issue, although you wouldn't notice in a simple game. std::vector will reserve memory and only reallocate when it has to.

Share this post


Link to post
Share on other sites

my question is really simple, how to I handle this new bullet?

The answer to that question is in the code around the 'fire' function.

 

 

In a game, you basically do

while not dead do
  read user input
  update stuff in the game (position, damage, etc)
  draw stuff for the player
end

This loop implies you have "stuff" available at global game level. "stuff" here is the player, the enemies, and all things that fly around, like bullets and explosions.

 

Each iteration you update all active elements, where it is possible that there are 0 bullets in the game at that time (which is very easy to update). In other words, the game knows there are 0 or more bullets in the game at all times, even before the player presses the space bar.

 

The 'fireWeapon' function operates in this context. It creates a new bullet, computing the initial position (and perhaps direction or velocity). Then it adds the new bullet to the global collection of "stuff" (which is often several lists, eg one list with enemies, one list with bullets, one list with explosions, and one player).

The "update" function above then handles updating bullet position each iteration, and eventually deleting the bullet from the game objects, when it flies off-screen.

Edited by Alberth

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!