Scripts / [SOLVED] Counting missed shots?

Author
Message
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 6th Dec 2020 15:37
Hi, is it possible to make a script for counting missed shots to soldiers?

What should i consider to do this?

The author of this post has marked a post as an answer.

Go to answer
PM
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 6th Dec 2020 16:51
Do you mean shots the player has missed or shots the AI has missed?
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 6th Dec 2020 18:13
Sorry, i mean shots the player has missed.
PM
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 6th Dec 2020 20:11 Edited at: 6th Dec 2020 20:57
I was thinking maybe in something like this code but going for the negative (i don't know if this makes sense). I mean, if the game does not find a limbhit then it adds one to the misshit counter.
Also not restricted to the soldier's head but his whole body.

PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 6th Dec 2020 21:35 Edited at: 6th Dec 2020 22:43
g_PlayerGunFired is set to 1 every time the player fires.

GetBulletHit() returns the x,y,z of a bullet hit.

if you calculate a ray cast along the players eyeline towards the bullet hit point you can get the object id of whatever was hit, convert that back to an entity id and use it to check if you hit an enemy.

You might be able to simplify it somewhat as the 4th parameter passed back by GetBulletHit() is the material index so if NPCs have a unique material index you could check for that I suppose.

Edited to add: by fourth parameter I mean:

local x, y, z, m = GetBulletHit()

then x, y, z give the bullet hit point and m is the material index.

You would need to experiment by shooting some NPCs and see what the m value is for them.

So I investigated and the value returned for a hit on a standard soldier is 2, dunno what 2 means as a material index but that might be unique enough.

Edited to add quick and dirty solution script.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 7th Dec 2020 02:36 Edited at: 7th Dec 2020 02:45
AmenMoses your script is magnific, it works perfectly fine. It counts the misses as expected.

There is one last thing i would like to consider for this script and i don't know if it possible.

I would like to count the shots that don't kill a common soldier as a miss.

In my level there are some soldiers that are extremly weak and will die by one shot, but there are others that were desing to be unkillable and i would like to count the shots that hit them (but can't kill them) as misses.

Is this possible?

Also here is the script i'm using for the soldiers (provided by smallg)

PM
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 7th Dec 2020 03:18
Ok. So i'm using these two scripts but it is not tottaly accurate. Sometimes a shot to an unkillable soldier may not count like a miss, and also sometimes a shot that kills a soldier (so it is not a miss) also counts like a miss.

Its is wierd, it's not perfect, but works most of the time so far.

This goes on a common soldier:


This one goes on a barrel always active:
PM
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 7th Dec 2020 13:40 Edited at: 7th Dec 2020 13:49
perhaps combine amen's method with some code in the exit(e) part of the AI (as this function will only be called for AI that die)
i.e.


and in the AI code

that should count every shot as a miss but then reduce it by 1 for every kill shot - thus if your enemies die in 1 shot it gives the same result as only counting shots that don't kill

p.s. amen's solution doesn't work with rapid fire - i.e. weapons that allow you to hold the mouse button down to fire multiple shots
so you should probably use something like this to account for it
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 7th Dec 2020 17:55
Be aware that g_PlayerGunFired will be set to 1 for many frames after you have fired just one shot so you will probably need to check the ammo count when it resets to 0, i.e. when the player stops firing, or after a specific amount of time if the left mouse button is held down.

To make it really accurate build a list of AI entities then every frame where g_PlayerGunFired == 1 check the health of them to see if it has changed, then process the change in health appropriately, i.e. as counting as a hit or if the entities health drops to zero in that frame count it as a kill.

Keep a running total of hits and kills and subtract from the ammo used to work out the misses.


Been there, done that, got all the T-Shirts!
PM
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 8th Dec 2020 16:25
smallg I have tried your scripts but i don't know why they don't work.

This one doesn't add to the misscounter (i'm using it on the soldier's properties as the one i posted before but with your changes)


And this other one gives me an error saying that it is trying to do an arithmetic count on g_misses whenever i fire the gun. (using it on a barrel always active)


PM
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 8th Dec 2020 16:32
This post has been marked by the post author as the answer.
this one goes in the soldier script
i.e. ai_soldier.lua

this one (make sure it's named "test_bullethit.lua") goes on an always active = yes entity (note: misses = 0 should not be local)


there's no reference to g_misses in those 2 so if you are trying to call that in a different script make sure you change misses to g_misses or vice versa (i.e. if you are trying to show the misses on screen from another script)
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
gaku95
4
Years of Service
User Offline
Joined: 2nd Dec 2020
Location: Buenos Aires, Argentina
Posted: 8th Dec 2020 18:35
Amazing smallg your script is totally accurate and works perfect now, it was my mistake before.
Thank you for your time guys you are awesome.
PM

Login to post a reply

Server time is: 2024-12-22 02:07:40
Your offset time is: 2024-12-22 02:07:40