Calling a function defined in the Event Editor from the scripting world?

1 favourites
  • 8 posts
From the Asset Store
A simple Map Editor that where you can edit a map with restriction or totally. You can save and load the map created.
  • Hey all,

    I'm starting to learn about the scripting system and I have a couple of questions. The most important being the title of this thread:

    #1 - I know I can call a function defined in the scripting world from the event editor. But is it possible to do the reverse? i.e. call a function defined in the Event Editor from the scripting world?

    #2 - Are global game objects available in the scripting world? For example, I don't seem to be able to call Touch.X - which normally works in expressions in the event editor.

    #3 - Lastly. I figured out how to pass a local variable from the event editor to the scripting world. i.e. "localVars.foo" – However is there a way to pass global variables? This is similar to #2 honestly.

    Thanks!

  • Aaand... I figured it out for questions 1 and 3. But I still can't figure out a solution for #2.

    For anyone else that encounters this and wants to know:

    #1

    The trick is that you need access to the runtime object. Be sure to always pass the runtime to the scripting world from the event editor when you call the script function. For example you script function should be defined as:

    function foobar(runtime, x, y) {
     ...
    }
    

    You can have whatever other variables you want but I'm guessing a best practice is to always make 'runtime' the first variable.

    Then inside your scripting function you can call an event editor function via runtime thus so:

     runtime.callFunction("eventFunction", [1, 2, 3]);
    
    

    The array will be used as the ordered arguments of the function call.

    #3

    This was achieved via the runtime object as well:

     runtime.globalVars.foo
    
  • Take a look at some of the built-in scripting examples. They'll show the basics, like for #2 you access objects through runtime.objects.

  • Oh interesting...

    I did try that but none of the functions of the Touch object seem to be accessible. Is that right?

    I looked at the alien game example recreated in scripting and I can see that this works:

    runtime.objects.Keyboard.isKeyDown("ArrowRight")
    

    But on the other hand, none of these work:

    runtime.objects.Touch.X
    runtime.objects.Touch.TouchCount
    runtime.objects.Touch.XAt(0)
    

    Do only some objects have access exposed to scripts? I think ideally you'd want to make it so that any expression in the event editor be made available to scripting, right?

  • Expressions are not directly accessible from script. The available properties and methods are documented in the scripting reference section of the manual.

  • Ahh interesting.

    The bindings for these core objects are very limited then? For example when I look at the Touch object, it appears to have only one function? (requestPermission). Although I can now see that you have a set of runtime events such as mouse down which would presumably provide access to "on touch is down" as well.

    Are there plans to expose the ACEs of objects to the scripting interface? It would seem like a very logical progression of the scripting experience, right?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Events and JavaScript programming are different paradigms which is why they are not directly callable. Things like picking instances are fundamental concepts that only apply to events and have no built-in equivalent in JavaScript, so things are done differently. We're expanding the available APIs over time, it's a huge amount of work to cover everything.

  • Ok. It's good to hear that there's a plan to expand the API.

    I can understand that not everything is directly portable. Hopefully some things (like Touch.TouchCount) are more straightforward than others though.

    And thanks for answering some of this over the weekend Ashley.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)