What can I use as a simple event/callback system?

0 favourites
From the Asset Store
14 amazing sound files of game events like Level Ups, Level Completes, object spawn, object taking etc.
  • When I opened Construct 3 after the last update, it warned me that the old "Function" plugin (a carryover from C2) is deprecated and may be removed. In my project, I use that alongside the new-style C3 functions. My reason is that where the C3 functions have a single definition, the old Function plugin lets you define a function in multiple places. This, to me, makes it a perfect event/callback system. A piece of code can fire off an event without knowing if there will be a piece of code in the current event sheet that listens for it.

    For example, I have a callback-style function called "checkpointDone". Note that I changed the name of the "Function" plugin to "Callbacks" to reduce ambiguity.

    In this code, I have a standard function that will save the game at a checkpoint.

    Then, in this code for a specific level, the game is supposed to wait for the checkpoint to finish saving before it does something.

    I have many, many other uses of this plugin, including the callback when my universal menu cursor selects something. In this specific case, this would easily be handled by custom object events (similar to custom actions), if only C3 had those.

    Is there another method I could do to accomplish the same purpose?

  • Maybe I'm misunderstanding you but you can still call functions using C3 and I don't see an issue.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • My problem is that C3 functions can only have one definition in one event sheet, where as the Function plugin lets you define a response to the same function call multiple times.

    I need callbacks that can have multiple different effects depending on what event sheets are loaded. For example, my "on menu item selected" callback that's fired from my global Menus event sheet, which has a different callback listener on each event sheet that has menu items defined in it.

  • How you are using the functions sounds alien to me. I see it as a repeatable set of actions. If I wanted to have different outcomes I would send through different parameters.

  • Okay, how about this case then: control inputs.

    My game supports both keyboard and gamepad inputs. All of these inputs are customizable with an in-game menu.

    I have 2 kinds of checks, similar to the built-in Keyboard & Gamepad plugins:

    * inputDown(control): Checks if the given input is being held down. Implemented with a native function. Checked with an expression.

    * inputPressed(control): Event called when this button is pressed. Implemented with the Functions plugin.

    For example, here's the code in my Controls event sheet that fires these events:

    And here's the code in my Menus event sheet that listens to keyboard input while a menu cursor is active:

    And here's the code in my Player event sheet that also listens to some of the same events:

    So basically, when I press a keyboard/gamepad button, the controls module sends out a callback that a playable character or a menu cursor or something else might possibly listen for. I can't make a single function out of all the possible things that might happen when you press a button.

  • You probably won't be able to make a single function out of every outcome of a key press which is why I wouldn't use functions to begin with because what you're describing is not repeatable bits of code. I use events based on the key pressed, not detect any key pressed then pass the key pressed through a function and determine it's outcome all inside one function, seems unnecessary.

    If I press 'down' then the player falls through a platform, that's one event. In a separate event if I press down and I am in the menu then other things may happen based off menu=1, I don't need functions.

    You can also make use of groups. If you are in the menu then you can disable the player controls group for example.

    Maybe some hardcore developer has got an idea about these 'callbacks' and will respond with their input but I wouldn't use them myself.

  • Right. I am not doing this for repeatable bits of code. That's what I use the native Functions feature for.

    I'm only using the old Function plugin because it just so happens to provide exactly the functionality I need for callbacks.

    If we had custom events on objects, I could replace a lot of this. But I can't.

    I'm not asking how functions work. I am asking if there is anything else out there that could do exactly (or approximately) what I am doing now.

  • Maybe I am misunderstanding this so if this doesn't make sense don't think too much about it, having said that...

    Have you tried adding a Signal at the end of your function? You can then have different "On Signal" triggers in different event sheets and each can do different actions.

  • Unfortunately, signals don't take parameters, which I rely on in many places.

  • Yeah, I miss the flexibility of old functions. I used similar callback-style functions in C2 projects.

    I think the nearest solution with C3 function is to use some universal naming system for them and call them by name. For example, you can have functions inputPressed_layoutname:

    inputPressed_Level1

    inputPressed_Level2

    inputPressed_Menu

    Then you can call the function for current layout using a bit of scripting:

    try { runtime.callFunction("inputPressed_"+runtime.layout.name, localVars.keyCode) }
    catch(e) {}
    
  • Speaking of controls, in our game I collect inputs from different devices (keyboard, mouse, gamepad, touch), and have a bunch of functions which mimic Keyboard/Gamepad conditions and return 0/1 value:

    One benefit of this approach is that these functions can be used in complex expressions, for example:

    Functions.OnKeyPressed("Confirm") | (Functions.OnKeyPressed("Use") & Player.isFlying=0)

  • Yeah, that sounds pretty similar to what I have.

    Using the layout name in the function call sadly won't help me because I also have a Pause menu that appears in most layouts. Some layouts have both the pause menu and another menu.

    I might swap out my own equivalent to your OnKeyPressed functions from callbacks to functions for consistency. I felt that might take a small performance hit, but it could be worth it.

  • Using the layout name in the function call sadly won't help me because I also have a Pause menu that appears in most layouts. Some layouts have both the pause menu and another menu.

    I think you can still use that system. Say, if the game is paused, try to call function inputPressed_layoutname_PauseMenu

    If it doesn't exist (catch branch in the script) - call inputPressed_Common_PauseMenu

  • I just tried using the new instance signals to get around signals not directly receiving parameters and it looks like you can set up what is effectively and Event-based observer pattern.

    At the end of your function create a new instance with the "Create Object" action, set any instance variables you want to be available in the "On Signal" triggers and then have the newly created instance fire the signal.

    The instance will then be available in the trigger with all the instance variables that where set.

    After firing the instance signal destroy the instance to save memory.

    It's pretty much the same as creating an event and then have a dispatcher fire it in traditional programming languages.

  • DiegoM - Hmm. It's not as elegant as I might like, but I suppose that could work.

    Perhaps some day I'll learn the addon SDK and develop my own plugin for this.

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