Getting Players
これらのノードは、個々のプレイヤー、プレイヤーのグループ、または全プレイヤーを取得する際に役立ちます。
Networking.get LocalPlayer
VRCPlayerApi
この関数は Networking クラスのメンバーですが、ここに含まれていることに注意してください。
ローカルプレイヤーとは、現在この Udon スクリプトが実行されているプレイヤー、つまり「あなた自身」のことです。自分自身を把握しておくことは非常に重要です。
GetPlayerCount
int
呼び出された時点でのインスタンス内の実際のプレイヤー数を取得します。
GetPlayers
VRCPlayerApi[]
ワールド内の全プレイヤーを取得する方法です。これを使用することで、For ループで各プレイヤーを走査し、設定の適用や変更を行ったり、特定の名前を検索したりすることができます。
最も簡単な方法は、パラメータを受け取らないバージョンを使用することです。
![The bare minimum for a working call to GetPlayers. A better approach would be to construct VRCPlayerApi[] as a variable so you can reuse it.](https://creators.vrchat.com/img/worlds/graphgetplayers_alloc.png)
VRCPlayerApi[] players = VRCPlayerApi.GetPlayers();
for (int i = 0; i < players.Length; i++)
{
VRCPlayerApi player = players[i];
// Do something with the player...
}
上記のアプローチは機能しますが、使用するたびにメモリを確保するため、呼び出されるたびに VRCPlayerApi 配列が再構築されます。
このメソッドを頻繁(毎フレームなど)に使用する場合は、代わりにメモリを確保しないバージョンを使用してください。こちらでは、変数として保存しておいた VRCPlayerApi 配列を渡して毎回再利用します。このアプローチで注意すべき点は、配列のサイズよりもワールド内のプレイヤー数が多い場合、あふれた分のプレイヤーは取得できないということです。動的なプレイヤー数に対応するには、以下の手法を使用できます。最初はあらかじめ決められたサイズで配列を作成し、ワールド内のプレイヤー数が配列のサイズを超えた場合に必要に応じて拡張するという方法です。
![A more efficient pattern for using GetPlayers regularly. This approach constructs VRCPlayerApi[] as a variable that gets reused, only constructing new ones as the number of players in the world exceeds its capacity.](https://creators.vrchat.com/img/worlds/graphgetplayers_nonAlloc.png)
private VRCPlayerApi[] playersArray;
private void Start()
{
playersArray = new VRCPlayerApi[10]; // Start with a capacity of 10 players.
}
private void Update()
{
int playerCount = VRCPlayerApi.GetPlayerCount();
while (playerCount > playersArray.Length)
{
// Keep doubling the capacity until the array is large enough to hold all our players.
playersArray = new VRCPlayerApi[playersArray.Length * 2];
}
VRCPlayerApi.GetPlayers(playersArray);
for (int i = 0; i < playerCount; i++)
{
VRCPlayerApi player = playersArray[i];
// Do something with the player...
}
}
GetPlayerById
int
指定されたプレイヤー ID に対応する VRCPlayerApi オブジェクトが存在する場合、それを取得します。
get playerId
int
キャッシュされた PlayerId を取得します。まだキャッシュされていない場合は GetPlayerId を呼び出します。
GetPlayerId
int
ソースからプレイヤーの Network Id を取得します。
プレイヤー・タグ・システム
このシステムは、独自の変数やコレクションを作成することなく、プレイヤーに文字列を割り当てるための簡易的な方法です。
SetPlayerTag / GetPlayerTag
Set: string, string
Get: string
後で参照できる文字列変数を設定します。例えば、料理ゲームにおけるプレイヤーの「役割(role)」を「シェフ」や「客」として設定できます。その後、GetPlayerTagで「role」を指定すれば、「シェフ」または「客」を取得できます。
ClearPlayerTags
VRCPlayerApi
プレイヤーに設定したすべてのタグを削除します。
GetPlayersWithTag
現在機能していません。Udonでは利用できない List を返すためです。
将来的には、VRCPlayerApi オブジェクトの配列とタグを渡すと、そのタグが設定されているプレイヤーをメソッドが配列に格納するようになる予定です。
最終更新: