蓝图中的转换

蓝图 中使用 Cast To 节点时,简单而言,就是在尝试检查发出转换的对象是否为被转换的特定对象。换言之,您创建了一个特殊的角色蓝图(如名为 MyCharacter),其中包含变量或其他自定义功能,并将其作为默认 Pawn 类(或所有玩家角色默认使用的角色蓝图)指定。

如需从另一个蓝图访问玩家角色的属性,可使用 Get Player Character 节点以常规方式(设置/获取其位置、旋转灯)对玩家角色产生影响,但无法访问已添加到 MyCharacter 蓝图的自定义功能,因为您获取的是玩家角色而非特定类型的角色。

使用 Get Player Character 节点,再使用 Cast To MyCharacter 节点(特殊的角色蓝图),即可确认玩家角色是否为 MyCharacter,以便访问该蓝图中所包含的变量、函数、事件或其他特殊功能。

在此页面中您可了解到转换的部分使用情况、查看 Cast To 节点的使用范例、了解关于目标蓝图转换的内容以及其他类型的转换。

范例使用情况

以下是几个 蓝图转换 使用范例:

  • 需要访问另一个蓝图的特殊版本。

    • 角色走进火焰中,导致体力值耗尽。

      • 投射到特殊的角色蓝图,以便访问并变更体力值。

    • 角色死亡,需要重新生成。

      • 投射到特殊的游戏模式蓝图,执行重新生成脚本。

  • 需要访问相同类的多个蓝图,并以相同方法进行修改。

    • 场景中拥有数盏灯,事件发生时需将它们开启或关闭。

      • 投射到灯蓝图并执行函数将灯关闭。

  • 需要访问一个特殊的子蓝图。

    • 存在基于一个动物蓝图(猫、狗、鸟)的数个蓝图,需要访问其中一个动物。

      • 投射到猫、狗和鸟,访问其相应的蓝图和特有功能。

设置一个 Cast To 范例

下例说明如何使用 Cast To 节点从一个蓝图中访问另一个蓝图。

For this example, we have a fire effect Blueprint in our level (which is an Actor) and we want it to communicate with the playable Character Blueprint the player is using. When the player enters the fire, we want to send a signal to the Character Blueprint that the player has entered the fire and that they should now take damage. By using the Return Value of an OverlapEvent, we can Cast To our Character Blueprint and access the Events, Functions, or Variables within it.

3_0a.png

  • The fire effect above is the Blueprint_Effect_Fire asset (included with starter content).

  • A sphere component named Trigger was added to the Blueprint and was set to OverlapOnlyPawn for its collision.

Using Blueprint Casting, we would do the following:

  1. The Character Blueprint assigned to the Default Pawn Class (the playable character) is our Target Blueprint we want to access.

    3_0b.png

    You can view the Default Pawn Class from the Edit menu under Project Settings in the Maps & Modes section.

  2. Now that we know our target is the MyCharacter Blueprint, inside it we create a Bool variable that states if the player Is on Fire.

    3_0c.png

    Above the Event Tick feeds a Branch where if True, we print Apply Damage to the screen (off True is where you would have your apply damage script).

  3. Inside the Blueprint_Effect_Fire Blueprint, we add two events for the Trigger: OnComponentBeginOverlap and OnComponentEndOverlap.

    3_1.png

  4. With the Events added, we drag off the Other Actor pin and enter Cast To My in the search field.

    3_2.png

    Here we check/assign the Actor (MyCharacter Blueprint) we want to trigger the event and Cast To it so that we may access it within the fire Blueprint.

  5. Select the Cast To MyCharacter option.

  6. With the node added, we can drag off the As My Character C pin and access the Events, Variables, Functions, etc. within it (in this case Set Is on Fire).

    3_3.png

  7. Both Events in the Blueprint_Effect_Fire Blueprint would then look like this.

    3_4.png

    When overlapping the fire, we are setting the IsOnFire variable in the MyCharacter Blueprint to True and setting it to False when not overlapping it. Inside the MyCharacter Blueprint, when IsOnFire is set to True via the fire Blueprint, we print to the screen Apply Damage (or if you have created a Health/Damage system, you could apply damage and reduce player's health here).

目标蓝图转换

There are also instances where you can use a variable to Cast To a specific type of Blueprint in order to access it.

CreateCasting.png

In the image above for number 1, a Save Game Object is created and assigned to a SaveGameObject variable. That variable is then used to Cast To a Save Game Blueprint called MySaveGame - which could be used to pass off or retrieve save game information such has a high score, best lap time, etc.

In the image above for number 2, a Widget Blueprint is created and assigned to a UserWidget variable. That variable is then used to Cast To a Widget Blueprint called MyWidgetBlueprint - which could be used to update or retrieve information from the Widget Blueprint (which could be a HUD or other UI element you want to access).

其他类型的转换

There are some special functions that can be used to Cast To different classifications of Blueprints.

OtherCasting.png

In the example graph above, the following examples are given:

Blueprint Description
Character (1) Here the Get Player Character node is used and we are casting to a Character Blueprint called MyCharacter.
PlayerController (2) Here the Get Player Controller node is used and we are casting to a Player Controller Blueprint called MyController.
Game Mode (3) Here the Get Game Mode node is used and we are casting to a Game Mode Blueprint called MyGame.
Pawn (4) Here the Get Controlled Pawn and Get Player Controller nodes are used to cast to a Pawn Blueprint called MyPawn.
HUD (5) Here the Get HUD and Get Player Controller nodes are used to cast to a HUD Blueprint called MyHUD.

In each of the examples above, dragging off the As My X (where X is the type of Blueprint) node will allow you to access the Events, Variables, Functions, etc. from their respective Blueprints.

Also of note, the Player Index value in the Get Player Character and Get Player Controller nodes can be used to specify different players in a multiplayer scenario. For a single player scenario, leaving these as 0 is fine.

Tags