How to embed a JS object into Skulpt

27 views
Skip to first unread message

Sergei Zherebtsov

unread,
Jan 15, 2020, 10:45:56 AM1/15/20
to Skulpt
Hello.

I'm trying to embed some custom js functions and js objects into Skulpt in my javascript application.

For example the way I embedded a function:

Sk.builtins.moveRight = function() { person.xPosition += 1 }

So, when I run a simple script - Sk.importMainWithBody('myScript', false, 'moveRight()', true); -  the function moveRight is triggered in js and changes the 'person' position. It works fine.

But I have an issue with injecting a js-object to Skulpt. For instance, the former way is not working and I'm getting error "AttributeError: '<invalid type>' object has no attribute 'moveRight' on line 1"
Sk.builtins.hero = {
  moveRight: () => person.xPosition += 1
}

Sk.importMainWithBody('myScript', false, 'hero.moveRight()', true);


Appreciate any help or ideas. Thanks!

BR,
Sergey
Reply all
Reply to author
Forward
0 new messages