Some Game Events and how to use them

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.
9 Likes