FiveM Tutorial - Some Game Events and how to use them - Thu, Oct 19, 2023
This was initially posted on the FiveM / CFX forums. You can go directly to the original post below.
Original Post
Recently I’ve been testing the gameEventTriggered 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 events that I found:
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!
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
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 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.
Back to Home
Categories