Construct 3 suggestions & ideas

Suggest and vote on ideas for Construct 3! Please note this is only one aspect of planning. We do not guarantee any features here will be implemented, even if they are top-voted ideas. The aim is just to collect feedback. Remember to search for existing submissions before adding an idea, describe your ideas as comprehensively as possible, and vote for plausible ideas that are well thought out. Please see our full guidelines on suggesting features.

32 VOTE

Functions (custom actions) as properties of Family / Object type

Suggested behaviour

Lets allow creation of functions as members of Object Types and Families. The same way these objects can have instance variables, let them also have list of functions (custom actions).

 

Using and defining the functions

Declaring function:

When adding a function, a dialog will appear to specify function name, the names and types of parameters, and a return value, possibly also descriptions for all of these, that can be shown in editor and expressions. The parameter types also allow for typechecking of calls in editor. These function wills be listed in Object type / Family properties (similarly to instance variables), and allow to quickly navigate to function body definition.

Defining a function body:

For these functions a new trigger event will appear in "Add Event" dialog, with that particular function name, similarly to how today functions are accomplished via Function plugin. These function will have its parameters named and types, and those will be available in function body (when defining actions) directly by their name. The body of a function will be defined on event sheet by the capabilities available today (events, actions), such as defining a Group or using existing Function plugin.

Using functions in expressions:

Such functions can be called in expressions with syntax Object type.Function name (Param 0, Param 1, ...) and will execute for each Object type / Family in currently picked list.

Using functions as Actions:

These functions will appear as Custom Actions of that particular Object Type / Family in the list of Actions in Add Action dialog, and will execute for each Object type / Family in currently picked list.

 

Family -> Object Type "virtual" functions

For Families, the mechanism will allow "virtual" calls - imagine as lightweight Object Oriented capability (2 levels only) - Family functions must be defined and will be called by default, individual Object types - members of the Family - can override Family function with their own version (but dont have to). In that case the Object type version will be called. Imagine usage such Enemies.GetScoreOnHit, with some default implementation for most Enemies as function of the Family, but for SuperEnemy object type some specific, more elaborate calculation will be provided.

 

Benefits:

  • Better encapsulation of logic tied with particular Family / Object type, coupling of data (instance variables), and events + actions in a logical container (Family / Object type)
  • Better orientation in source code especially in larger projects (imagine functions such as MenuBar.Hide, Enemy.GetScore, Player.ModifyHealth, instead of searching the event sheets for places where you put his logic)
  • Increases maintainability and extensibility of the code

 

Remarks
  • should be implemented with function call overhead optimization, as suggested and measured in https://www.scirra.com/forum/feature-request-first-class-functions_t189835
  • can it be achieved with existing capabilities? Yes it can. It involves creating instance variables to store function names, defining the functions via Function plugin and than calling the functions, retrieving proper function name from instance variables, always supply UID of particular object as a first parameter and picking it on the start of function. It is however plagued by poor function call performance (see bullet above), no support in the editor and no navigation, so resulting code is not easy to read and is full of those indirect "call function by name stored in a variable" things.
  • mcabak
  • Apr 5 2017
  • Attach files
  • Guest commented
    April 05, 2017 17:26

    A separate type event sheet specifically for functions might be nice. They could even get a separate window like tilemaps where the top event has to be a function. Clicking on a function call highlights the function in its editor.

  • ChadoriXD commented
    August 25, 2018 12:51

    Hey, If you guys really need it that much. I have an addon for that.

    Self Function :: C3 & C2 Behavior

    Reference :: https://www.scirra.com/store/construct2-behaviours/self-function-construct-2-3-4423

  • Guest commented
    31 May 21:36

    Demonstrating how it could work with built-in functions