How to create a clip instance from assets via code?

Fill this out this info to make it easier for the community to help you! If you don’t fill out this information, your post may be deleted or removed.

What Wick Editor Version are you using?
1.19.3

Describe the Problem
I want to create a clip from assets via code

What have you tried so far?
Have you attempted anything et to fix the problem? Let us know!

Do you have a Wick Editor File that we can see? Optional*
Attach a .wick file if you have one in progress that can help us help you!

To place a clip asset with code the following lines can be used.

let myAsset = project.project.getAssetByName(MY_ASSET_NAME);

project.project.createClipInstanceFromAsset(myAsset, MY_ASSET_X, MY_ASSET_Y, (clip)=>{});

The first line will retrieve the asset from the project. Replace the MY_ASSET_NAME with the name of the asset as a string.

For example:
let myAsset = project.project.getAssetByName("myClip.wickobj");

The second line then places the clip in the variable named myAsset from the first line at a position with a x value of MY_ASSET_X and with a y value of MY_ASSET_Y.

For example:
project.project.createClipInstanceFromAsset(myAsset, 360, 240, (clip)=>{});

The final bit in the second line of (clip)=>{} is a function that will run once the clip has been placed with the parameter of clip that is the clip placed. This is useful if you what to do something immediately to the clip or to save it to a variable to be used later.

For example to place out a clip asset with a name of myClip.wickobj at position [360,240] with
a rotation of 25:

let myAsset = project.project.getAssetByName("myClip.wickobj");

project.project.createClipInstanceFromAsset(myAsset, 360, 240, (clip)=>{
    clip.rotation = 25;
});
2 Likes

how do I name the clip?

Inside the function quoted above you could use clip._identifier = CLIP_NAME; to set the name of the clip. For example:

project.project.createClipInstanceFromAsset(myAsset, 360, 240, (clip)=>{
    clip._identifier = "myNewClip";
});

Note that if you are trying to access the clip via the name that you set it to that it very well may be undefined at times because the clip might not be created or named by that frame.

To get around a issue like above, when you want to access the clip you can check that it isn’t undefined. For example if you have another clip that wants to try to access to clip that has to be created you could do something like the following:

let myClip = myNewClip;

if (myClip !== undefined) {
    // since its not undefined the clip should be ready to be accessed
    myClip.rotation += 2;
} else {
    // the clip is undefined and not quite ready yet
}

thanks, that was very helpful

From testing you actually would want it to look like this

let myClip = window.myNewClip;

if (myClip !== undefined) {
    // since its not undefined the clip should be ready to be accessed
    myClip.rotation += 2;
} else {
    // the clip is undefined and not quite ready yet
}

You need to specify the window. in the first line.

1 Like

what does “window.” do?

When accessing clips with a name for something like someClip.gotoAndStop(); what the line is interpreted as widow.someClip.gotoAndStop(); unless a local variable is declared with the same name as the clip. So really the clip is in the window object. Values in the window object can be accessed in javascript without having to write the window. as long as there isn’t a local variable that has that name. The reason that it is required here is because if the clip that is going to be created is going to be access by a line of code elsewhere but it doesn’t exist yet when the line of code run with let myClip = myNewClip; it doesn’t find a local variable with that name nor does it find that variable declared in the window and therefore outputs a error that there is no myNewClip any where. By adding the window. though we specify that the value should be grabbed from the window object even if it is undefined. This means that no error will appear and stop the project since it will get the value from the window object even if it is undefined.

ok, thanks, btw, I made it to use easier

here is a thing click a clip and press ctrl+e

  1. it’s already solved
  2. I know
  3. from the assets via code, not downloading clips