ChilloutVRでLuaを使う方法
Luaとはスクリプト言語(プログラミング言語)です。
ChilloutVRにおいては、コンポーネントのみでは難しい挙動や処理を実現できます。
ですが、ChilloutVRでのLua実装はまだ開発中なので、ChilloutVRでLuaを使用するにはベータ版にアクセスする必要があります。
※2024年9月16日時点の情報を基にしています。
ベータ版にアクセスする
ChilloutVR公式のDiscordの-experimental-newsにある以下の投稿から、ベータ版のアクセスコードを入手します。
※Scriptingの方
※投稿を見るには、ChilloutVR公式のDiscordに入っておく必要があります。
Steamのライブラリから、ChilloutVRのプロパティを開きます。

「ベータ」タブに移動し、コードを入力し、「コードを確認」を押します。
その後、「オプトイン~」を押します。

以下のような表示になります。


Lua環境をセットアップする
Visual Studio Codeをインストールします。
※Visual Studio Codeとは、Luaを記述するためのコードエディターです。
CCKのプロジェクトを開きます。
※事前にCCKのプロジェクトをセットアップしている前提で進めます。
Alpha Blend Interactive->Scripting->Set Up VSCodeを選択します。

Visual Studio CodeにLuaの拡張機能をインストールします。
Hello World
ログに「Hello, world!」と表示するPropを作成します。
シーンにCubeを作成します。

CubeにCVRSpawnableをアタッチします。

CVRLuaClientBehaviourをアタッチします。

プロジェクト内に「Lua」というフォルダーを作成します。(必須ではありません)
「Lua」フォルダーに「Hello World」というフォルダーを作成します。(必須ではありません)
CVR Lua Scriptを作成します。

名前を「helloworld.lua」にします。

Visual Studio CodeでCCKのプロジェクトのAssetsフォルダーを開きます。

helloworld.luaに以下の内容を記述します。
-- Start is called before the first frame update
function Start()
print "Hello, world!"
end
-- Update is called once per frame
function Update()
end
helloworld.luaをCVRLuaClientBehaviourのScriptAssetに設定します。

Propとしてアップロードします。
Propをスポーンすると、
%UserProfile%\AppData\LocalLow\Alpha Blend Interactive\ChilloutVR\Player.log
にログが出力されます。
例:
[Lua] /CVRSpawnable_84df4c4c-f727-44af-b45f-e73e202f80dd_78925301(Clone):Client Lua[CVRSpawnable_84df4c4c-f727-44af-b45f-e73e202f80dd_78925301(Clone)]: print: Hello, world!
※ベータ版(scripting)でないと出力されません。
セキュリティについて
以下の記事のざっくりとしたまとめになります。
クライアントを決して信用しない
改ざんのおそれがあるため
暗証番号ギミックなど
改ざん対策として、暗号ハッシュを使用する
適用範囲
自分のスクリプト内に保持したいコードがある場合は、関数にlocalキーワードを使用する
所持金を増やす関数など
サンプル
※随時追加予定です。
FunnyCube
オブジェクトをランダムな軸で回転させます。
数秒ごとに新しい位置にテレポートします。
-- From scripting team meeting notes:
-- First use case: Cubespin with random start position, random direction, resets after 10s.
UnityEngine = require "UnityEngine"
CCKComponents = require "ABI.CCK"
-- LuaLS/LuaCATS annotations start with three dashes (---)
--- @type UnityEngine.Time
Time = UnityEngine.Time
--- @type UnityEngine.Random
UnityRandom = UnityEngine.Random
ORIGINAL_POSITION = UnityEngine.NewVector3(0, 0, 0)
ROTATION_AXIS = nil
nextBehaviourChange = 0.0
function Start()
-- Seed random noise.
math.randomseed(math.floor(Time.time))
-- Print "Hello world!" to Debug console.
print("Hello world!")
-- Record original position.
ORIGINAL_POSITION = UnityEngine.NewVector3(gameObject.transform.position.x, gameObject.transform.position.y,
gameObject.transform.position.z)
end
function Update()
if Time.realtimeSinceStartup > nextBehaviourChange then
i = math.random(1, 6)
if i == 1 then
ROTATION_AXIS = UnityEngine.NewVector3(1, 0, 0)
elseif i == 2 then
ROTATION_AXIS = UnityEngine.NewVector3(0, 1, 0)
elseif i == 3 then
ROTATION_AXIS = UnityEngine.NewVector3(0, 0, 1)
elseif i == 4 then
ROTATION_AXIS = UnityEngine.NewVector3(-1, 0, 0)
elseif i == 5 then
ROTATION_AXIS = UnityEngine.NewVector3(0, -1, 0)
elseif i == 6 then
ROTATION_AXIS = UnityEngine.NewVector3(0, 0, -1)
end
gameObject.transform.position = ORIGINAL_POSITION + UnityRandom.insideUnitSphere
nextBehaviourChange = Time.realtimeSinceStartup + 10.0
end
if ROTATION_AXIS ~= nil then
gameObject.transform.Rotate(ROTATION_AXIS)
end
end
※サンプルコードから、
--- @type CVR.CCK.CVRSpawnable
spawnable = gameObject.GetComponentInParent("ABI.CCK.Components.CVRSpawnable")
if spawnable ~= nil then
spawnable.ForceUpdate()
end
この部分を削除しないと動きませんでした。
