How do I Encapsulate code for reuse

Get help using Construct 2

Post » Fri Nov 21, 2014 1:22 pm

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?
B
5
S
1
Posts: 11
Reputation: 422

Post » Fri Nov 21, 2014 1:28 pm

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.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Fri Nov 21, 2014 4:28 pm

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.
B
5
S
1
Posts: 11
Reputation: 422

Post » Fri Nov 21, 2014 10:24 pm

function.call("myfunction")
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Sat Nov 22, 2014 3:48 pm

But on the function conditions there is only the "Compare Parameter" and the "On Function".

Are you using another plugin to do this?
B
5
S
1
Posts: 11
Reputation: 422

Post » Sat Nov 22, 2014 4:58 pm

@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.
B
19
S
2
G
1
Posts: 24
Reputation: 1,475

Post » Sat Nov 22, 2014 5:36 pm

@moonworX that's about how I'd approach it, and I see @luizfbicalho already has a license so that should be okay.
Image
B
9
S
3
G
1
Posts: 346
Reputation: 1,560

Post » Sun Nov 23, 2014 1:10 am

moonworX wrote:
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.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests