Create components at runtime
I have a function external to flamework which components/services/controllers can call to create components at runtime without having to set the instance up with attributes or having to call some setup method so that components can always be expected to work solely on attributes. This makes it a lot easier to create components that depend on each other or need to construct each other without implementing special handling for components created outside of those cases.
I am sure this is not a proper way of doing this but seems to be the only one I can find. Currently it is producing an error that I am not sure how to fix:
I also have a gripe with it; creating the components dependency every time the function is called sounds like a bad idea, but I can't create it outside of the function since the script will call it before flamework is done loading:
Any help is appreciated 🙂
Solution:Jump to solution
Solution was to pass the component specifier to addComponent instead of the class:
```ts
/** @metadata macro */
export function makeComponent<T>(attributes?: { [key: string]: AttributeValue }, instance?: Instance, id?: Modding.Generic<T, 'id'>) {
const components = Dependency<Components>();...
2 Replies
Here is an example of how it is intended to be used:
This should just print "HELLO" to the console.
Ok, I think I have figured some of it out. I am passing the type of BaseComponent to addComponent which compiles to the specifier for the base component, which does not exist. I should be passing the specifier of
component but I am not sure how to get it.
I created a macro and take the component as a type parameter now however the macro appears to not be inserted into the caller
I expected something like this:
Solution
Solution was to pass the component specifier to addComponent instead of the class: