Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Improved formatting and corrected some code
Source Link

I'm writing a SpriteKit game in swift and making use of GameplayKit's entity-component system. There are many components that do different stuff but share the same methods in which they do it, so I thought I'd group them into categories. At first I thought about using protocols, but component(ofType:) only accepts classes. So then I thought about making a base class for each category, define methods and variables there and have the actual components classes override them to implement their own functionality (you know like basic OOP). So I had something like:

    class InputComponent: GKComponent {
        func handleInput() {
            //Whatever
        }
    }

    class ControllerComponent: InputComponent {
        override func handleInput() {
            //Do something
        }
    }

    class KeyboardComponent: InputComponent {
        override func handleInput() {
            //Do something different
        }
    }

Then somewhere in my code I'd call

    entity.component(ofType: InputComponent)

and I thought I'd get both ControllerComponent and KeyboardComponent type objects. But I am getting none, it seems component(ofType:) doesn't return instances of subclasses.

I guess I can make an extension to GKEntity and define something like component<P>(conformingTo protocol: P)

component<P>(conformingTo protocol: P.Type)

and implement that behavior myself but I want to know if I can use the built in function to achieve the same.

I'm writing a SpriteKit game in swift and making use of GameplayKit's entity-component system. There are many components that do different stuff but share the same methods in which they do it, so I thought I'd group them into categories. At first I thought about using protocols, but component(ofType:) only accepts classes. So then I thought about making a base class for each category, define methods and variables there and have the actual components classes override them to implement their own functionality (you know like basic OOP). So I had something like:

    class InputComponent: GKComponent {
        func handleInput() {
            //Whatever
        }
    }

    class ControllerComponent: InputComponent {
        override func handleInput() {
            //Do something
        }
    }

    class KeyboardComponent: InputComponent {
        override func handleInput() {
            //Do something different
        }
    }

Then somewhere in my code I'd call

    entity.component(ofType: InputComponent)

and I thought I'd get both ControllerComponent and KeyboardComponent type objects. But I am getting none, it seems component(ofType:) doesn't return instances of subclasses.

I guess I can make an extension to GKEntity and define something like component<P>(conformingTo protocol: P) and implement that behavior myself but I want to know if I can use the built in function to achieve the same.

I'm writing a SpriteKit game in swift and making use of GameplayKit's entity-component system. There are many components that do different stuff but share the same methods in which they do it, so I thought I'd group them into categories. At first I thought about using protocols, but component(ofType:) only accepts classes. So then I thought about making a base class for each category, define methods and variables there and have the actual components classes override them to implement their own functionality (you know like basic OOP). So I had something like:

class InputComponent: GKComponent {
    func handleInput() {
        //Whatever
    }
}

class ControllerComponent: InputComponent {
    override func handleInput() {
        //Do something
    }
}

class KeyboardComponent: InputComponent {
    override func handleInput() {
        //Do something different
    }
}

Then somewhere in my code I'd call

entity.component(ofType: InputComponent)

and I thought I'd get both ControllerComponent and KeyboardComponent type objects. But I am getting none, it seems component(ofType:) doesn't return instances of subclasses.

I guess I can make an extension to GKEntity and define something like

component<P>(conformingTo protocol: P.Type)

and implement that behavior myself but I want to know if I can use the built in function to achieve the same.

Source Link

Use GKEntity's component(ofType:) with inheritance

I'm writing a SpriteKit game in swift and making use of GameplayKit's entity-component system. There are many components that do different stuff but share the same methods in which they do it, so I thought I'd group them into categories. At first I thought about using protocols, but component(ofType:) only accepts classes. So then I thought about making a base class for each category, define methods and variables there and have the actual components classes override them to implement their own functionality (you know like basic OOP). So I had something like:

    class InputComponent: GKComponent {
        func handleInput() {
            //Whatever
        }
    }

    class ControllerComponent: InputComponent {
        override func handleInput() {
            //Do something
        }
    }

    class KeyboardComponent: InputComponent {
        override func handleInput() {
            //Do something different
        }
    }

Then somewhere in my code I'd call

    entity.component(ofType: InputComponent)

and I thought I'd get both ControllerComponent and KeyboardComponent type objects. But I am getting none, it seems component(ofType:) doesn't return instances of subclasses.

I guess I can make an extension to GKEntity and define something like component<P>(conformingTo protocol: P) and implement that behavior myself but I want to know if I can use the built in function to achieve the same.