How do I Encapsulate code for reuse

  • I'm creating buttons, and many of the effects and events are repeated many times.

    For example I have this condition:

    Main Condition

    Mouse->On Left Button Clicked on "Button"

    OR

    Touch-> Is Touching "Button"

    Sub Condition

    System -> Trigger once

    How Can I create in construct language something that receice a button as parameter, and check all of this conditions, to be used instead of repeating all of this?

  • Usual way is to create a function, pass it as a param the UID of a button object. In the function, pick the object from UID then do what you want.

  • This works fine for actions, but this doesn't work for conditions, I can't find a way to put a function call as a condition.

  • function.call("myfunction")

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • But on the function conditions there is only the "Compare Parameter" and the "On Function".

    Are you using another plugin to do this?

  • luizfbicalho

    You could put all of your different buttons into a family and assign an instance variable to the family, where you store the desired action.

    Then you check for mouseclicks like so:

    On left mouse button clicked on ButtonFamily -> call function CheckForButtonPress(ButtonFamily.UID)

    On Function "CheckForButtonPress"

    Pick ButtonFamily with UID function.param 0

    Compare ButtonFamily.Variable

    ButtonFamily.Variable = "menu" -> Call Menu

    ButtonFamily.Variable="quit" -> Quit

    etc.

    I hope it's somewhat understandable.

    But it will not work with the free version of Construct 2, because of families.

  • that's about how I'd approach it, and I see luizfbicalho already has a license so that should be okay.

  • Compare ButtonFamily.Variable

    ButtonFamily.Variable = "menu" -> Call Menu

    ButtonFamily.Variable="quit" -> Quit

    etc.

    ok I misunderstood the OP. Callbacks is a good solution but you can just do Function: Call ButtonFamily.Variable() as a shortcut.

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