# 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.

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?

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

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

This is missing the period in Bullet.2

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?

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:

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.

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.