Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak(client/server): gamemode #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions ctf-gamemode/ctf_client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
File: ctf_client.lua
Description:
This is the main client file and its main purpose is:
- To handle the following client related logic executed via our main thread: (see Citizen.CreateThread).
- To handle the following client related logic executed via our main thread: (see CreateThread).
- To perform team selection and camera manipulation if the user is in camera selection: (see boolean bInTeamSelection).

- To handle certain client-side flag logic:
Expand Down Expand Up @@ -79,11 +79,11 @@ AddEventHandler("SetObjectiveVisible", function(flagEntityNetID, bVisible)
-- Call NetToEnt to get the entity handle from our net handle (flagEntityNetID), which is sent by the server.
local flagEntity = NetToEnt(flagEntityNetID)

print("SetObjectiveVisible: " .. GetEntityArchetypeName(flagEntity) .. " to our player, owner is: " .. GetPlayerName(NetworkGetEntityOwner(flagEntity)))
Citizen.Trace("SetObjectiveVisible: " .. GetEntityArchetypeName(flagEntity) .. " to our player, owner is: " .. GetPlayerName(NetworkGetEntityOwner(flagEntity)))

SetEntityVisible(flagEntity, bVisible)
else
print("AttachFlagToPlayer: Something terrible happened, where's our flag?")
Citizen.Trace("AttachFlagToPlayer: Something terrible happened, where's our flag?")
end
end)

Expand Down Expand Up @@ -359,7 +359,7 @@ function processFlagLogic(flagEntityNetID)
local playerPed = PlayerPedId()
if entityHasEntityInRadius(ent, playerPed) and not IsEntityDead(playerPed) then
TriggerServerEvent("requestFlagUpdate")
Citizen.Wait(500)
Wait(500)
end
end
end
Expand Down Expand Up @@ -422,25 +422,26 @@ spawnmanager:setAutoSpawn(true)
----------------------------------------------- Threads -----------------------------------------------

-- Process the client flag logic for each flag every tick
Citizen.CreateThread(function()
CreateThread(function()
while true do
if not bInTeamSelection then
if receivedServerTeams and #receivedServerTeams > 0 then
for _, team in ipairs(receivedServerTeams) do
if not isInTeamSelection() then
local teamReceived = getReceivedServerTeams()
if teamReceived and #teamReceived > 0 then
for _, team in ipairs(teamReceived) do
-- Run flag logic if they are not in camera/team selection
if team.id ~= TeamType.TEAM_SPECTATOR then
processFlagLogic(team.flagNetworkedID)
end
end
end
end
Citizen.Wait(0)
Wait(0)
end
end)

--- Our main thread.
-- We use this thread to handle team selection and to process any flag related logic.
Citizen.CreateThread(function()
CreateThread(function()
while true do
if receivedServerTeams and #receivedServerTeams > 0 then
if shouldGoIntoTeamSelection() and not bIsAttemptingToSwitchTeams then
Expand All @@ -466,6 +467,6 @@ Citizen.CreateThread(function()
-- Process all rendering logic for this frame
ctfRenderingRenderThisFrame()

Citizen.Wait(0)
Wait(0)
end
end)
30 changes: 15 additions & 15 deletions ctf-gamemode/ctf_rendering.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- Draws instructional UI on the screen: (see drawScaleFormUI).
- Contains helper methods to draw text on the screen.
Event handlers:
- SendClientHudNotification: Dispatched by the server to display simple 'toast' UI notifications on the client.
- SendClientHudNotification: https://docs.fivem.net/docs/resources/example-resources/events/ctf-gamemode/SendClientHudNotification/
]]

-- Store UI configuration from ctfConfig.UI.
Expand Down Expand Up @@ -67,7 +67,7 @@ function ctfRenderingRenderThisFrame()
end

if isInTeamSelection() then
DisableRadarThisFrame()
HideMinimapExteriorMapThisFrame()
HideHudAndRadarThisFrame()
DrawScaleformMovieFullscreen(buttonsHandle, 255, 255, 255, 255, 1) -- Draw the instructional buttons this frame

Expand Down Expand Up @@ -121,7 +121,7 @@ end

function buttonMessage(text)
BeginTextCommandScaleformString("STRING")
AddTextComponentScaleform(text)
AddTextComponentSubstringKeyboardDisplay(text)
EndTextCommandScaleformString()
end

Expand All @@ -135,20 +135,20 @@ function drawScaleFormUI(buttonsHandle)

CallScaleformMovieMethod(buttonsHandle, 'CLEAR_ALL') -- Clear previous buttons

PushScaleformMovieFunction(buttonsHandle, "SET_DATA_SLOT")
PushScaleformMovieFunctionParameterInt(2)
BeginScaleformMovieMethod(buttonsHandle, "SET_DATA_SLOT")
ScaleformMovieMethodAddParamInt(2)
ScaleformMovieMethodAddParamPlayerNameString("~INPUT_SPRINT~")
buttonMessage(btnCaptions.Spawn)
PopScaleformMovieFunctionVoid()

PushScaleformMovieFunction(buttonsHandle, "SET_DATA_SLOT")
PushScaleformMovieFunctionParameterInt(1)
BeginScaleformMovieMethod(buttonsHandle, "SET_DATA_SLOT")
ScaleformMovieMethodAddParamInt(1)
ScaleformMovieMethodAddParamPlayerNameString("~INPUT_ATTACK~")
buttonMessage(btnCaptions.PreviousTeam)
PopScaleformMovieFunctionVoid()

PushScaleformMovieFunction(buttonsHandle, "SET_DATA_SLOT")
PushScaleformMovieFunctionParameterInt(0)
BeginScaleformMovieMethod(buttonsHandle, "SET_DATA_SLOT")
ScaleformMovieMethodAddParamInt(0)
ScaleformMovieMethodAddParamPlayerNameString("~INPUT_AIM~") -- The button to display
buttonMessage(btnCaptions.NextTeam) -- the message to display next to it
PopScaleformMovieFunctionVoid()
Expand Down Expand Up @@ -182,9 +182,9 @@ function drawTxt(x,y,width,height,scale, text, r,g,b,a)
SetTextDropShadow()
SetTextOutline()
-- Let's use our previously created text entry 'textRenderingEntry'
SetTextEntry("textRenderingEntry")
AddTextComponentString(text)
DrawText(x - width/2, y - height/2 + 0.005)
BeginTextCommandDisplayText("textRenderingEntry")
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayText(x - width/2, y - height/2 + 0.005)
end

--- https://forum.cfx.re/t/draw-3d-text-as-marker/2643565/2
Expand All @@ -205,9 +205,9 @@ function Draw3DText(x, y, z, scl_factor, text)
SetTextDropShadow()
SetTextOutline()
-- Let's use our previously created text entry 'textRenderingEntry'
SetTextEntry("textRenderingEntry")
BeginTextCommandDisplayText("textRenderingEntry")
SetTextCentre(1)
AddTextComponentString(text)
DrawText(_x, _y)
AddTextComponentSubstringPlayerName(text)
EndTextCommandDisplayText(_x, _y)
end
end
20 changes: 10 additions & 10 deletions ctf-gamemode/ctf_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
- CTFGame:shutDown: Handles cleanup tasks upon game shutdown, including flag and team destruction.

Event Handlers:
- playerJoining: Handles player joining events, triggering team data updates for the client.
- requestTeamData: Responds to client requests for team data.
- sendTeamDataToClient: Sends team data to clients for UI and game state updates.
- assignPlayerTeam: Assigns a team to a player.
- playerJoining: https://docs.fivem.net/docs/resources/example-resources/events/cft-gamemode/playerJoining/
- requestTeamData: https://docs.fivem.net/docs/resources/example-resources/events/cft-gamemode/requestTeamData/
- sendTeamDataToClient: https://docs.fivem.net/docs/resources/example-resources/events/cft-gamemode/sendTeamDataToClient/
- assignPlayerTeam: https://docs.fivem.net/docs/resources/example-resources/events/cft-gamemode/assignPlayerTeam/

Classes:
- Team: Represents a team in the CTF game mode.
Expand Down Expand Up @@ -109,7 +109,7 @@ function Team:createBaseObject()

-- wait until it has been created
while not DoesEntityExist(baseEntity) do
Citizen.Wait(1)
Wait(1)
end

-- Now that it's created we can set its state
Expand Down Expand Up @@ -216,12 +216,12 @@ end
-- @see NetworkGetNetworkIdFromEntity
-- @see EFlagStatuses
function Flag:spawn()
print('Spawning flag at: ' .. self.spawnPosition)
print('^5[INFO] ^7Spawning flag at: ' .. tostring(self.spawnPosition))
-- Calls server setter CREATE_OBJECT_NO_OFFSET, to create an entity on the server
local flagEntity = CreateObjectNoOffset(self.modelHash, self.spawnPosition)

while not DoesEntityExist(flagEntity) do -- wait until it has been created
Citizen.Wait(1)
Wait(1)
end

-- Make the object fall so it doesn't stay still in the air by setting the z-velocity
Expand Down Expand Up @@ -349,7 +349,7 @@ end
--- Sets the position of the flag entity.
-- @param position (vector3) The new position to set for the flag.
function Flag:setPosition(position)
print("setPosition: " .. position .. " entity: " .. tostring(self.entity))
print("^5[INFO] ^2setPosition: ^5" .. position .. " ^2entity: ^5" .. tostring(self.entity))
SetEntityCoords(self.entity, position.x, position.y, position.z, true, true, true, true)
end

Expand Down Expand Up @@ -724,9 +724,9 @@ AddEventHandler('playerDropped', function (reason)
end)

-- Main game loop
Citizen.CreateThread(function()
CreateThread(function()
while true do
ctfGame:update()
Citizen.Wait(500) -- Adjust the interval as needed
Wait(500) -- Adjust the interval as needed
end
end)
7 changes: 4 additions & 3 deletions tdm-gamemode/fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
fx_version 'bodacious'
game 'gta5'

author 'You'
version '1.0.0'
author 'CFX'
description 'First CFX gamemode!'
version '1.0.1'

client_script 'tdm_client.lua'
server_script 'tdm_server.lua'
shared_script {
'tdm_shared.lua',
'tdm_locales.lua',
'tdm_config.lua'
}
Loading