How to assign a script to an actor runtime

Hi! We can get the script list of an actor,but is it possible to assign a script to an actor runtime?

Here is some docs about it:

To add script from code you can do sth like this:

class RuntimeScript : Script
{
    public override void OnStart()
    {
        Debug.Log("Hello!");
    }
}

class MyScript : Script
{
    public override void OnStart()
    {
        Actor.AddScript<RuntimeScript>();
        // or:
        new RuntimeScript
        {
            Parent = Actor
        };
    }
}

In your script yo can access Actor property which has methods to get or add scripts from code.

Than you !!! I’ve found the addScript method in c# api,but not in c++ api.
I’d like to use c++ so,is it possible in c++ too?

I don’t see the method either. For this you can use the second example demonstrated above by creating a new script object and setting its parent to the Actor. Here’s an example:

image

Tested and works.

Edit: Instead of using the new keyword, use the New<T>() function. It does proper memory allocation. In this case it would just be RuntimeScript* script = New<RuntimeScript>();.

Wonderful,thank you!!! You both helped me a lot :smiley: