Firing bullets in a stacked order?

hello I am having trouble working out how to get 3 turrets stacked on on top of each other and have each turret fire 1 bullet at a time in order from top to bottom. for example top fires a bullet then middle fires then bottom fires then loops back to the top? I have tried but having trouble working out the logic.

example of what I am trying to do
Sprite-0001

Did you consider using boolean variables for activating/deactivating the turrets? When no. 1 shot → activate 2 and deactivate 1 (3 is inactive). When no. 2 shot → activate 3 and deactivate 2 (1 stays inactive).

1 Like

thanks for your reply @Drona I tried something like what you suggested and it works but only fires 2 loops then stops :confused:

events

example 2 loops then stops
Animation

That’s strange. I build something quick (without the fire extension and timers) and it works:

It looks a bit redundant because I just copied and pasted the events, but with a counter variable it should be possible to reduce it to just one block.
I am not sure if you are willing to change your events, maybe you could use a timer instead of the wait action. I remember that I had some trouble with it because it did not work the way I thought and a timer solved the problem. Btw is there any event where your scene variable “icecreamcatattack” is false?

2 Likes

great thank you that works perfectly :+1: but I have a major flaw whenever the player destroys one of the turrets the loop breaks and the remaining turrets stop firing. my idea is to have the turrets keep firing in a loop even if only one turret is left… I didn’t think about that beforehand :sweat_smile:

I didn’t have variable “icecreamcatattack” is false I added it but still wasn’t looping.

It sounds like you want to make use of a Doubly Linked List type structure using the tower objects as the list nodes.

Each tower has 3 variables -

  • Id which is unique and only for this tower,
  • NextTower_Id which is the Id of the tower that fires next, and
  • PrevTower_Id which is the Id of the tower that fired before

That last variable is needed for removing a tower without altering the firing loop.

You start off with the Id of the first firing tower stored in a variable (currentId?). When the timer reaches s it’s target, reset it, fire the bullet from the tower matching currentId, and set the currentId = NextTower_Id.

Repeat and rinse forever.

To delete a tower, you’ll need to point the deleted tower’s pervious and next towers to each other. A straight forward task. Here’s an explanation on how to remove a node from a doubly linked list.

2 Likes

hello @MrMen thanks for helping me out. I did read up on double lists and deleting nodes but had trouble understanding the looping process and the different variables :confused: I ended up making it so the loop doesn’t stop firing if a turret is deleted it will keep going down the list. not ideal but works.

working
Animation

events

Yeah, that kinda works, but there’s that obvious pause between towers 1 & 3 once tower 2 is removed.


Try this (it assumes the turrets have instance variables Id, NextId and PrevId all correctly filled in):

2 Likes

Hi thanks for the example but I’m having allot of trouble understanding how it works :sweat: I tried to follow but I’m stuck on how to set up the variables names for each instance. I have a few questions is the object var “Id” in your example is that the unique name of the turret… like “turret1”?
with naming variables “NextId” and “next_Id” how do they work with var “Id”?
and how does “currentTurret” is set to 1 change turrets? thanks :sweat_smile:

variables of first turret instance

events but nothing work hahaha

It can be whatever you’d like it to be, as long as it uniquely identifies the turret. The simplest is to make them all a number, from 1 onwards, but it’s up to whatever your preference is.


Firstly, you’ve defined the object variables as string, but are referring to them as number/value. If you’ve named then turret1, turret2 etc, then the value will be 0 for all of them.

Secondly, you’ve named the variables in the Object Variables screen next_id and prev_id, both with an underscore:

image

But you are referring to them in your events without the underscores.

image

1 Like

thanks for the reply. haha yes the problem was strings I forgot to change to numbers. I have each turret firing in a loop that is working now. but I’m having trouble with the delete part when I destroy turret1 for example the other turrets stop firing?

variables for turret1

variables for turret2

events

No, that’s not right. what you had earlier was much better:

Are icecreamcatpink, icecreamcatpink2 and icecreamcatpink3 completely different objects, or just duplicates?

If they’re the same object duplicated, then get rid of the duplicates and use 'multiple instances of one object, but each with a different id.

If they’re different objects, put them in an object group and access that.

1 Like

EDIT UPDATE the turrets are firing in a loop and working. I had an issue depending when a turret gets destroyed it sometimes won’t continue the loop but I fixed that. thanks for your help.

working events

Did you take a look to how the boss of the Space Shooter example is done? It looks similar to what you want to do.

1 Like

hello thanks for the link. after playing and looking at the events I don’t think the timer of either boss attacks is affected when one or the other is destroyed. at least it seem that way when playing.

@MrMen hello sorry to bring it up again I’m still having some trouble with deleting turrets. the loop works and when a turret is deleted works. but if the middle turret is deleted right after the top turret has fired the middle turret doesn’t go to the bottom turret and stops firing? the same happens if bottom turret is deleted right after middle turret has fired :no_mouth: I’m not sure why?

events

instance variables top


middle

bottom

I decided to take a different route. Instead of iterating them, I decided to pick a random turret. I added a Boolean object variable to keep them from repeating until all of them had fired. But that could be skipped.

Try online: The target can be dragged. Click a turret to delete it.

2 Likes

hello thanks for the reply. that’s a neat way of doing it simple and works! :sweat_smile: is it possible to change it so that the turrets fire in order and not random?

I played around with some ideas but most seemed overcomplicated. I like simple and random.

One idea was to incrememt a variable for an Id check. 1 through the number of instances. When the variable equals (instances plus 1) then set it to 1. When a turret was deleted, the IDs would have be renumbered or swapped. That seemed uneven.

When you delete a turret, you mught delete turret #2. So, the IDs would be 1 and 3. You can skip invalid numbers or renumber the IDs.

The other idea involved ray casting up and down
to find the next turret but again. Overcomplicated.

1 Like

I just don’t know what’s wrong with this one you did.

You have each turret firing one bullet every 3 seconds and it’s in order, and the only beef there was about it was that, if the player destroys one of the turrets, the bullets in the remaining turrets fire at the same speed as before instead of increasing to make up the gap of the missing turret.

So if it’s just a matter of increasing the speed, or taking out the timer of the missing turret, can’t you just make the timer in a sub event? Like The scene variable turret = 1, then sub event: number of icecreamcatpink instances on the scene doesn’t equal 0 and icecreamattacktimer > 1 seconds, event block exactly as you have it. Then another sub event (to the scene variable turret = 1 condition): number of icecreamcatpink instances = 0 (NO TIMER), event change scene var turret add 1.

1 Like