Casting one (parent) class to another (child) class issue

Heyo,

I have this class function in a child/sub class called itemBox. It inherits from the parent/super class spawnableThing. Both have a function called Spawn_this, and it’s overridden in the itemBox child class. In that function I try to cast a creative_object (which could be teleporter devices, collectible_object_devices, anything Fortnite offers as a device…) to the more specific collectible_object_device to do specific functions from that collectible object class. Does anyone know about using classes within classes and parent-child (super-subclass) relationships and why I can’t get the Show() and RespawnForAll() functions of collectible_object_device to actually work?

Notes:

  • I made this class called spawnableThings
  • spawnableThings has a creative_object optional variable called spawnableDevice
  • in the function I try to cast the creative_object variable named spawnableDevice as a collectibe_object_device in this case to do more specific things, such as call Show() and RespawnForAll() (which are methods of collectible_object_device )

Here is some simplified reduced code that I made to help show what I’m doing:


# ===================================
# spawnable Thing class
# ===================================

spawnableThing := class<unique><concrete>:
    # parent class for spawnable things like item boxes, teleporter class, etc.
    # idea was to store any creative device from Fortnite's selection, like collectible object device, teleporter device, etc and then handle general shared things like "teleport to new location" functions without writing those individually in sub classes

    var spawnableDevice<protected> : ?creative_object = false

    # class functions ----- 
    Spawn_this<public>(itemManager_counter : itemManagerStats, newSpot : spawnableSpot):void =
        # "spawn" this object by moving it from original position to new position
        # note: specific devices like collectible objects should hide / unhide in their own overrided function of this function

        # moving object will be done by spawnableSpot function
        # hiding / unhiding object will be done in specific object subclass overridden method
        # here, we just set the objects status flags and timer

# ===================================
# itemBox class
# ===================================

itemBox := class<unique><concrete>(spawnableThing):
    # class for holding item box info, which is a collectible_object_device that players can collect

    # class variables
    var isSpecial<private> : logic = false

    # class functions ----- 
    Spawn_this<override>(itemManager_counter : itemManagerStats, newSpot : spawnableSpot):void =
        # "spawn" this item box object

        # moving object will be done by spawnableSpot function
        # here, we unhide the object / respawn it
        if (thisCollectibleDevice : collectible_object_device = collectible_object_device[spawnableDevice?]):
            thisCollectibleDevice.Show()
            thisCollectibleDevice.RespawnForAll()
            (super:)Spawn_this(itemManager_counter, newSpot) # calls super class version of spawn function to finish setting flags

I’m particularly trying to verify this part is working

        if (thisCollectibleDevice : collectible_object_device = collectible_object_device[spawnableDevice?]):
            thisCollectibleDevice.Show()
            thisCollectibleDevice.RespawnForAll()
            (super:)Spawn_this(itemManager_counter, newSpot) # calls super class version of spawn 

Ok my bad guys xD I had something else going on that made the collectible object hidden at the start of the game, and I didn’t realize it until now that it was messing with my debug test results lol. It happens but everything appears to work so far!