ModularM
Modular16mo ago
17 replies
banananas

Calling methods from list indexes

When I try running update from here:
alias CptList = List[ComponentList]

struct Entity:
    var components: CptList

    fn __init__(inout self, components: CptList):
        self.components = components
    
    fn update(inout self):
        for i in range(self.components.__len__()):
            self.components[i].update()
    
    fn addCpt(inout self, value: ComponentList):
        self.components.append(value)

It returns this error:
error: 'Variant[BaseComponent]' value has no attribute 'update'
            self.components[i].update()
            ~~~~~~~~~~~~~~~~~~^

ComponentList is defined here:
alias ComponentList = Variant[
    BaseComponent,
]

BaseComponent is defined here:
@value
struct BaseComponent: # example
    fn __init__(inout self): print("hi!!!!!")

    fn update(inout self): print("update method")
    fn render(inout self): pass
Was this page helpful?