Functions in Typescript

I want to pick up a box when I press “e” when in a 3 meter proximity. I am trying to convert CoBlocks code into Type Script so I can get rid of collision with the camera while holding the box. Here are my Coblocks:

This is what I have so far in TypeScript:

let camera = Scene.getItem(“8T2GQDc1”) as CameraItem;

let box1 = Scene.getItem(“mieWUi3c”) as Cuboid;

let box1dist = camera.center.dist(box1.center);

while (true)

{

Input.onKeyDown(pickUpBox(),'e');

}

function pickUpBox(): () => void {

    box1.physics.enabled = false;

    camera.add(box1);

}

It seems that “() => void” has a problem saying, “A function whose declared type is neither ‘void’ nor ‘any’ must return a value.” I don’t know what it means.

Try this:

function pickUpBox(){

    box1.physics.enabled = false;

    camera.add(box1);
}

I’ve tried this before, but unfortunately now it says that “onKeyDown(pickUpBox(),‘e’)” is the problem, stating that “Argument of type ‘void’ is not assignable to parameter of type ‘() => void’.” I’m confused, because I just deleted that piece of code.

1 Like

That’s because the handler parameter in your Input statement has parentheses, which aren’t needed when you’re calling an external handler - use parentheses when you want a function to run right now, and without when you’re passing it as something to run/call later:

Input.onKeyDown(pickUpBox,'e');

Oh, usually you would need a parameter, but in this case apparently not. Thanks!