Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to skip init and start when adding a component #3

Open
signorpipo opened this issue Mar 17, 2023 · 2 comments
Open

Add a way to skip init and start when adding a component #3

signorpipo opened this issue Mar 17, 2023 · 2 comments

Comments

@signorpipo
Copy link
Contributor

When I want to clone a component, and with this I don't mean just creating the same component with the same starting variables, but a clone of the current state, with possibly deep cloning and more elaborate clone decisions, I may want to skip init and start.

This is not different from the copy constructor of C++, which does not execute the normal constructor because with a copy u may prefer to do some other things.
This means that in this case it should not be needed to call init and start if u don't want to and can instead be harmful and u may have to do more work in order to deinitialize stuff that is done in the init and start method.

It would be interesting to have a clone method on the component itself in the official API, but I think it would still be interesting to have the chance to add a component without init and start to allow for custom features to be made, should not hurt if u are doing it on purpose.

@DavidPeicho
Copy link
Collaborator

I think the idea would be:

class Component {
    copy(params?: Component) {
        if (params !== undefined) {
            const ctor = component.constructor as ComponentConstructor;
            for (const key in params) {
                if (!(key in ctor.Properties)) continue;
                (component as Record<string, any>)[key] = params[key];
            }
        }
        return this;
    }
}

And then people can override that in their components:

class Component {
    copy(params?: Component) {
        // I don't do anything because I am evil!
        return this;
    }
}

@signorpipo
Copy link
Contributor Author

signorpipo commented Mar 17, 2023

Would copy be a static method? Maybe the reverse can be done, a clone method where u specify the object on which to add the component?

Edit:
For example

class Component { clone(targetObject) { return targetObject.addComponent(this.type, this); } }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants