Some Game Events and how to use them

This article was copied word-for-word from my cfx.re forum post. Check it out here:

Recently I’ve been testing the gameEventTriggered - Cfx.re Docs 13 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' thenif args[6] == 1 then --damage leads to entity deathif IsEntityAPed(args[1]) and IsEntityAPed(args[2]) then --both victim and killer are peds.
                TriggerEvent('gameEvent:PedDied', args[1], args[2], args[7])
            endendendend)

AddEventHandler('gameEvent:PlayerEnteredVehicle', function(netID, vehicleid) --Player entered vehicleprint('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 undrivableprint('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 2)

  • 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!

 

As of server version 5182 1, 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 6

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.

Driving on pavement

Gun shot

Crime reported

collision events


90 views

Recent Posts

See All
  • GitHub-Mark-Light-32px
  • Twitter