Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6830519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:37:38+00:00 2026-05-26T22:37:38+00:00

So i thought i had the idea of passing by reference down, but it

  • 0

So i thought i had the idea of passing by reference down, but it seems i’m still struggling with it a bit.

Here’s my issue, i’ve been debugging my game for awhile, and i left the enemies section of my code untouched for ages. It’s a long story but i thought my projectiles were doing damage to the enemies, I changed the enemies health to being very high, so i thought they were doing the damage but because of the high health, not killing it. Only now have i realised that they are not doing any damage and that the code is wrong :/

So here is were i begin:

void Towers::Update(std::vector<Enemies>& enemies, SDLib& lib, Map cMap)   

Here i’m passing the enemies by reference, into my update function. Which then i go onto see if there is any enemy within range of the tower;

for (int numOfEnemies = 0; numOfEnemies < lib.numberOfEnemies; numOfEnemies++)
{
    float y =  pow(enemies[numOfEnemies].position.y - position.y, 2);
    float x = pow(enemies[numOfEnemies].position.x - position.x, 2);

    if (sqrt(y + x) < range && enemies[numOfEnemies].alive)
    {
        cEnemy = enemies[numOfEnemies];
        acquiredTarget = true;
        break;
    }
}

cEnemy (currentEnemy), then holds the enemy that’s within range. After this, i then create the projectile to fire, and here is were i think i’m messing up;

bullet = Projectile((float)position.x, (float)position.y - 8, 8, 8, 0, 0, damage, 1, speed, cEnemy);

And the function arguments for that are:

Projectile::Projectile(float x, float y, int w, int h, int sX, int sY, int dmg, int type, float mxSpeed, Enemies bulletTarget)
{
     //....other values set. 
     target = bulletTarget;
}

(Target is the following)

Enemies target;

The idea here is that target should hold the reference to the initial enemy that i set it to via the function… But it isn’t working as i want.
I’m not too sure, but i’m guessing some form of pointer might be required here. It seems to be out of everything the concept i’m finding hardest to grasp *and have done for some time now.

Any help appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:37:39+00:00Added an answer on May 26, 2026 at 10:37 pm

    You are making a copy of the Enemies object:

    • On cEnemy = enemies[numOfEnemies];
    • When passing a value to the Projectile constructor, since it takes an Enemies by value.
    • On target = bulletTarget;

    To fix all of these, you need to consider what code “owns” an Enemies object and makes sure it lives long enough for all code that tries to use it. If there is such a place, all other places should use a reference or pointer. If not, maybe you can give the responsibility to shared_ptr<Enemies>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had thought I was clever. But in light of recent discoveries, I'm not
I thought I had this figured out but it turns out I'm just deleting
Alright, I thought I had this whole setTimeout thing perfect but I seem to
On Monday, I thought I had solved the session hijacking security issue by setting
I thought I had a sql error licked in a post here just a
I am a complete newcomer to Object C but I thought I had the
OK. So I thought I had this licked ... but now .... I have
First I thought that I had to convert JavaScript to PHP, but then I
I had thought I could enumerate the types using IMetaDataImport.EnumTypeDefs and for each of
I had thought about decorating various control items with attributes to declare group ownership,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.