Some Game Events and how to use them

Recently I’ve been testing the gameEventTriggered - Cfx.re Docs event. In theory, this event should be able to take care of game checks, like entity damage, “ped entered vehicle” or “vehicle is destroyed / undrivable”.

In fact, I’m gonna show you how to set up some cool event triggers.

These are some easy to implement

AddEventHandler('gameEventTriggered', function (name, args)
    if name == "CEventNetworkPlayerEnteredVehicle" then
        TriggerEvent('gameEvent:NetworkPlayerEnteredVehicle', args[1], args[2])
    elseif name == 'CEventNetworkVehicleUndrivable' then
        TriggerEvent('gameEvent:NetworkVehicleIsUndrivable', args[1], args[2], args[3])
    elseif name == 'CEventNetworkEntityDamage' then
        if args[6] == 1 then --damage leads to entity death
            if IsEntityAPed(args[1]) and IsEntityAPed(args[2]) then --both victim and killer are peds.
                TriggerEvent('gameEvent:PedDied', args[1], args[2], args[7])
            end
        end
    end
end)

AddEventHandler('gameEvent:PlayerEnteredVehicle', function(netID, vehicleid) --Player entered vehicle
    print('Player entered vehicle id ~y~'..vehicleid..'~s~ with network id of ~y~'..netID..'~s~')
    --netID is different than server id. 128 = your client.
end)

AddEventHandler('gameEvent:VehicleIsUndrivable', function(vehicleid, killerid, weaponHash) --Vehicle becomes undrivable
    print('Vehicle '..vehicleid.." got destroyed by ped id "..killerid.." using weapon hash "..weaponHash)
    --killerid is a ped entity id. weaponhash can also show the death by grenade explosion, or by going underwater.
end)

AddEventHandler('gameEvent:PedDied', function(victimid, killerid, weaponHash)
    print('Ped '..victimid.." got killed by ped id "..killerid.." using weapon hash "..weaponHash)
end)

Let’s talk a bit about about CEventNetworkEntityDamage. This event outputs 13 arguments!
From my testing, I found the following:

args[1] --entity that received the damage.
args[2] --entity (or -1 for falling or drowning) that caused the damage.
args[3] --unknown, but it looks like a hash
args[4] --unknown, always 0
args[5] --unknown, always 0
args[6] --damage caused entity to die. Boolean.
args[7] --weapon hash. This can be a pistol, a grenade, unarmed, drowned, drowned in vehicle, fallen, etc. There's a lot of them, but it works with already known player weapon hashes.
args[8] --sometimes this is a hash, but usually 0. Something to do with vehicle collision? 
args[9] --sometimes this is a hash, but usually 0. Something to do with vehicle collision? 
args[10] --unk, always 0 in my testing
args[11] --unk, sometimes 1, but mostly 0. I think something to do with cops.
args[12] --always 1 if you hit a parked car. Otherwise 0
args[13] --some sort of flag. parked cars is 116. Otherwise mostly 0.

Some notes:

  • Only network game events are triggered. (for now?) (single player events added in server version 5182)
  • Game the entity damage event might be of better use rather than always looping ped damage / death natives.
  • There are some other events that I caught, like CEventNetworkPlayerCollectedAmbientPickup, that triggers every time you pickup something from the ground. I haven’t tested it that much, which is why I haven’t included it here, yet!
13 Likes

nice!

As of server version 5182, more low-level game events became available for use.

Those events are handled using their own names, instead of gameEventTriggered, and can, in theory, provide more tools for devs to handle AI behavior, for example.

You can find the game events here: Game events - Cfx.re Docs

Syntax example:

AddEventHandler("CEventShockingGunshotFired", function(entities, eventEntity, args)
	print("CEventShockingGunshotFired"..json.encode(entities)..' '..tostring(eventEntity)..' '.. json.encode(args))
end)

Below, I will share some of the events that I got working, and I think are cool.

  • Opening of a door:

    • From my testing, it only returns the ped that opens a door, as eventEntity.
    • Event is called as soon as the ped begins the door open animation / motion.
    • If you open a double door, the event can be called once or twice, depending on how many doors the ped touches.
    • Doesn’t get triggered by garage doors.
      AddEventHandler('CEventOpenDoor', function(entities, eventEntity, args)
      local _text = "CEventOpenDoor \npid: "..PlayerPedId().."\neventEnt: "..eventEntity.."\nentities: "..json.encode(entities).."\nargs: "..json.encode(args)
      print(_text)
    end)
    
  • AI ped sees a nice car.

    • eventEntity seems to be the ped that saw the car
    • entities is a table. Most of the time it passes a single entity. But it’s not necessarily the car.
    • there seems to be 1 arg, which is a table. I thought it was some sort of coords, but it’s not. Not sure what it is.
    AddEventHandler('CEventShockingSeenNiceCar', function(entities, eventEntity, args)
      local _text = "CEventShockingSeenNiceCar \npid: "..PlayerPedId().."\neventEnt: "..eventEntity.."\nentities: "..json.encode(entities).."\nargs: "..json.encode(args)
      print(_text)
    end)
    
  • Driving on pavement

    • Triggered when a ped sees someone* driving on the sidewalk.
    • eventEntity seems to be the vehicle handle.
    • args[1] is a table. not sure what it contains.
    • entities contains the ped handles that saw the vehicle (usually one)
    AddEventHandler('CEventShockingDrivingOnPavement', function(entities, eventEntity, args)
      local _text = "CEventShockingDrivingOnPavement \npid: "..PlayerPedId().."\neventEnt: "..eventEntity.."\nentities: "..json.encode(entities).."\nargs: "..json.encode(args)
      print(_text)
    end)
    
  • Gun shot

    • This is a weird one. It triggers, when a ped shoots a gun, but also when a ped is affected by a gunshot (so, like, getting scared, or hearing the gunshot.)
    • eventEntity is the shooter.
    • entities contains peds affected by the shot (including the shooter, after first shot?)
    • usually no args. empty table.
    • In AI crowded, this event will be triggered multiple times, as more and more peds will be affected by the shoot. (maybe peds in the direction of the projectile?)
    AddEventHandler('CEventGunShot', function(entities, eventEntity, args)
      local _text = "CEventGunShot \npid: "..PlayerPedId().."\neventEnt: "..eventEntity.."\nentities: "..json.encode(entities).."\nargs: "..json.encode(args)
      print(_text)
    end)
    
  • Crime reported

    • I’m gonna be honest. This is almost useless.
    • Will always trigger twice in my testing. second event is always blank (no entities, no args)
    • eventEntity is always 0
    • entities is a table containing a list of peds. I think it contains peds that responded to the crime? not sure. Using ReportCrime() will not add your ped to the entities list, but crimes reported by AI will…
    AddEventHandler('CEventCrimeReported', function(entities, eventEntity, args)
      local _text = "CEventCrimeReported \npid: "..PlayerPedId().."\neventEnt: "..eventEntity.."\nentities: "..json.encode(entities).."\nargs: "..json.encode(args)
      print(_text)
    end)
    
  • collision events

    • I tried "CEventObjectCollision" and "CEventVehicleCollision". Both of them seem to be held by the game in some sort of buffer, until they are released all at once, every couple of minutes. They might be used for saving stuff to memory, maybe? we might never know.
7 Likes

Let’s bring this thread back from the dead!

Recently, I set about making a Utility Function Library, and in doing that, I decided I need to know the parameters of all the in game events, fun!

Now most peoples searches would most likely lead them here, after the FiveM docs, so I figure this will be the best place to post it (for now).

I’ve found nearly every shocking events params, as well as a good amount of the low and mid level events as well! There’s example handlers with comments, describing the params and what they mean in the client/events.lua.

3 Likes

This sounds like one of those choices that pretty quickly devolves into “why did I commit to doing this?!”

So thank you for doing it!

1 Like

Thank you, if I’m honest, the dopamine release of actually find parameters and it working has me pretty high rn, will wear off if I work on math functions too much longer XD.

I’m super surprised how little attention this post got in the first place. I first learned about game events from here, and I’ve been using them ever since. I’m happy to see that this thread is getting revived again!

1 Like

So a note on this, what I believe is happening is, certain game events take precedence over others. So for example, whilst trying to find the parameters for CEventVehicleOnFire I was having trouble getting that event to trigger, depending on how I damaged it, to light it on fire. I originally spawned a Technical and rained hellfire of 50cal rounds on unsuspecting locals, until the vehicle caught fire but the event that would fire is; CEventVehicleDamageWeapon… huh, strange, but I guess it makes sense. But if that vehicle I originally lit on fire, leaked petrol and started a small fire on the ground and the closest vehicle happened to get lit on fire, guess what event fires!? Yup, CEventVehicleOnFire.