Is there anyway to access a script’s method anonymously without knowing the script’s name?
Here’s my example – I have a bunch of ‘ability’ prefabs that I’m adding as properties to my player script. Abilities are interchangeable and they all share a ‘trigger’ method. I need to be able to call trigger on whichever ability I pass into the player, but getComponent needs a specific script name. Is it possible to do something like this.ability.getComponent(cc.TypeScript).method()
?
Looks like the perfect use case scenario for event emitting and listening.
Kindly look up the procedure in api and let me know if it suits your requirement.
@shishir451 That’s a really good point. In this case it’s probably best to use events if I’m unable to call a method anonymously like that.
can’t you create a base class with this trigger thing and extend it for every new ability? I think you could getComponent(baseClass) and you will get the ability class.
Or do the event approach, it’s more robust for the case of more than one ability in the node
1 Like
Just wanted to follow up and mention that @randonzord suggestion worked well. You can do getComponent on the base class and it’ll grab the extended class of that base class. Eventually I may have to use events to build a more robust system but this works for the purposes of my prototype.