![見出し画像](https://assets.st-note.com/production/uploads/images/118966865/rectangle_large_type_2_7794c445551577c51d378caf40057417.png?width=1200)
[UEFN&Verse]カギを拾って扉を開く
条件付きボタンを使えば簡単に組めると思っていたのだが
条件に設定できるアイテムが限られていて使い勝手が悪い
![](https://assets.st-note.com/img/1697359798441-z7Gv7LyhhO.png)
例えば扉を開けるカギや暗号を記したメモといったものを自由に設定したいわけだが現状では難しいようだ
今後は対応されるのかもしれないが、今できることを考えてみた
プロジェクトの準備
UEFNを起動してプロジェクトを作成する
テンプレートは何でも良いし、新規に作らなくても既にあるプロジェクト上でも良い
今回はTestProject004という名前で島テンプレートのBlankを指定している
![](https://assets.st-note.com/img/1697184926213-uDCRxTrJRX.png?width=1200)
鍵のかかった扉を用意する
まずはなんでもよいので開閉可能な扉を適当に配置する
壁とドアが一体になったパーツだ
![](https://assets.st-note.com/img/1697319756604-mhI8v7LqB5.png?width=1200)
次にAll > Fortnite > Deviceから仕掛けをロックするを探して配置する
別にどこにおいても構わないが何となく扉の上部などに付けたくなる
![](https://assets.st-note.com/img/1697320133984-zfpMXxQmtl.png?width=1200)
詳細のユーザーオプションにあるロックに扉のアイテムを指定する
![](https://assets.st-note.com/img/1697360821400-miO5lbian4.png)
ここで起動してみると扉は開かない
![](https://assets.st-note.com/img/1697320729130-dBC8yEdQKH.png?width=1200)
ボタンを押してロックを解除する
シンプルにボタンを押したら扉のロックが解除されるようにしてみる
All > Fortnite > Deviceからボタンを探して配置する
![](https://assets.st-note.com/img/1697324504449-78ZcrhUUhr.png?width=1200)
Verseファイルを作成する
名前はgame_managerとした
![](https://assets.st-note.com/img/1697324598246-hOMWufOxNT.png?width=1200)
game_manager.verseを編集する
button_deviceが押されたらlock_deviceをアンロックするだけのシンプルなコードだ
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
game_manager := class(creative_device):
@editable DoorSwitch : button_device = button_device {}
@editable DoorLock : lock_device = lock_device{}
OnBegin<override>()<suspends>:void=
DoorSwitch.InteractedWithEvent.Subscribe(SwitchOn)
SwitchOn(Agent : agent) : void=
DoorLock.Unlock(Agent)
ソースコードを保存してVerseコードをビルドする
ビルドに成功したらAll > TestProject004 > CreativeDevicesからgame_managerデバイスをシーンに配置する
![](https://assets.st-note.com/img/1697327260679-YOfxpFKat3.png?width=1200)
そのままgame_managerデバイスの詳細からDoorSwitch, DoorLockを指定する
![](https://assets.st-note.com/img/1697327386178-v5izbBA97k.png)
ここで動作確認
ボタンを押すとロックが解除する
![](https://assets.st-note.com/img/1697328190896-RfmL1xGG9h.png?width=1200)
カギを見つけて扉を開ける
ボタンを押したときに無条件にアンロックせずに条件をつけてみる
まずカギが隠されている場所を作るために追加のボタンをシーンに配置する
![](https://assets.st-note.com/img/1697352972630-NI9YmPNLvk.png)
このボタンのデザインだと雰囲気が出ないのでメッシを差し替える
カスタムメッシュにそれっぽいモデルを設定しインタラクトテキストもそれっぽい文章にする
![](https://assets.st-note.com/img/1697353397024-JzCgNl6DOo.png?width=1200)
今回はこちらの木箱を使わせてらった
カギを拾ったときなどにメッセージを出したいので
All > Fortnite > DevicesからHUDメッセージの仕掛けもシーンに配置する
![](https://assets.st-note.com/img/1697353871819-vPVmzBAgbj.png?width=1200)
game_manager.verseを編集する
以下がコード
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }
game_manager := class(creative_device):
@editable DoorSwitch : button_device = button_device {}
@editable WoodBox : button_device = button_device {}
@editable DoorLock : lock_device = lock_device{}
@editable Hud : hud_message_device = hud_message_device{}
var LockKey : logic = true
var HaveKey : logic = false
GetKey<localizes> : message = "カギをみつけた"
NoKey<localizes> : message = "何も見つからなかった"
NeedKey<localizes> : message = "カギが必要だ"
UseKey<localizes> : message = "扉の鍵が開いた"
OnBegin<override>()<suspends>:void=
DoorSwitch.InteractedWithEvent.Subscribe(SwitchOn)
WoodBox.InteractedWithEvent.Subscribe(SearchWoodbox)
SwitchOn(Agent : agent) : void=
if(LockKey = true):
if (HaveKey = true):
DoorLock.Unlock(Agent)
Hud.SetText(UseKey)
set LockKey = false
else:
Hud.SetText(NeedKey)
Hud.SetDisplayTime(5.0)
Hud.Show()
SearchWoodbox(Agent : agent) : void=
if(HaveKey = true):
Hud.SetText(NoKey)
else:
set HaveKey = true
Hud.SetText(GetKey)
Hud.SetDisplayTime(5.0)
Hud.Show()
木箱用のbutton_deviceとhud_message_deviceを追加する
@editable WoodBox : button_device = button_device {}
@editable Hud : hud_message_device = hud_message_device{}
カギの施錠/開錠のためのフラグと
カギを所持しているかどうかのフラグを追加
var LockKey : logic = true
var HaveKey : logic = false
色々出したいメッセージ
GetKey<localizes> : message = "カギをみつけた"
NoKey<localizes> : message = "何も見つからなかった"
NeedKey<localizes> : message = "カギが必要だ"
UseKey<localizes> : message = "扉の鍵が開いた"
扉のカギの処理
SwitchOn(Agent : agent) : void=
if(LockKey = true):
if (HaveKey = true):
DoorLock.Unlock(Agent)
Hud.SetText(UseKey)
set LockKey = false
else:
Hud.SetText(NeedKey)
Hud.SetDisplayTime(5.0)
Hud.Show()
木箱の処理
SearchWoodbox(Agent : agent) : void=
if(HaveKey = true):
Hud.SetText(NoKey)
else:
set HaveKey = true
Hud.SetText(GetKey)
Hud.SetDisplayTime(5.0)
Hud.Show()
ビルドしてWoodBoxとHudにデバイスを設定する
![](https://assets.st-note.com/img/1697354756657-lCZm7VaMd6.png)
ここで動作確認
木箱のなかから鍵を入手した後じゃないと扉をアンロックできないようになっている
![](https://assets.st-note.com/img/1697355179993-5nPq8AM5TK.png)
カギアイコンを表示する
このままでもメッセージでカギの入手を知らせてくれるが不親切なので入手したカギをどこかに表示しておきたい
まずはカギアイコンの画像をコンテンツブラウザのAll>TestProject004の下に登録する(Key.png)
![](https://assets.st-note.com/img/1697355631273-1OyMqsZPg9.png?width=1200)
こちらのイラストを使わせていただいた
登録したら念のためプロジェクトを保存する
Verse ExplorerからAssets.digest.verseを開き
この行が追加されていることを確認する
Key<scoped {TestProject004}>:texture = external {}
Verseではこの先頭のKeyという名前で画像にアクセスする
確認したらgame_manager.verseを編集する
これが全コードだ
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }
game_manager := class(creative_device):
@editable DoorSwitch : button_device = button_device {}
@editable WoodBox : button_device = button_device {}
@editable DoorLock : lock_device = lock_device{}
@editable Hud : hud_message_device = hud_message_device{}
var LockKey : logic = true
var HaveKey : logic = false
GetKey<localizes> : message = "カギをみつけた"
NoKey<localizes> : message = "何も見つからなかった"
NeedKey<localizes> : message = "カギが必要だ"
UseKey<localizes> : message = "扉の鍵が開いた"
OnBegin<override>()<suspends>:void=
DoorSwitch.InteractedWithEvent.Subscribe(SwitchOn)
WoodBox.InteractedWithEvent.Subscribe(SearchWoodbox)
SwitchOn(Agent : agent) : void=
if(LockKey = true):
if (HaveKey = true):
DoorLock.Unlock(Agent)
Hud.SetText(UseKey)
set LockKey = false
else:
Hud.SetText(NeedKey)
Hud.SetDisplayTime(5.0)
Hud.Show()
SearchWoodbox(Agent : agent) : void=
if(HaveKey = true):
Hud.SetText(NoKey)
else:
set HaveKey = true
ShowKeyIcon(Agent)
Hud.SetText(GetKey)
Hud.SetDisplayTime(5.0)
Hud.Show()
ShowKeyIcon(Agent :agent) : void=
if (Player := player[Agent], PlayerUI := GetPlayerUI[Player]):
NewUI := CreateWidget()
PlayerUI.AddWidget(NewUI)
CreateWidget() : canvas=
MessageWidget : canvas = canvas:
Slots := array:
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.5}, Maximum := vector2{X := 0.5, Y := 0.5}}
Offsets := margin{Left := -900.0, Top := 300.0, Right := 100.0, Bottom := 100.0}
Alignment := vector2{X := 0.0, Y := 0.0}
SizeToContent := false
Widget := texture_block:
DefaultImage := Key
return MessageWidget
追加した部分のコード
カギを拾ったタイミングでHUDに鍵アイコンを表示する
流れとしてはcanvasを作ってその中にパーツを配置しPlayerごとのUIに登録する
ShowKeyIcon(Agent :agent) : void=
if (Player := player[Agent], PlayerUI := GetPlayerUI[Player]):
NewUI := CreateWidget()
PlayerUI.AddWidget(NewUI)
CreateWidget() : canvas=
MessageWidget : canvas = canvas:
Slots := array:
canvas_slot:
Anchors := anchors{Minimum := vector2{X := 0.5, Y := 0.5}, Maximum := vector2{X := 0.5, Y := 0.5}}
Offsets := margin{Left := -900.0, Top := 300.0, Right := 100.0, Bottom := 100.0}
Alignment := vector2{X := 0.0, Y := 0.0}
SizeToContent := false
Widget := texture_block:
DefaultImage := Key
return MessageWidget
正直まだ詳細には理解できていないのだけど、以下の記事のほうが多少は詳しく説明しているので参考にして欲しい
動作確認
カギをとると左下に鍵アイコンが表示されるようになるのでわかりやすい
![](https://assets.st-note.com/img/1697359459470-5LQC3JQ060.png?width=1200)
フォートナイトはアイテム関連の機能が充実しているゲームなのにそれが全然利用できないのは悔しいところだが
まぁこの辺が今(2023/10)の限界だと思う
この記事が気に入ったらサポートをしてみませんか?