From 3fd458ac63cedc8ff5e60cd2ef31ed947f78bc9e Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 26 Feb 2021 15:42:17 +0000 Subject: [PATCH] feat: power button sync and passenger control The power state is now synced between the driver and passenger. The passenger can also toggle the radar's power if the config line allows for it. --- cl_radar.lua | 8 ++++++-- cl_sync.lua | 20 ++++++++++++++++++++ sv_sync.lua | 4 ++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cl_radar.lua b/cl_radar.lua index a1ac301..7696a54 100644 --- a/cl_radar.lua +++ b/cl_radar.lua @@ -1487,8 +1487,12 @@ end ) -- Runs when the user presses the power button on the radar ui RegisterNUICallback( "togglePower", function( data, cb ) if ( PLY:CanControlRadar() ) then - -- Toggle the radar's power - RADAR:TogglePower() + if ( not RADAR:IsPoweringUp() ) then + -- Toggle the radar's power + RADAR:TogglePower() + + SYNC:SendPowerState( RADAR:IsPowerOn() ) + end end cb( "ok" ) diff --git a/cl_sync.lua b/cl_sync.lua index c6a701e..295b6b0 100644 --- a/cl_sync.lua +++ b/cl_sync.lua @@ -32,3 +32,23 @@ SYNC = {} +function SYNC:SendPowerState( state ) + local otherPed = PLY:GetOtherPed() + + local otherPly = GetPlayerServerId( NetworkGetPlayerIndexFromPed( otherPed ) ) + + TriggerServerEvent( "wk_wars2x_sync:sendPowerState", otherPly, state ) +end + + + +RegisterNetEvent( "wk_wars2x_sync:receivePowerState" ) +AddEventHandler( "wk_wars2x_sync:receivePowerState", function( state ) + local power = RADAR:IsPowerOn() + + if ( power ~= state ) then + Citizen.SetTimeout( 100, function() + RADAR:TogglePower() + end ) + end +end ) \ No newline at end of file diff --git a/sv_sync.lua b/sv_sync.lua index 9df6cf1..ca0586f 100644 --- a/sv_sync.lua +++ b/sv_sync.lua @@ -31,6 +31,6 @@ ---------------------------------------------------------------------------------------]]-- RegisterNetEvent( "wk_wars2x_sync:sendPowerState" ) -AddEventHandler( "wk_wars2x_sync:sendPowerState", function( state, target ) - -- code +AddEventHandler( "wk_wars2x_sync:sendPowerState", function( target, state ) + TriggerClientEvent( "wk_wars2x_sync:receivePowerState", target, state ) end ) \ No newline at end of file