Removing drawn entities.

Need help in general for programming? Ask here.

Removing drawn entities.

Postby Nishok » Wed May 02, 2012 9:09 am

Hey guys,

I need help on how to remove drawn entities. As of now I tried using the following:

1) Surf_Entity = NULL;
This just makes the entity vanish and never return (I made a projectile which you can shoot, but once I trigger the "Surf_Entity = NULL", it removes the entity but it never draws it again if I shoot again).
But what also occurs trying to do it through this way is that when the Projectile is going any direction (using the MoveLeft, MoveRight, etc from your tutorial) it keeps on going even if "Surf_Entity = NULL" is called. The entity only disappears when I move an entity (For examply when I move my player or an NPC moves).

Any Idea how to make it disappear correctly, so I can redraw it again?
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Removing drawn entities.

Postby Aaru » Wed May 02, 2012 9:59 am

Yes, I can help you with this. Actually if you browse the Topic bullet factory. We had a huge discussion of the sort. Towards the end you will see how we eventually came to remove Entities. If you're still lost I can walk you through it as soon as I get on break!
User avatar
Aaru
 
Posts: 72
Joined: Fri Sep 10, 2010 3:56 am
Location: California

Re: Removing drawn entities.

Postby Nishok » Wed May 02, 2012 11:38 am

I sent you a PM ^^

Will post the end result if everything gets solved. Just trying to keep the thread clean.
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Removing drawn entities.

Postby medicCoder » Thu May 03, 2012 12:38 am

I haven't been keeping up with the forums recently so not sure of any other way that might have been thought of for this but for these tutorials on this sight I think I may have a really easy way of doing this.

Add a bool variable to CEntity that keeps track of rather or not a current Entity is Active.
Set the bool variable to either true or false as needed and only update/draw the entity if the variable is true.
medicCoder
 
Posts: 43
Joined: Sat Aug 27, 2011 11:01 am
Location: TN, USA

Re: Removing drawn entities.

Postby Nishok » Wed May 09, 2012 3:01 am

Aaru, I understand the whole for loop with the iterator and all, but sadly I can't get it to work correctly because I don't think I'm placing it on the correct spot.

When I place it in my Projectile's OnCleanup function, it crashes the game as soon as my projectile hits the distance limit (Because it keeps calling for Oncleanup() inside the function,so it never leaves that loop basically).

When I add it to the Projectile's OnLoop function, it simply removes the entity, but also never draws it back whenever I 'shoot' again.

Can I have a bit of help from here please?




Code that looks if the projectile has hit it's distance limit:

Code: Select all
while(PPosX <= (X - 250)) {
                MoveLeft = false;
                Dead = true;
                OnCleanup();
                break;
}




And here is the code that should remove the projectile from the EntityList but also remove the drawn projectile too:

Code: Select all
for(std::vector<CEntity*>::iterator it = CEntity::EntityList.begin(); it != CEntity::EntityList.end();) {
        if(!(*it)) continue;
        if((*it)->Dead == true) {
                (*it)->OnCleanup();
                delete (*it);
                it = CEntity::EntityList.erase(it);
        }
        else {
                it++;
        }
    }



NEVERMIND: I have placed that cleanup code inside the CApp_OnCleanup.cpp file and all is working perfect. Thank you very much.
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Removing drawn entities.

Postby Nishok » Wed May 09, 2012 3:15 pm

Sorry for the double post, but I thought I'd keep my previous code clean so other people can easily find a solution if they have the similar problem like I had above.

Sadly I ran into another problem..

Code: Select all
int PPosX = X;
int PPosY = Y;
if(app->AccuierePlayer().Direction == 'R') {
       MoveLeft = false; MoveDown = false; MoveUp = false;
       SpeedY = 0;
       int PlyX = app->AccuierePlayer().X;       //The player's position
       int PlyY = app->AccuierePlayer().Y;
       X = (PlyX + 35);       //The player's position + 35, because if I don't add this, then the projectile will spawn inside the player
       Y = (PlyY + 10);
       SpeedX = MaxSpeedX;
       MoveRight = true;
       while(PPosX > (X + 250)) {
              MoveRight = false;
              Dead = true;
              break;
       }
}


This code is inside my OnLoop function for my projectiles.
For some reason when the projectile is moving, it does not get removed when it reaches X + 250 (So basically if I shoot the projectile when I'm standing at X = 500, then the projectile should automatically get removed when it reaches X = 750).

I hope you understand what I mean, if not then I'll post the project so you can see the game yourself and notice what I mean.
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Removing drawn entities.

Postby Tim » Fri May 11, 2012 5:53 pm

If this is inside your OnLoop, then it looks like you are setting the value of PPosX each time, and X is going to update also when OnMove is called. PPosX needs to be a member of the class and set once, and then you should just do something like:

Code: Select all
// Has moved 250 pixels away in any direction
if(abs(PPosX - X) > 250 || abs(PPosY - Y) > 250) {
}
User avatar
Tim
Site Admin
 
Posts: 255
Joined: Tue Oct 28, 2008 9:55 am
Location: Indiana

Re: Removing drawn entities.

Postby Nishok » Fri May 11, 2012 7:02 pm

Alright, I think I'm in the right direction thanks to your explanation. I'll see how I can fix this. I'll update this post on Sunday, because I won't be able to code until then.
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Removing drawn entities.

Postby Nishok » Thu May 17, 2012 3:44 pm

Alright, I got everything to work except for the redrawing of it.

I currently made it so that when it collides with an entity, it should remove it (this works),
and normally when I press space again, it should redraw it (this doesn't work).

The oncollision code (inside Projectile.cpp):
Code: Select all
bool CProjectile::OnCollision(CEntity* Entity) {
    Dead = true;

    return true;
}


The removal code (inside CApp_OnLoop.cpp):
Code: Select all
for(std::vector<CEntity*>::iterator it = CEntity::EntityList.begin(); it != CEntity::EntityList.end();) {
        if(!(*it)) continue;
        if((*it)->Dead == true) {
                (*it)->OnCleanup();
                delete (*it);
                it = CEntity::EntityList.erase(it);
        }
        else {
                it++;
        }
    }


And here is the code that makes the projectile fire (inside CEntity.cpp):
Code: Select all
bool CEntity::Fire() {
        CProjectile &Projectile = app->AccuiereProjectile();

        if(this->Direction == 'R') {    //Get the player's direction (in this case the player is looking to the right side)
            Projectile.MoveLeft = false; Projectile.MoveDown = false; Projectile.MoveUp = false;     //Stop the projectile from moving, otherwise it'll move into all directions.
            Projectile.SpeedY = 0;     //Set the speed of the projectile to 0.
            Projectile.X = (this->X + 35);  //Get the current player's position and add 35 to X, so the projectile doesn't get stuck inside of him
            Projectile.Y = (this->Y + 10);  //Same for the Y-axis
            Projectile.SpeedX = Projectile.MaxSpeedX;   //Set the projectile's speed to the maximum speed
            Projectile.MoveRight = true;    //Launch the projectile right
            cerr << "Test" << endl;    //Just to see if it actually gets to this part, and it does.
        }
}



Can anyone help me with this please? Because I need the projectile to be redrawn :/
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Removing drawn entities.

Postby Aaru » Fri May 18, 2012 2:00 pm

The reason why you are only getting one projectile, is because you are only creating one projectile. You need to allocate memory for a "new" object every time you shoot. In other words:
Code: Select all
Entity::Onfire()
{
// Create a new Projectile
new Projectile(int X, int Y, speedX, speedY); // whatever constructor you have

}

// And on the code that you used from the other topic where we remove "dead" EntityList Objects:
// you need to De-allocate that memory, which you are.

for(std::vector<CEntity*>::iterator it = CEntity::EntityList.begin(); it != CEntity::EntityList.end();)
{
        if(!(*it)) continue;
        if((*it)->Dead == true) {
                (*it)->OnCleanup();
                delete (*it); // Here, is where the Object is being erased from memory
                it = CEntity::EntityList.erase(it);
        }
        else {
                it++;
        }
    }


If, I read your code correct. You are basically only making one object, and then deleting it. So that is why you only get one projectile. Hope that helps.

-Aaru
User avatar
Aaru
 
Posts: 72
Joined: Fri Sep 10, 2010 3:56 am
Location: California

Next

Return to Help

Who is online

Users browsing this forum: No registered users and 0 guests

cron