0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FiveMのプラグイン開発 メモ

Posted at

使いそうなの

個人的に使いそうだなと思ったやつを一部まとめます。
スニペットともいいます。

コマンド

client.lua
RegisterCommand('test', function(args)
    print("test command.")
)
-- ゲーム内チャットで /test で呼べる

Config(設定ファイル)

config.lua
Config = {}
Config.Value   = 10.0
Config.Locale  = "ja"

必ずfxmanifestに記述してください。

fxmanifest.lua
...

shared_scripts {
    'config.lua'
}

...
client.lua
-- fxmanifestに書いてあれば require とかはいらないです。
local value = Config.Value
local locale = Config.Locale

print

client.lua
print("test") -- testと出力される。
print("テスト") -- 文字化けする。

スレッド

  • 非同期で実行したいときに便利。
client.lua
Citizen.CreateThread(function()
    while true do
        print("Hello world!")
        Citizen.Wait(1000) -- 待機時間(ミリ秒)
    end
end)

Wait を入れ忘れると、負荷が高まりクラッシュする可能性があります。

描画関係

スクリーンへの描画。

client.lua
-- 線を描画
DrawLine_2d(x1, y1, x2, y2, lineWidth, r, g, b, a)

-- 四角形を描画
DrawRect(x, y, width, height, r, g, b, a)

四角形は必ず塗りつぶされます。枠線のみが必要な場合は DrawLine_2d を使用してください。

座標系

client.lua
-- Entityの座標
local playerCoords = GetEntityCoords(PlayerPedId())
print(playerCoords) -- vector3で返される。

-- ワールド座標をスクリーン座標へ変換する。
local isOnScreen, screenX, screenY = GetScreenCoordFromWorldCoord(boolean, screenX, screenY)

GetScreenCoordFromWorldCoordについて
最初の返り値は「スクリーン内に映っているかどうか」を示すboolean。

GetWorldCoordFromScreenCoordというワールド座標へ変更する方もあります。

イベント関連

  • クライアント側からサーバーイベントのトリガー
client.lua
TriggerServerEvent('custom_event')
server.lua
-- カスタムイベントは必ずRegisterNetEventをしないとフックできません。
RegisterNetEvent('custom_event')
AddEventHandler('custom_event', function()
    print("Server event has")
end)
  • サーバー側からクライアントイベントのトリガー
client.lua
-- カスタムイベントは必ずRegisterNetEventをしないとフックできません。
RegisterNetEvent('custom_event')
AddEventHandler('custom_event', function()
    print("Client")
end)
server.lua
TriggerClientEvent('custom_event')

TriggerClientEvent(クライアントをトリガー)、TriggerServerEvent(サーバーをトリガー)の混同に注意。

https://docs.fivem.net/docs/scripting-reference/events/list/
に乗っているやつはRegisterNetEventしなくても大丈夫っぽい?

プレイヤー関連

client.lua
local health = GetEntityHealth(PlayerPedId())
local armor = GetPedArmour(PlayerPedId())

print("player health : " .. health)
print("player armor  : " .. armor)

プレイヤーのヘルスとかアーマーとか他にも色々取得できます。

車両

client.lua

-- Ped(何らかのキャラクター)が乗り物に乗っているかどうか
if IsPedInAnyVehicle(PlayerPedId(), false) then
    print('Local player is in a vehicle')
end

local playerPed = PlayerPedId()

-- Pedが乗っている車両を取得
local vehicle = GetVehiclePedIsIn(playerPed, false)
print("Vehicle ID: " .. vehicle)

-- 組み合わせると便利

QBCore 編

アイテムの使用

client.lua
-- アイテムを使えるようにする
function QBCore.Functions.CreateUseableItem(item, cb)
    QBCore.UseableItems[item] = cb
end

-- アイテムが使えるかのチェックを推奨
function QBCore.Functions.CanUseItem(item)
    return QBCore.UseableItems[item]
end

-- アイテムを所持しているかどうか
local hasItem = QBCore.Functions.HasItem('my_cool_item')
print(hasItem)

通知

client.lua
-- 文字化けせず日本語が使える
QBCore.Functions.Notify("通知が呼び出されました", "success")

-- 前者と違い、赤くエラー表記になる
QBCore.Functions.Notify("通知が呼び出されました", "error") 

まとめ

使えそうなのがあればまた追記していきます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?