Skip to content

Commit c601ded

Browse files
committed
Lua code formatting for event functions
- Fixed examples
1 parent 81a80f5 commit c601ded

16 files changed

+139
-148
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
addEvent ( "onSpecialEvent", true )
1+
addEvent("onSpecialEvent", true)
22

3-
function specialEventHandler ( text )
4-
outputChatBox ( text )
3+
function specialEventHandler(text)
4+
outputChatBox(text)
55
end
6-
addEventHandler ( "onSpecialEvent", root, specialEventHandler )
6+
addEventHandler("onSpecialEvent", root, specialEventHandler)
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
-- define our handler function
2-
function onPlayerSpawnHandler ( )
3-
-- get the player's name, source is the player because he was spawned
4-
local playerName = getPlayerName( source )
5-
-- output in the chat box that they've spawned
6-
outputChatBox ( playerName .. " has spawned!" )
2+
function onPlayerSpawnHandler()
3+
-- get the player's name, source is the player because he was spawned
4+
local playerName = getPlayerName(source)
5+
-- output in the chat box that they've spawned
6+
outputChatBox(playerName .. " has spawned!")
77
end
8-
addEventHandler( "onPlayerSpawn", root, onPlayerSpawnHandler ) -- root is a predefined global variable for getRootElement()
8+
addEventHandler("onPlayerSpawn", root, onPlayerSpawnHandler) -- root is a predefined global variable for getRootElement()
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
-- *****************************************************************************
22
-- CLIENT CODE
3-
addCommandHandler("cancelLatentEvent",
4-
function ()
5-
triggerLatentServerEvent("exampleEvent",3000,false,localPlayer)
6-
local handles = getLatentEventHandles() -- Returns a table.
7-
local handle = handles[#handles] -- Get the latest handle.
3+
addCommandHandler("cancelLatentEvent", function()
4+
triggerLatentServerEvent("exampleEvent", 3000, false, localPlayer)
5+
local handles = getLatentEventHandles() -- Returns a table.
6+
local handle = handles[#handles] -- Get the latest handle.
87

9-
if cancelLatentEvent(handle) then -- Cancel it!
10-
outputChatBox("Successfully cancelled!",0,200,0)
11-
end
8+
if cancelLatentEvent(handle) then -- Cancel it!
9+
outputChatBox("Successfully cancelled!", 0, 200, 0)
10+
end
1211
end)
1312

1413
-- *****************************************************************************
1514
-- SERVER CODE
16-
addEvent("exampleEvent",true)
17-
addEventHandler("exampleEvent",root,
18-
function ()
19-
outputChatBox("Warning! The triggerLatentServerEvent wasn't cancelled!",client,255,0,0) -- warn the user.
20-
end)
15+
addEvent("exampleEvent", true)
16+
addEventHandler("exampleEvent", root, function()
17+
outputChatBox("Warning! The triggerLatentServerEvent wasn't cancelled!", client, 255, 0, 0) -- warn the user.
18+
end)
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
addCommandHandler("cancelLatentEvents",
2-
function (player)
3-
local handles = getLatentEventHandles (player) -- Returns a table.
4-
5-
for index=1,#handles do -- Loop through the table.
6-
local handle = handles[index]
7-
cancelLatentEvent(player,handle) -- Cancel it!
8-
end
9-
end)
1+
addCommandHandler("cancelLatentEvents", function(player)
2+
local handles = getLatentEventHandles(player) -- Returns a table.
3+
4+
for index = 1, #handles do -- Loop through the table.
5+
local handle = handles[index]
6+
cancelLatentEvent(player, handle) -- Cancel it!
7+
end
8+
end)
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
addCommandHandler("cancelLatentEvents",
2-
function ()
3-
local handles = getLatentEventHandles () -- Returns a table.
4-
5-
for index=1,#handles do -- Loop through the table.
6-
local handle = handles[index]
7-
cancelLatentEvent(handle) -- Cancel it!
8-
end
9-
end)
1+
addCommandHandler("cancelLatentEvents", function()
2+
local handles = getLatentEventHandles() -- Returns a table.
3+
4+
for index = 1, #handles do -- Loop through the table.
5+
local handle = handles[index]
6+
cancelLatentEvent(handle) -- Cancel it!
7+
end
8+
end)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
function stopVehicleEntry ( theplayer, seat, jacked )
2-
cancelEvent (true, "You can't enter a vehicle during war.") -- stop the event from occuring and tell the player the reason.
3-
outputConsole("We told "..getPlayerName(theplayer).." : "..getCancelReason()) --Now tell everyone what the player tried to do
1+
function stopVehicleEntry(theplayer, seat, jacked)
2+
cancelEvent(true, "You can't enter a vehicle during war.") -- stop the event from occuring and tell the player the reason.
3+
outputConsole("We told " .. getPlayerName(theplayer) .. ": " .. getCancelReason()) -- Now tell everyone what the player tried to do
44
end
5-
addEventHandler ( "onVehicleStartEnter", huntedPlayer, stopVehicleEntry )
5+
addEventHandler("onVehicleStartEnter", huntedPlayer, stopVehicleEntry)
Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
function isEventHandlerAdded( sEventName, pElementAttachedTo, func )
2-
if type( sEventName ) == 'string' and isElement( pElementAttachedTo ) and type( func ) == 'function' then
3-
local aAttachedFunctions = getEventHandlers( sEventName, pElementAttachedTo )
4-
if type( aAttachedFunctions ) == 'table' and #aAttachedFunctions > 0 then
5-
for i, v in ipairs( aAttachedFunctions ) do
6-
if v == func then
7-
return true
8-
end
9-
end
10-
end
11-
end
12-
return false
1+
function isEventHandlerAdded(sEventName, pElementAttachedTo, func)
2+
if type(sEventName) == 'string' and isElement(pElementAttachedTo) and type(func) == 'function' then
3+
local aAttachedFunctions = getEventHandlers(sEventName, pElementAttachedTo)
4+
if type(aAttachedFunctions) == 'table' and #aAttachedFunctions > 0 then
5+
for i, v in ipairs(aAttachedFunctions) do
6+
if v == func then return true end
7+
end
8+
end
9+
end
10+
return false
1311
end
1412

1513
function onPlayerWasted()
16-
outputChatBox( getPlayerName( source ) .. ' died.' )
14+
outputChatBox(getPlayerName(source) .. ' died.')
1715
end
18-
addEventHandler( 'onPlayerWasted', root, onPlayerWasted )
16+
addEventHandler('onPlayerWasted', root, onPlayerWasted)
1917

20-
addCommandHandler( 'removeOnPlayerWastedEvent', function()
21-
if isEventHandlerAdded( 'onPlayerWasted', root, onPlayerWasted ) then
22-
outputChatBox( 'onPlayerWasted succesfully removed!' )
23-
removeEventHandler( 'onPlayerWasted', root, onPlayerWasted )
18+
addCommandHandler('removeOnPlayerWastedEvent', function()
19+
if isEventHandlerAdded('onPlayerWasted', root, onPlayerWasted) then
20+
outputChatBox('onPlayerWasted succesfully removed!')
21+
removeEventHandler('onPlayerWasted', root, onPlayerWasted)
2422
end
25-
end)
23+
end)
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local events = getEventHandlers ( "onClientMarkerHit", resourceRoot )
2-
for i,v in ipairs(events) do
3-
removeEventHandler ( "onClientMarkerHit", resourceRoot, v)
4-
end
1+
local events = getEventHandlers("onClientMarkerHit", resourceRoot)
2+
for i, v in ipairs(events) do
3+
removeEventHandler("onClientMarkerHit", resourceRoot, v)
4+
end
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
-- *****************************************************************************
22
-- CLIENT CODE
3-
local lastTriggerd = false
4-
addCommandHandler("trigger",function()
5-
local triggers = getLatentEventHandles() -- get all latent events
6-
if triggers[lastTriggerd] then -- you can use (getLatentEventStatus) too!
7-
outputChatBox("Wait until the trigger ("..lastTriggerd..") ends!",255,0,0)
8-
return
9-
end
10-
triggerLatentServerEvent("LatentEventsCheck",20000,resourceRoot,localPlayer)
11-
lastTriggerd = #getLatentEventHandles() -- set the lastTriggerd with the id for last event triggerd
3+
local lastTriggerd = false
4+
addCommandHandler("trigger", function()
5+
local triggers = getLatentEventHandles() -- get all latent events
6+
if triggers[lastTriggerd] then -- you can use (getLatentEventStatus) too!
7+
outputChatBox("Wait until the trigger (" .. lastTriggerd .. ") ends!", 255, 0, 0)
8+
return
9+
end
10+
triggerLatentServerEvent("LatentEventsCheck", 20000, resourceRoot, localPlayer)
11+
lastTriggerd = #getLatentEventHandles() -- set the lastTriggerd with the id for last event triggerd
1212
end)
1313

1414
-- *****************************************************************************
1515
-- SERVER CODE
16-
addEvent("LatentEventsCheck",true)
17-
addEventHandler("LatentEventsCheck",root,function (thePlayer)
18-
outputChatBox("Latent trigger done from: " .. getPlayerName(thePlayer), root,math.random(255),0,0)
19-
end)
16+
addEvent("LatentEventsCheck", true)
17+
addEventHandler("LatentEventsCheck", root, function(thePlayer)
18+
outputChatBox("Latent trigger done from: " .. getPlayerName(thePlayer), root, math.random(255), 0, 0)
19+
end)
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
function beginTransfer()
2-
triggerLatentServerEvent("blah", resourceRoot, myVeryLongString) -- Start latent event
3-
myHandle = getLatentEventHandles()[#getLatentEventHandles()] -- Get last latent event handle
4-
myTimer = setTimer( updateStatus, 1000, 0 ) -- Output status once a second
2+
triggerLatentServerEvent("blah", resourceRoot, myVeryLongString) -- Start latent event
3+
myHandle = getLatentEventHandles()[#getLatentEventHandles()] -- Get last latent event handle
4+
myTimer = setTimer(updateStatus, 1000, 0) -- Output status once a second
55
end
66

77
function updateStatus()
8-
local status = getLatentEventStatus(myHandle) -- Get latent event status
8+
local status = getLatentEventStatus(myHandle) -- Get latent event status
99
if not status then
10-
killTimer(myTimer) -- getLatentEventStatus returns false when the handle is no longer valid
10+
killTimer(myTimer) -- getLatentEventStatus returns false when the handle is no longer valid
1111
else
12-
outputConsole( "Transfer status:"
13-
.. " tickStart:" .. tostring(status.tickStart)
14-
.. " tickEnd:" .. tostring(status.tickEnd)
15-
.. " totalSize:" .. tostring(status.totalSize)
16-
.. " percentComplete:" .. tostring(status.percentComplete)
17-
)
12+
outputConsole("Transfer status:" .. " tickStart:" ..
13+
tostring(status.tickStart) .. " tickEnd:" ..
14+
tostring(status.tickEnd) .. " totalSize:" ..
15+
tostring(status.totalSize) .. " percentComplete:" ..
16+
tostring(status.percentComplete))
1817
end
19-
end
18+
end

0 commit comments

Comments
 (0)