vErr:S76: Expected block, got "nearest_player" following "if" Verse Error

Hi to all
i imported a godzilla model from sketchfab that had a (walking, Idle, Roar), i imported it to UEFN, the animation Idle file is named (godzillaIdle); the animation walk file is named (godzillawalk); the animation attack file is named (godzillaroar), i made a verse code for the NPC, but now am stuck on this problem
vErr:S76: Expected block, got “nearest_player” following “if”(3100) [Ln43 ,Col16]
here’s the code

using { /Verse.org }
using { /Verse.org/Native }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/SceneGraph }

A Verse-authored component that can be added to entities

Godzilla_behaviour := class(component):

# A custom variable you can expose to the editor
@editable
var MyCustomInt<public>:int = 10

# Define variables for Godzilla NPC behavior
var npc : fort_character_entity
var attack_radius : float = 1000.0 # Set the radius for detecting players
var attack_interval : duration = 3.0 # Time between attacks

# Runs when the component should start simulating in a running game.
OnBeginSimulation<override>():void =
    # Run OnBeginSimulation from the parent class before
    # running this component's OnBeginSimulation logic
    (super:)OnBeginSimulation()

    # Assuming the entity this component is attached to is the Godzilla NPC
    npc = fort_character_entity(cast(self.get_entity()))

    # Start NPC behavior
    self.OnBegin()

# Runs the NPC behavior logic
OnBegin():void =
    loop
        # Wait a small interval before checking for players again
        sleep(0.1)

        # Get all players in the world
        players := fort_player_manager().get_all_players()

        # Find the nearest player within the attack radius
        nearest_player := self.FindNearestPlayer(players)

        # Check if the nearest player is valid
        if nearest_player is not none
        {
            # If a player is found, attack them
            self.AttackPlayer(nearest_player)
        }
        else
        {
            # Play idle animation if no players are nearby
            npc.play_animation("godzillaIdle")
        }

# Function to find the nearest player within the attack radius
FindNearestPlayer(players : []fort_character_entity) : fort_character_entity?:
    var nearest : fort_character_entity? = none
    var min_distance : float = attack_radius
    
    # Iterate through all players to find the nearest one
    for player in players
    {
        distance := vector_distance(npc.get_position(), player.get_position())
        if distance < min_distance
        {
            min_distance = distance
            nearest = player
        }
    }
    
    return nearest

# Function to attack the nearest player
AttackPlayer(player : fort_character_entity):void =
    # Ensure player is not none before proceeding
    if player is not none
    {
        # Play the attack animation
        npc.play_animation("godzillaroar")

        # Apply damage to the player (customize as needed)
        damage := 50.0
        player.apply_damage(damage)
        
        # Wait before attacking again
        sleep(attack_interval)

        # Play walk animation as it moves towards the next target
        npc.play_animation("godzillawalk")
    }



Hi @Kaydo007 :

The syntax you have in your sample provided is incorrect.

Please refer to: If | Unreal Editor for Fortnite Documentation | Epic Developer Community

You need to surround the condition of the if in parentheses. So for example, the line:

if player is not none

Should be

if (player):

The same goes for the for macro as well.

Additionally, there are a bunch of other issues, such as Self needing the S capitalized (which, in your case, you don’t need to specify in the places you’re using it, since you’re not passing the class instance itself as a value - please refer to Class | Unreal Editor for Fortnite Documentation | Epic Developer Community for more details). There’s also no such thing as a none value, for the line:

    var nearest : fort_character_entity? = none

I think you’re trying to say:

var nearest:?fort_character_entity = false

In Verse, an unset optional evaluates to false. Please refer to: Option | Unreal Editor for Fortnite Documentation | Epic Developer Community

1 Like