メインコンテンツへスキップ

weak_map(session, [agent]test_class) を使い、プレイヤーごとに複数のグローバル変数を作成する方法

    グローバル変数を定義
    ・データ型は weak_map( session, [agent]player_global_data )

    var PlayerGlobalDataMap : weak_map( session, [agent]player_global_data ) = map{}

    Playerごとに割り当てるクラスを定義

    player_global_data := class():
     MyInt : int = 0
     MyFloat : float = 0.0

    他クラス内の関数からグローバルデータへアクセス
    ・OnBegin関数内でweak_mapを初期化(プロジェクト内で一回初期化すればOK(なはず。))

    GlobalDataTest1 := class(creative=device):
    
     @editable
     MyButton1 : button_device = button_device{}
    
     OnBegin<override>()<suspends>:void=
      if(set PlayerGlobalDataMap[GetSession()]=map{})
      MyButton1.InteractedWithEvent.Subscrive(OnMyButton1)
    
     OnMyButton1(PlayerAgent:agent):void=
        if( set PlayerGlobalDataMap[GetSession()][PlayerAgent].MyInt += 1 ):
      #プレイヤーのデータがない場合、初期設定
        else if(set PlayerGlobalDataMap[GetSession()][PlayerAgent] = player_global_data{})
    
      if(Value := PlayerGlobalDataMap[GetSession()][PlayerAgent].MyInt):
       Print("{Value}")

    複数のクラスからアクセスできるかテストする為、GlobalDataTest2を作成
    ・OnBegin内でPlayerGlobalDataMap[GetSession()]の初期化はしない

    GlobalDataTest2 := class(creative=device):
    
     @editable
     MyButton2 : button_device = button_device{}
    
     OnBegin<override>()<suspends>:void=
      MyButton2.InteractedWithEvent.Subscrive(OnMyButton2)
    
     OnMyButton2(PlayerAgent:agent):void=
        if( set PlayerGlobalDataMap[GetSession()][PlayerAgent].MyInt += 1 ):
      #プレイヤーのデータがない場合、初期設定
        else if(set PlayerGlobalDataMap[GetSession()][PlayerAgent] = player_global_data{})
    
      if(Value := PlayerGlobalDataMap[GetSession()][PlayerAgent].MyInt):
       Print("{Value}")


    検証動画(※UEFNセッションでの検証です。)
    左のボタンがGlobalDataTest1クラスのボタン
    右のボタンがGlobalDataTest2クラスのボタン

    サブ垢を一度退出させ、同じセッションに入りなおす
    個別プレイヤーのデータは同じセッションでも初期化される


    まとめ
    グローバル変数で使用可能なデータ型には制限があります
    https://dev.epicgames.com/documentation/en-us/uefn/using-persistable-data-in-verse

    weak_mapには2種ある
    ・weak_map( session, t )  ➡今のセッションでデータを保存する型
                  (ラウンド間はまたげないかも?)
    ・weak_map( player, t )    ➡プレイヤーごとに永続データを保存する型​​

    今回は(session, t)型を使用しました。
    (player, t)型に関しては、今後まとめれたら記事書きます。

     
     

    AKIMA

     
     
    Fortnite Map Creator / UEFN

    あなたへのおすすめ