Mutual Hit at the same time, how to?

Hello Everyone, hope you are doing great!.
I’m trying to create a mutual hit situation, where my “bullet” has a power that is reduced when it hits the enemy by the same amount of health that the enemy has and only when the power of the bullet reaches 0 then is destroyed. For example, I shoot a bullet with a power of 10, and hits an enemy with a health of 6, so the enemy die but the bullet continues with a power of 4 and hits the next enemy with 6 health points, that enemy only receives 4 damage, and the bullet is destroyed because the enemy had more health than the actual power of the bullet.
But I’ getting mixed results because when calculating the collision condition in the event, one action occurs 1st than the other, so the next action is not yielding the result I need. Example: If my first action is to reduce the power of the bullet based on the enemy health, then for the 2nd action the bullet already only have 4 power points, so it really only do 4 damage points to the enemy. And the same thing happens viceversa. If I put the action to reduce the enemy health 1st then, because the bullet has more power, when calculating the “damage” to the bullet, there enemy will have no health, so it won’t reduce the power of the bullet.
Does anyone has any idea how to solve this? Thanks in advance


!

1 Like

I understand what are you trying to do, but before i suggest you something can you show your events?

Try something along these lines:

1 Like

Hi, here is the event for the collision, is really simple.

awesome, I’ll try that. Thank you! I’ll report later about the result.

Thank you all @Kruger and @MrMen for your help. I have changed somethings in the game I’m making so I won’t need this now. But I’ll try to remember to come back and reply, for the sake of others also, how I solved this if I come back to do it.

1 Like

To my understanding
You have solution right in front of you
You have hp for enemies which you subtract
Sooo
How about give bullet hp and subtract it when it hits the enemy?
I guess in your case your hp would be power of your bullet so you do not need another variable there

1 Like

If you still want to make this happen, i think i got how…

So here we go…

Lets give the enemy some health for this example…

Condition

  • At the beginning of the scene

Action

  • Change Object Number Variable “Health” of “Enemy” “set to” “5”

  • Start by making a Global Number Variable, call it “BulletID”, leave it at “0”

  • Next lets create the bullet…

Conditions (for example)

  • “a” key is pressed
  • Trigger once

Action

  • Create object “Bullet” at Player.X(), Player.Y()
  • Change Global Number Variable “BulletID” “add” “1”
  • Change Object Number Variable “BulletShotID” of “Bullet” “set to” “BulletID”
  • Change Object Number Variable “Health” of “Bullet” “set to” “10”
  • Add permanent force of 500px to “Bullet” (In the direction you want)

Now for the collision with the enemy…

Condition

  • “Bullet” is in collision with “Enemy”
  • Object Number Variable “GotHit” of “Enemy” is “<” “Bullet.Variable(BulletShotID)”

Action

  • Change Object Number Variable “GotHit” of “Enemy” “set to” “Bullet.Variable(BulletShotID)”
  • Change Object Number Variable “StoreValue” of “Bullet” “set to” “Enemy.Variable(Health)”
  • Chamge Object Number Variable “Health” of “Enemy” “subtract” “Bullet.Variable(Health)”
  • Change Object Number Variable “Health” of “Bullet” “subtract” “Bullet.Variable(StoreValue)”

Then for the Bullet…

Condition

  • Object Number Variable “Health” of “Bullet” is “=<” “0”

Action

  • Delete Object “Bullet”

…and finally to reset the “ID”

Condition

  • Global Number Variable “BulletID” is “>” “500”

Action

  • Change Global Number Variable “BulletID” “set to” “0”

…and your welcome :smiley:

This way your bullets will do exactly what you wanted!

1 Like

Hi Zerox4, thanks, but that’s exactly how I had it, but whatever action happened first was taking from the other. Example: Bullet = 10hp enemy = 6hp
if the first action was to apply damage to the enemy, then the second action of applying damage to the bullet based on the enemy hp would result in 0 damage to the bullet. But if the first action is apply damage to the bullet, then the bullet would only apply 4 damage to the enemy.

Thanks!, I will also try this when I come back to this part!

1 Like

It will work, trust me.

If it dosent, post the events and ill show tell you were the flaw is :slight_smile:

But when you apply static damage based on hit and not other value then it should work unless you are doing something wrong

Like for each enemy you subtract 2 HP from bullet
Unless you are applying one HP over another?

Think about it…

Lets say the bullet has 10 HP and the Enemy has 4 HP

When the bullet collides with the enemy… How do you subtract from both of them at the same time?

GDevelop reads events one at a time, from top to bottom, one will always be subtracted first.

For example…

Bullet Hits Enemy

  1. Subtract from Enemy HP the Bullet HP
  2. (Enemy is now at -6 HP)
  3. Now Subtract Bullet HP from Enemy HP
  4. (Bullet is now at 4 HP)

This is where the weird calculations Archivas was talking about comes from…

So how do you make a hit at the same time? The way i showed you. You have to store the enemy HP before you subtract from it, so that you can use it later on the bullet.

So…

Bullet Hits Enemy

  1. Save Enemy HP into Variable (4 HP)
  2. Subtract Enemy HP = to Bullet HP (4-10=-6)
  3. Enemy is now at -6 HP (Dead)
  4. Now Subtract Previouse Stored Enemy HP (4) from Bullet HP (10-4=6)
  5. Bullet now has 6 HP

Did that make sense?

It seems simple, but sort of a little puzzle, kinda like the sheppard that had to cross the river with the wollf, sheep and the cabbage.

1 Like