[SOLVED] Bullet Lifespan

Greetings,

I have this POC where I’m testing bullet patterns, and I want to add a lifespan to bullets.

So here, I have a guy with various variables, one of them being bulLife. The plan is so that if I put any number in here (For example 1 for 1 second), then every bullet shot by the guy will only stay for a second, then it will be deleted.

What I tried does not work.

Is there something I’m not doing right?

Why are you linking guy and bullet?
image

I am not entirely sure but I think you need a Repeat for each object for this event.


The event above likely didn’t work because it has nothing in its condition except Trigger Once. That means, it only happens once, which is at the beginning of the scene. Instead of doing that, try putting that event as a sub-event of event where you spawn bullets.

image
Also if you want to gradually reduce the opacity of the bullet, giving it an effect of fading, you can try using the Opacity Tween.

Alright, I changed the code:

The bullets are now disappearing when 1 second passes now, but now there’s another problem:

issue1

Notice the guy in the top left corner? His bulLife value is set to 0, meaning HIS bullets shouldn’t disappear. However, both guys seem to be sharing the same timer for some reason.

How can I make it so that the guys both have individual lifespan values?

@Mixen I guess it’s not possible then?

In your first event, you’re setting the lifespan of bullet to the value of guy.Variable(bulLife).

The question is, which guy’s variable are you using? You’re not selecting the guy linked to each bullet. So GDevelop uses the first one in the list of all guy objects. You need to link them as a condition of the first event.

However, I’d question why you aren’t setting the bullet lifespan in the event that creates the bullet. I’m guessing at that point you know which guy it’s coming from, so there’s no fiddly little after events to worry about.

Okay I added links

And I copy pasted the lifespan variable changes to each creation event for the bullets (There are more hidden offscreen)

However, it didn’t work. Here’s the result:

issue1

The guy in the middle has 2.5 as his bulLife value, so his bullets should last for 2.5 seconds. The guy in the topmost right corner has a 1 for the value, so bullets should last 1 second.

For some reason, the guy in the middle stole the corner guy’s 1 second value, and he’s using that for his own bullets. Additionally, the 2 guys at the top’s bullets don’t EVER disappear.

Did I miss something important?

Yes,

Why are you creating the link there? What are you doing? You need the link in the condition, as in all guys linked to bullet. How else will you filter out the correct guy object the bullet is linked to?

Also, think about it what you are doing. Think of the steps you are taking. Do they seem logical? As I explained, the in the first event you are not filtering the guy object to use.


You’ve already created the link between the bullet and guy object. No need to make more random links.

Alright, I made the changes. The lifespan values work fine for the guy in the middle now.

However, the rest of the guys ignore their lifespan values for some reason.

If it helps, here’s the whole code for the guys shooting logic:



You need to add a repeat for each guy object where the arrows are. As it stands, there could be more than 1 guy object that meets the conditions :

And create the bullets in the repeat block.

I tried it out for the state 0 guy to test it, and there’s unfortunately no change

Ah, my bad. I completely missed that you are looping for each guy object at the very start. Ignore my previous message.

The code you have for creating the bullets seems fine.

But I think your code for checking the bullet lifespan needs work.

Firstly, I can’t see the event actions that create the lifespan timer. You should start it when the bullet is created.

Secondly, try changing the bullet removal events to :

Also unlink all object from bullet before you delete it.

Unfortunately, nothing changed. I even put the reset timer in one of the creation events, and not even that worked.

Of course that won’t work; you are resetting the timer every single frame just before you check if it’s greater than the variable lifespan. Get rid of that first event completely, and just have the the second event and it’s subevent.

Oohh, I see now. I deleted the event and put the reset timers under all the creation events, but once again, nothing changed.

Again, only the guy with state 5 obeys his timer. The rest of the guys do not

To confirm, the bullet lifespan check isn’t a subevent where the parent checks if state of guy is 5?

If not, can you provide screen snips of the updated events? Including where you check the bullet lifespan? There’s something that won’t be stacking up.

Okay, here’s the ENTIRE code





You are linking guy to bullet spinner, and bullet to bullet spinner for bullet type 5. However, bullet types 0 - 4 don’t have bullet spinner attached.

So I wonder if this part

doesn’t act on anything for bullet types 0-4 because there is no linked bullet spinner. I wouldn’t have thought it would affect it, but you ever know.

Try making the delete bullet a separate event after the bullet spinner is unlinked and deleted (so the delete bullet event is a subevent of the lifespan timer check)

That actually worked! All bullets now obey their lifespans!

But now the spinners won’t delete. Do I need to go one step further to take care of spinners?

Also, are these events

image

image

image

don’t appear to be part of the repeat for each guy block. You can confirm this by collapsing the repeat for each guy block. If you can still the the above events, then they’re not indented enough.

You didn’t read the instructions well enough :slight_smile:

Delete the bullet after you delete the spinner. Otherwise the bullet doesn’t exist when you find the link to bullet spinner.

1 Like