Calculate the damage inflicted

HI,
How can I calculate the hits inflicted on the enemy in a given period of time?
every time the enemy is hit, a text is created with the damage inflicted by the weapon.
I have machine guns that create 10 damage per shot and when they hit quickly it creates multiple times the damage 10 text.
what I would like to obtain is a calculation of the damages suffered.
if the enemy is hit 3 times change to 30


oh I forgot… these are my events:

Use an array on the enemy object, and every time a bullet hits the enemy add the current timestamp to that array.

Add a while event that removes the first array element if it’s less than (current_timestamp - period_of_time)

The number of children in the array will be the number of bullets that have just hit the enemy over the given timer period.

1 Like

ok I added the array on all my enemies

Could you kindly help me with this step? I’m not very familiar with arrays

I imagine that for the final damage calculation I will have to do 10 x the number of arrays

How about something like this? It uses a timer and linking. You might need a different way to place the text object in relationship to the enemy if it’s moving. But the concept seems good.

ScreenRecorderProject36

2 Likes

looks good… sorry can you explain to me how to multiply the value of the dmg.ak47 variable by the number of bullets hit.

Are there different weapons? If so, do all the weapons apply the same amount of damage?

image

To multiply dmg.ak7 * bullet count it would be

ToString(Variable(dmg.ak47) * PickedInstancesCount(bullet3))

You need to add the numbers and then change it to text for the text object otherwise it will append text to text and you’d get “5” + “5” equals “55” instead of 5 + 5 equals 10.

Depending on the setup you might need for each enemy first and then for each bullet if the bullets are different types. It’s rare but you need to plan for the possibility of multiple enemies being hit simultaneously.

1 Like

Here’s a setup that fires a bullet with either the default red animation or a purple animation. It uses a damage variable to calculate the damage. If the bullets were in a group, then the objects could have a preset object variable. Since it checks each bullet separately it doesn’t need to multiply the select count because it will always be only 1.

1 Like

Thanks Keith for taking the time.
There are different types of weapons and each one inflicts different damage.
I created a small game project and followed your events but the text “NaN” is created.
I’m definitely doing something wrong… I’ve attached the file, could you take a look at it when you have time? I’m going crazy hahah

When I can I’ll look at your project. It sounds like it’s trying to convert text to a number or add text that’s not a number. The same original happened to me because I didn’t set the default text value of my text object to 0. It was set to text.

Edit: That was the issue the initial text needs to be 0
image

Also, make sure all of the events use the group name for the bullets.

This is missing the period in Bullet.2
image

Setting the default value for the damage object variable for the bullets and changing the first collision to use the bullets group seems to work. Plus I added a delete Bullets.

1 Like

sorry for my absence… I did as you suggested and now it seems to be working! thank you so much

1 Like


in the event that the bullets pass through the enemies and are not cancelled, the value is calculated several times.
How would you handle this?

There are posts about this.

I would use a structure. Add the ID variable to each enemy as a string, give them a value based on a counter variable. Give the bullet object a structure named IDs

Events

project: (click green [code] then [download zip]

This might need more testing. But it’s a start

I’d prefer to use the linking version. That wouldn’t require IDs. I tried to make it but it didn’t work. If I get it to work. I’ll share it.

1 Like

I was rewriting the events by removing the health behavior and added a damage value to the projectiles and this is what happens:
Registrazione bullets


the last enemies take no damage.
I am attaching the file again if you have the opportunity to take a look I would be grateful.

I’ll take a look at it to see what is happening but I was able to figure what was wrong with my linking. I needed a pick all.

Edit: I looked at your project and the enemies didn’t have unique IDs. That’s one of the reasons why I try not to use IDs.

This version doesn’t need any variables or IDs. As always, it needs testing.

Updated with a screenshot of all of the events.

project
https://github.com/doug13579/gdevelop-multiple-bullet-one-time-hit-2

1 Like

The message was deleted just as I was replying. I don’t know if that means you realized that the text object was being added but the text wasn’t being changed.

There’s a 2nd part to my example that I didn’t screenshot but should be in the project file. Sorry about that. I’ll update the previous post.

In your version, you would need to link the text object to the enemy after it’s created. In my test I added the text objects and linked them in the beginning. I was testing the linking part not your entire concept.

Yes, I deleted it because I realized that the text was not calculated anywhere
In the previous example where should the “pick all” be added?
I was also trying the second one but there are several problems including the text is calculated infinitely when the enemy and the projectile are linked and the text is deleted simultaneously on all the enemies to which it is linked

I’m trying it using your concept and it’s not working for me. Let me look into it.

1 Like

the first concept works very well except that the IDs are not calculated correctly.
in my project I am not using any type of behavior

OK, this doesn’t delete the text object but it’s only creating 1 text object and it’s only adding the damage once per collision.

try me:
https://gd.games/keith_13579/single-hit-bullet