How do functions actually work?

0 favourites
  • 6 posts
From the Asset Store
_______ Huge collection of metal fixtures ________
  • construct.net/en/make-games/manuals/construct-3/project-primitives/events/functions

    It seems like experienced users use functions more than others, and that it's a preferable to use them as often as you can.

    However, it's not super clear to me how functions function.

    How are triggers checked?

    construct.net/en/make-games/manuals/construct-3/project-primitives/events/how-events-work

    Rather than running once per tick, this event simply runs (or "fires") upon something actually happening. In this case, the event runs when the user hits the Spacebar key on the keyboard. It is never checked any other time.

    How can that work?

    How can it just miraculously know to run the trigger when that the button is pressed?

    Why doesn't the computer need to check every tick to see if that button is being pressed?

    Why aren't all triggers checked to see if they're true every tick?

    For me, functions feel magical and mysterious compared to regular events, where we know they run from top to bottom, being checked in a logical way.

    Does this mean triggers are always better for performance because they're not always being checked like regular events are?

    If you have hundreds of triggers from objects that are currently all destroyed, does that mean that these events/triggers effectively don't exist, they're never checked?

    For example, the trigger "On Collision", wouldn't you need to check that just as often as "Is overlapping"?

    And with ones like "On animation finished", wouldn't the engine need to keep checking to see what the animation is up to?

    Thank you for clarifying!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • A good solution would be to have two slightly different arrow icons. One for true triggers and one for fake triggers.

  • Functions are simply a way to re-use chunks of events. They are only run when you call the function.

    Triggers can be thought of as functions. If it helps some things like input are probably checked regularly like non triggers but that check is done at some lower level point in the system. Like mouse clicks are checked in the mouse driver, which is taken by the operating system, sent to the browser, which loops over the events and sends them to the js event listeners, which in turn is handled by construct’s engine.

    Other triggers come from checks in other places. For example on frame changed comes from the sprite objects tick function which updates the frames. There it calls the trigger when the frame changes.

    Some things are so called fake triggers, but that’s mainly a term in the sdk. There aren’t too many examples of them, off the top of my head on collision, gamepad triggers, timer triggers, and I want to say in the Los behavior. It’s more of an exemption to the rule, and they act like a normal event with a trigger once.

    In collision is interesting since it’s both a fake trigger and a real one. It’s kind of like an is overlapping and a trigger once but it’s done per instance It’s also a normal trigger because the physics behavior calls it when the physics engine has a collision.

    Anyways, functions are just a way to organize and reuse code.

    In general you can think of triggers as functions that don’t run every tick, and instead are called by other parts of the runtime. But like many parts of the engine there are exemptions where things may work differently.

  • R0J0hound - I've noticed that sometimes when I try to make a function, when I try to call it in an action it will show up in the "functions" section of the add function action, and sometimes they wont?

  • In construct 3 if you make the function return a value it won’t show up in actions but it will show up in expressions.

  • Thanks for explaining R0j0, I think I was also mixing up triggers and functions.

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