一般做法
本地化尽可能多的函数和变量。Lua 可以更快地读取它们
myVariable = false -- Don't use this
local myVariable = false -- Use this
function someFunction() -- Don't use this
print('Im a global function!')
end
local function someFunction() -- Use this
print('Im a local function!')
end
不要使用 table.insert,而是使用 tableName[#tableName+1] = data
function someFunction()
local table = {}
table.insert(table, {}) -- Don't use this
table[#table+1] = {} -- Use this
end
不要使用 if something ~= nil,而是使用 if something then。此方法同时检查 nil 和/或 false
function someFunction()
local bool = nil
if bool ~= nil then -- Don't use this
print('bool was not nil but also could be false!')
end
if bool then -- Use this
print('bool was neither nil or false!')
end
end
如果要创建函数或事件,请使其可以在具有不同变量的不同场景中使用。基本上保持通用
local function someFunction(param1, param2, param3)
print('Im a function that accepts 3 parameters which allows for multiple conditions!')
if param1 == 'something' then
print('I met condition number one!')
elseif param2 == 'somethingelse' then
print('I met condition number two!')
elseif param3 == 'somethingelsemore' then
print('I met condition number three!')
end
end
RegisterNetEvent('someEvent', function(param1, param2, param3)
print('Im an event that accepts 3 parameters which allows for multiple conditions!')
if param1 == 'something' then
print('I met condition number one!')
elseif param2 == 'somethingelse' then
print('I met condition number two!')
elseif param3 == 'somethingelsemore' then
print('I met condition number three!')
end
end)
编写代码时,对失败的条件使用所谓的 “short returns”
local function someFunction(param1, param2, param3)
if not param1 then return end
print('I met condition number one!')
if not param2 then return end
print('I met condition number two!')
if not param3 then return end
print('I met condition number three!')
end
本机用法
始终将 GetPlayerPed(-1) 替换为 PlayerPedId()
local function someFunction()
local ped = GetPlayerPed(-1) -- Don't use this
local ped = PlayerPedId() -- Use this
end
始终将 GetDistanceBetweenCoords 替换为 lua math aka #(vector3 – vector3)
local function someFunction()
local ped = PlayerPedId()
local pCoords = GetEntityCoords(ped)
local coords = vector3(-29.53, -1103.67, 26.42)
local dist = GetDistanceBetweenCoords(pCoords, coords, true) -- Don't use this
local dist = #(pCoords - coords) -- Use this
if dist < 5 then
print('Im within 5 distance units of the coords!')
end
end
原生研究站点/工具
- RedM 原生参考
- 本地人 |红色查找
- femga/rdr3_discoveries:《荒野大镖客:救赎 2》中的一些有趣内容。
- Halen84/RDR3-Native-Flags-And-Enums:包含本机的标志和枚举
循环
控制 while 循环及其运行时间
local function exampleLoop()
CreateThread(function()
while listen do
print('running while loop only when needed')
Wait(1)
end
end)
end
local listen = false
CreateThread(function()
LoopZone = CircleZone:Create(vector3(-851.63, 74.36, 51.86), 5.0, {
name = "ExampleLoop",
debugPoly = true,
})
LoopZone:onPlayerInOut(function(isPointInside)
if isPointInside then
listen = true
exampleLoop() -- Initiate loop
else
listen = false -- turns off when your outside the zone
end
end)
end)
- 如果您确实必须创建包含 “while” 循环的线程,请尽可能避免使用 “while true do”。如果您必须使用它,请按照下一个提示进行作,它不会对性能产生太大影响
- 通过使用追溯更改等待时间的变量来控制线程时间。因此,您可以将线程等待时间设置为 1000 毫秒,每秒检查一次 if 语句,如果它进入语句,则只需更改变量值即可缩短等待时间。等待 (睡眠)
如果您有特定于职业的 Loop,请确保它们仅适用于拥有该职业的玩家。不是警察的人没有理由在他们的机器上运行一个不适用于他们的循环
local function exampleJobLoop()
local job = RSGCore.Functions.GetPlayerData().job.name
CreateThread(function()
while job == 'law' do
print('im a lawman!')
Wait(1)
end
end)
end
安全建议
代码中安全性过剩并不是一件坏事。不要害怕添加多个 if 检查或创建随机变量来传递您的事件
切勿与玩家就资源客户端的金钱或物品进行任何类型的交易
事件处理程序
在资源中设置变量时,处理程序特别方便,因为不需要经常运行检查
-- These are client-side examples
local isLoggedIn = false
local PlayerData = {}
AddStateBagChangeHandler('isLoggedIn', nil, function(_, _, value) -- FiveM native method
if value then
isLoggedIn = true
PlayerData = RSGCore.Functions.GetPlayerData()
else
isLoggedIn = false
PlayerData = {}
end
end)
AddEventHandler('RSGCore:Client:OnPlayerLoaded', function() -- Don't use this with the native method
isLoggedIn = true
PlayerData = RSGCore.Functions.GetPlayerData()
end)
RegisterNetEvent('RSGCore:Client:OnPlayerUnload', function() -- Don't use this with the native method
isLoggedIn = false
PlayerData = {}
end)
RegisterNetEvent('RSGCore:Player:SetPlayerData', function(val)
PlayerData = val
end)
本站代码模板仅供学习交流使用请勿商业运营,严禁从事违法,侵权等任何非法活动,否则后果自负!
THE END
- 最新
- 最热
只看作者