Why does my code fail at a certain point?

My code fails at the if statement followed by a print call but I have no idea why. Can someone please enlighten me?

SpacePenguinsGame := class(creative_device):

@editable CharProp : creative_prop = creative_prop{}
@editable Mutator : mutator_zone_device = mutator_zone_device{}
@editable var SpacePlayers : []Abilities = array{}

UpdateModel ( Agent : agent )<suspends> : void =
    if (FortCharacter := Agent.GetFortCharacter[]):
        # Hide Player character
        FortCharacter.Hide()
        # Moves replacement character according to player position
        loop:
            if (FortCharacter.IsActive[]):
                Sleep(0.0)
                PlayerPos := FortCharacter.GetTransform().Translation
                CharPos := CharProp.GetTransform().Translation

                DistanceFromPlayer := Distance(PlayerPos, CharPos)
                    
                DirectionCharToPlayer := PlayerPos - CharPos

                Angle := ArcTan(DirectionCharToPlayer.X, DirectionCharToPlayer.Y) - 3.14 / 2.0
                # Change Z to -1 to have rotation match characters
                NewRotation := MakeRotation( vector3{ X := 0.0, Y := 0.0, Z := 0.0} , Angle) 

                CharProp.MoveTo(PlayerPos, NewRotation, 0.15)

HandleAgentTrigger ( Agent : agent ) : void =
    spawn:
        UpdateModel(Agent)

# On Start detect Mutator entry to activate classes/functions
OnBegin<override>()<suspends>:void=
    Mutator.AgentEntersEvent.Subscribe(HandleAgentTrigger)
    AllPlayers := GetPlayspace().GetPlayers()
        for (IDX := 0..AllPlayers.Length - 1):            
            if (PlayerAbility := SpacePlayers[IDX], Player := AllPlayers[IDX]):
                PlayerAbility.Init(Player)

Abilities := class():

@editable var VFXSpawner : vfx_spawner_device = vfx_spawner_device{}
@editable var DamageDevice : damage_volume_device = damage_volume_device{}
@editable var AbilityButton : button_device = button_device{}
@editable var SFXPlayer : audio_player_device = audio_player_device{}

var Player : ?player = false

# Disable sound and damage devices on start while listening for button activity
Init(IPlayer : player):void=
    VFXSpawner.Disable()
    DamageDevice.Disable()
    AbilityButton.InteractedWithEvent.Subscribe(OnAbilityButtonActivated)

# When button is activated activate ability function
OnAbilityButtonActivated(Agent : agent):void=
    if (IsAgentSameAsPlayer(Agent)?):
        Print("Hello, world!")
        spawn:
            UseAbility()

IsAgentSameAsPlayer(Incoming : agent)<transacts>:logic=
    if (IPlayer := player[Incoming]):
        if (Player? = IPlayer) { return true }
        else { return false }
    
    return false

# Enables ability VFX and Damage device for (time) and then disables them
UseAbility()<suspends>:void=
    VFXSpawner.Enable()
    if (Agent := agent[Player?]) {SFXPlayer.Play(Agent)}
    DamageDevice.Enable()
    Sleep(2.0)
    DamageDevice.Disable()
    VFXSpawner.Disable()

Abilities should be a class<concrete> if you want to expose its variables, idk why the compiler doesn’t tell you so.

What’s the Print statement you’re talking about ?

Not sure why the post left out the class because I copied and pasted but it is there. I will post a screenshot below and hopefully that will make it clearer.

Everything works until the OnAbilityButtonActivated if statement. So something must be wrong with my IsAgentSameAsPlayer function, right?

So, first of all, you need to display the code between 3 ticks, using this button, what happened is that it used the Verse tags as Markdown formatting, which is used on this forum.
image

For your solution, I think the function works, even if I think there’s an inline solution to it.
Your problem is that you’re comparing Incoming : agent to Player which is never set, so your Player is always false as you defined it here : var Player : ?player = false

Maybe you forgot to set the player in the Init(Player: player) function ?

set Player = option{IPlayer}

Awesome thank you so much! That makes so much sense and I appreciate your time in helping me out. It is always small things missed that make me want to rip out my hair.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.