Page 1 of 1

How to destroy actors?

PostPosted: Thu Dec 29, 2011 6:59 pm
by TGKG
I have a shooting game where after I shoot say 20 times (and now have 20 of these shot Actors in the game), I want to be able to fade out and destroy the first shot that was fired and so on.
Thus when I fire the 20th shot the 1st is faded out and destroyed,
when I fire the 21st shot the 2nd is faded out and destroyed,
fire the 22nd and the 3rd is faded out and destroyed.

You get the idea. I have tried using a timer and counting shots, etc. and am stumped. Any ideas and help is appreciated.

Re: How to destroy actors?

PostPosted: Thu Dec 29, 2011 7:22 pm
by skydereign
Is the fading out/destroying purely based on the shots fired? Or will they naturally fade out, even with less than 20 bullets fired, and you were just specifying the rate that they disappear? It sounds like the first meaning there will always be 20 bullets on the screen (based off your attempt). In that case you could do something like this.
bullet -> Create Actor -> Script Editor
Code: Select all
xvelocity=10; // or whatever you use to make your bullet move
if(ActorCount("bullet")>=20)
{
    DestroyActor(bullet.clonename); // this gets the clonename of the lowest indexed bullet and destroys it
}

This isn't a very elegant way of doing it, because if your bullets get destroyed by other means it won't work. But given what you have said about it, I would need to know more to make it better. And if you happen to mean naturally fade, than you can just use draw for that (since you want them to fade). When they've faded to the point where your 20th bullet has been created, destroy it. All you need to do is calculate how long that is.

Re: How to destroy actors?

PostPosted: Fri Dec 30, 2011 2:55 am
by TGKG
Good points. I will try to explain better.
Every time I click the mouse a new "bullet" is created, it runs a 9 frame animation, stops and remains on the screen. The more mouse clicks the more "bullets" on the screen. If I get too many "bullets" on the screen the game slows down (because of memory overflow??), hence I guess I need to destroy the "bullets" after a certain number have been created. I have choosen 20 bullets as my limit, but could be another number. Rather than just delete the "bullet" I would like it to fade away and then disappear/be destroyed. If less than 20 "bullets" are created then they will stay on the screen forever.

Answers to you good points:
The "bullet" actor does not fade out on its own. It will remain on the screen if I do nothing. The "bullet" actor is made up of a multiple frame (9 frames) actor that when created runs the 9 frame animation and then stops on the last frame using the "Animation Finish - Stopped" event.

I have not used the clone feature before so I don't know anything about using this. I will investigate this further.

The "bullets" do not move using xvelocity or anything like that, the 9 frame animation is their movement.
The "bullets" do not get destroyed by any other means.
I have been using the "Change Transparency" function to make the bullets fade out and when the transparency is fully transparent I destroy the actor.
I have tried counting the number of bullets, however I did not know how to keep track of the order of the bullets so that I can destroy them in order.
I have tried using "Create Timer" each time a "bullet" is created and then using this timer event to fade out and destroy each "bullet" however the multiple timers that get created seems to be causing problems. Several "bullets" fade out and get destroyed at the same time.??

I hope this makes more sense.
Your help/ thoughts on this are appreciated.

Re: How to destroy actors?

PostPosted: Fri Dec 30, 2011 8:25 pm
by skydereign
The slowdown when you have a lot of bullets is usually because of two things. One, if you have a draw event in the bullet that will cause some slowdown with large numbers of bullets. The other, and more important one, is if your bullets have collision events with other actors. The rule on collision events is that if you need a collision event between actor A and actor B. Then the collision should belong to the actor that there are less of. So if A is an enemy, and B is a bullet, then if the game has 20 bullets on average and 4 enemies, then the collision event should belong to the enemies.

Now to address your problem, it is a little tricky because of the fading out. Since you want no more than 20 bullets that are not fading out, you'll need to store the indexes in an array. I've attached an example of what you want, it contains two scripts in global code, and uses an actor variable called state. It's pretty straight forward if you understand arrays, and how to use modulo. If you want something explained though feel free to ask.

Re: How to destroy actors?

PostPosted: Fri Dec 30, 2011 9:24 pm
by TGKG
Thank you skydereign,
You have it and a very clean solution.
I understand the simple parts :)
Please explain what the getCloneIdx function does.
I also do not understand in the global code what "Actor *" is all about.

Also, thank you for the info about collisions.

Re: How to destroy actors?

PostPosted: Fri Dec 30, 2011 11:48 pm
by skydereign
Ok, an Actor* is a pointer to an actor. Not sure if you've ever done this, but if you type something like this...
Code: Select all
actor.x+=10; // this increases actor's position by 10

You are using an actor struct. These structs are what contain all of an actor's variables, such as x, y, animindex, animpos, and everything else. Well, an Actor* is a pointer to an actor, so it works in a very similar way. Now the function getCloneIdx gets the pointer of a given clone via the getclone function. getclone takes a clonename (name.cloneindex) and retrieves the pointer to the actor, so you can do things to it (in the case of the ged I uploaded all you use it for is to set one of its actor variables to 1).

With that said, all Actor*s are really used for is to interact with individual clones. So you have a lot of bullets, but you want to target a very individual bullet. We can use the cloneindex stored in the bullet_indexes array to get the proper cloneindex, which is passed to the getCloneIdx function.

To actually use an Actor*, you have to use -> instead of the . character.
Code: Select all
Actor* actor_pointer = getCloneIdx("actor", 4);
actor_pointer->r=0; // sets actor.4's red value equal to 0

Re: How to destroy actors?

PostPosted: Tue Jan 03, 2012 5:03 am
by TGKG
Thank you for the clear answer. I think I get most of it, however how did you know to use a pointer rather than the variable itself.

Not sure how I give you +1 (or more) for this answer, however well done +1

Re: How to destroy actors?

PostPosted: Tue Jan 03, 2012 5:08 am
by skydereign
To give points, you just click the +1 button under my username. But you can have a pointer by adding the *. So you declare an int pointer by using this.
Code: Select all
int* var;

The same thing for a char, versus a char*. To dereference a pointer, you use *pointer_name. That way you can access the value that the pointer actually points to, instead of using the address.