データを処理する際に、特定のデータ型から別のデータ型に変数を変換する必要があることがよくあります。 たとえば、計算結果を表示するには、float (浮動小数点数) から string (文字列) に変換する必要があります。
Verse 内のすべての型変換、つまり、オブジェクトを別のデータ型に変換するには、ToString() などの関数を使用するか、乗算 (*) などの演算子を使用する必要があります。 特定のデータ型から別のデータ型への明示的な変換は、型変換とも呼ばれます。
float (浮動小数点数)から int (整数) に変換する
float から int への変換では、浮動小数点数から整数への変換方法を明示的に指定する関数が必要です。 以下の関数はすべてこの変換を行いますが、それぞれ動作が異なります。 状況に応じて、最も適切な関数を自分で決定する必要があります。
この例では、異なる関数が 4 つの float リテラル値を、or 演算子を使用して int 値に変換し、失敗コンテキストを作成します。 次に、set でその値を int 型の変数に代入します。
# The Int[] method has the <decides> effect,
# This means that we need to account for failure as it is a failable expression.
# This results in the following
# var WoodInt:int = failable expression or the value if it fails.
var WoodCollectedFloat:float = 10.5
var WoodInt:int = Int[WoodCollectedFloat] or 0
Print("Printing WoodInt Value (10): {WoodInt}")
# Similar to Int[], Floor[], Ceil[], and Round[] also have the <decides> effect
この例では、if 式がこれらの失敗する可能性がある関数の失敗コンテキストを作成し、set でその値を int 型の変数に代入します。
var WoodCollected:int = 0
var StoneCollected:int = 0
var GoldCollected:int = 0
var FoodCollected:int = 0
if:
# This block is the condition of the if expression
# Which creates the failure context
# If any fail, the entire chain of execution is rolled back
# And the else branch, if it exists, is executed
int (整数) から float (浮動小数点数) に変換する
乗算演算子 (*) は、乗算を実行する前に整数を浮動小数点数に変換します。 データ型を int から float に変換するには、整数を 1.0 で乗算します。
このコードでは、vector3 変数 の宣言で使用できるように、乗算によって int 変数 StartingPositionX を float に変換します。 データ型 vector3 では、X、Y、Z フィールドに float 型の値が必要です。
# Required for the vector3 type
using { /UnrealEngine.com/Temporary/SpatialMath}
var StartingPositionX:int = 960
# CurrentX = 960.0
var CurrentX:float = StartingPositionX * 1.0
var CurrentPosition:vector3 = vector3{X := CurrentX, Y := 0.0, Z := 0.0}
Print("CurrentX: {CurrentX}")string (文字列) に変換する
ToString() 関数か、ToString() 関数を呼び出す文字列補間を使用すると、複数のデータ型を文字列に変換できます。 現在、以下のデータ型は、Verse では組み込み関数 ToString() を含んでいます。
float
int
[]char
char
vector2
vector3
rotation
次の例では、文字列の補間と ToString() 関数によって変数が文字列に変換されているのがわかります。 どちらの方法でも結果は同じです。文字列補間は ToString() を呼び出すためです。
var WoodCollected:int = 100
# Convert using string interpolation
Print("WoodCollected: { WoodCollected }")
# or ToString() function
Print("WoodCollected: " + ToString(WoodCollected))
var InitialDistance:float = 3.625
# Convert using string interpolation
Print("InitialDistance: { InitialDistance }")
# or ToString() function
カスタム データ型を文字列に変換する
カスタム データ型も、そのデータ型用の ToString(custom_type) 関数を実装することで文字列に変換できます。 ToString(custom_type) 関数がある場合は、文字列補間はこの関数を使用してデータ型を自動的に文字列に変換します。
以下は、果物の列挙型のカスタム関数 ToString() の例です。
fruit := enum:
Apple
Banana
Strawberry
ToString(Fruit: fruit):string =
case(Fruit):
fruit.Apple => "Apple"
fruit.Banana => "Banana"
fruit.Strawberry => "Strawberry"
以下は、カスタム クラスのカスタム関数 ToString() の例です。 なお、ToString() 関数は waypoint クラス外で宣言されています。 SetDestination() 関数では、Destination の文字列補間がカスタム関数 ToString() を呼び出しています。
# Custom class with constructor and a ToString() function
waypoint := class():
DisplayName:string
Position:vector3 = vector3{}
MakeWaypoint<constructor>(Name:string, X:float, Y:float, Z:float) := waypoint:
DisplayName := Name
Position := vector3{X := X, Y := Y, Z := Z}
ToString(Waypoint: waypoint):string =
オブジェクト参照を別の型に変換する
次の構文を使用すると、オブジェクトへの参照を異なるクラスまたはインターフェースに明示的に変換 (つまり型変換) できます。
if (NewObjectReference := object_type_to_cast_to[ObjectReference]) {}object_type_to_cast_to は、参照の変換を試行しているクラスまたはインターフェースを表します。 これは失敗する可能性がある式です。指定された型にオブジェクトを変換できない場合、型変換が失敗するためです。 オブジェクト参照をクラスに変換しようとしても、そのクラスがオブジェクトの型、スーパークラスの型、またはオブジェクトのクラスが実装しているインターフェースに一致しない場合は失敗します。
このコードではインターフェース positionable、positionable を継承する抽象クラス形状、および形状の 2 つのサブクラス triangle および square を宣言します。 その後、MyShape という square 型のオブジェクトを作成し、このオブジェクトの他の 3 つの型への型変換を試行します。 以下は、結果の詳細です。
| square 変換先の型 | 結果 |
|---|---|
|
|
|
|
|
|
# Class and interface definitions
positionable := interface() {}
shape := class<abstract>(positionable) {}
triangle := class(shape) {}
square := class(shape) {}
# Create a square object referenced using the superclass type shape
MyShape:shape = square{}
# This will succeed since MySquare is a square object
最後の例では、型変換は成功しますが、不要です。 このコードも同じ結果になります。
MyDrawable:positionable = MyShape型変換を使用する例
UEFN におけるオブジェクトの型変換の使用例の 1 つは、特定の型のアクタを見つけ、その型に基づいて関数を呼び出すことです。 これを実行する方法については、「ゲームプレイ タグでアクタを見つける」を参照してください。