Attach an object into a part of a boy item

HOW TO ATTACH AN OBJECT INTO A PART OF A BOY ITEM

I am using TypeScript within my project. I wonder how can I attach an item (a key) into another item (a boy) in the boy’s left hand.

I know I can do it without coding within the cospaces Designer but I want the object to be attached only when a certain action occurs. I don’t know how to do it. I have read the docs without any success. The only thing I have found related is #BaseItem.children.add but I don’t know what is the index parameter.

Fake it. Put it in their hand to begin with and hide it. Lets say they find the key on the shelf. Hide the one on the shelf and make the one in their hand appear.

Hi @pablornc,

Here is some code assuming, that you want to attach the key in the boy’s right hand, by clicking on the key. Of course, you can adjust that. (example - the plugin scene)


let boy = Scene.getItem("Casual boy") as AnimatedItem;

let hand_slot = boy.getSlot('HandR');

let key = Scene.getItem("Key") as AnimatedItem;

let key_slot = key.getSlot('Handle')



key.input.onClick(() => { key_slot.attachTo(hand_slot) })

Success!

1 Like

Note, also, that in Typescript you can use const instead of let for values which don’t change.

1 Like

Okey. I have discarted the human item as I want to use a camera. So my next question is how can I edit cospaces UI in order to add an image which represents the key. I have not found any reference into the docs.

Honestly, I have tried by creating an instance of a media item and using the add function to add, for example, an image but it isn’t shown.

I have not found any property which could depict to be like the src

Hoi @pablornc,

You don’t need any special code to add images as media items:

const pic = Scene.getItem("JzE3EkhB") as MediaItem;

You can do with these items whatever you need, for example change their opacity, like in GTC-NWC, plugin scene.

Oh. I thought the MediaItem was an item that when is created is displayed in the UI and you can attach different objects into it for being displayed.

The thing is that I was searching for a method in which I can make an image (MediaItem) be displayed in the UI and whenever I move the camera it moves with it. Like editing Cospaces UI.

With the above in mind. I wanted to display and image of the key I mention in my other messages instead of making it to be ‘physically’ in the game.

Maybe you can upload an image of a key (Library/upload/images/[key]web search), attach it to the camera, and then threat it as a media item?

it might well work. I’ll try.