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】Citizen.Wait()とWait()について

Last updated at Posted at 2025-09-12

はじめに

これを書こうと思ったきっかけは2つの待機(Wait)があるのはなんでなんだろうと気になったからです。

まず結論から言うと、特別な理由がない限り、常に Citizen.Wait() を使用することが推奨されます。

-- 推奨される書き方
Citizen.Wait(1000)

-- 古い、非推奨な書き方
Wait(1000)

そもそもCitizen.Wait() / Wait() とは

この関数は、スクリプトの処理を一時的に停止(待機)させるために使われます。

Citizen.Wait() / Wait() は新しいか、古いかで、Wait()は古い形式で、下位互換性のため残されており、何かしら理由がないならCitizen.Wait()を推奨します。

FiveMのスクリプトでは、特定の処理を繰り返し実行するためにwhile true doのような無限ループがよく使われます。

以下のコードはWait()を入れていないため常に処理し続ける悪い例です。

sample1.lua
-- 例:Waitを入れないループ
Citizen.CreateThread(function()
    while true do
        print("Hello!")
    end
end)

そこで、必要最低限の処理をするために使うのがCitizen.Wait() / Wait()です。

sample2.lua
-- 例:Waitを入れた無限ループ
Citizen.CreateThread(function()
    while true do
        -- このループは1秒に1回だけ実行される
        Citizen.Wait(1000) -- 1000ミリ秒 (1秒) 待機
        print("Hello!")
    end
end)

Citizen.Wait(0)について

Citizen.Wait(0)のように引数に0を指定すると次のフレームまで待機するとなります。

これは毎フレームごとに何かをチェックし続けたい場合に便利です。

例えば、プレイヤーの座標を常に監視したり、キー入力をチェックしたりする処理に使います。

sample3.lua
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(0)

        local playerPed = PlayerPedId()
        local coords = GetEntityCoords(playerPed)
        
        print(coords) -- 座標を毎フレーム出力し続ける
    end
end)

しかし、毎フレーム実行する必要のない処理にCitizen.Wait(0)を使うのはパフォーマンスの無駄遣いです。「1秒おきに通知を出す」のような処理であれば、Citizen.Wait(1000)を使いましょう。

まとめ

Citizen.Wait()Wait()があるが、常にCitizen.Wait()を使うのがよい。

Wait系の関数は、無限ループによるリソースの枯渇を防ぐために必須。

引数には待機時間をミリ秒で指定する。

Citizen.Wait(0)は、毎フレーム実行したい処理にのみ使用する。

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?