Creating a subroutine in Construct2

1
  • 39 favourites

Stats

2,560 visits, 5,936 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Introduction

You will need to be atleast familiar with the Construct2 event sheet in order to follow this tutorial.

For those new to programming

A subroutine is quite simply re-usable code. eg. if you wanted to create baddies in random places at random times during a game you could write a subroutine that creates a baddy and then call up that routine every time you need to make one, a bit like phoning the mother inlaw and asking her round for tea.

Reason for this Tutorial

I am an experienced programmer new to Construct, very soon I wanted to make a subroutine but I must confess to being baffled as to how, I could find no obvious way, looking through the help and tutorials I could find nothing, although it may be in a tutorial I never opened. Anyway if you are as baffled as I was here is a neat way of making a subroutine.

Getting started

What we shall be doing

We shall create two buttons that will set the text in a textbox, in order to keep things as simple as possible they will both do exactly the same thing. We will also create a third button to reset the text.

Setup

Create a new project, go to Layout1 and create three buttons and a text box, also add a mouse. If you don't know how to do this you need to do the beginners guide at

Beginners Guide to Construct2

Click on the first Button and in the properties on the left change it's text to "Button1"

Now change Button2 text to "Button2" and Button3 text to "Reset" then change Button3's Name property to "BtnReset". Select the Textbox and clear the Text property (it probably has "Text" in it).

The Event Sheet

Go to the event sheet(Event Sheet 1) on the top tabs.

Click on the blue 'Add event' in the top left corner,

in the window Double click the mouse,

in the next window double click 'On Object Clicked',

in the window that pops up leave the top two boxes as they are and click on the button '<click to choose>',

double click 'Button' then click on 'Done'.

We now have a mouse event, we will not enter any action for it just yet. Repeat this procedure for Button2 and the Reset button.

Making our Subroutine

In the event sheet move the mouse below the last mouse event and away from the blue 'Add event' then right click the mouse,

choose 'Add group' In the window rename the group to "Subroutine" without the inverted commas, click 'OK'.

Our subroutine appears, right click somewhere in the subroutine box and select 'Add sub-event',

double click 'System',

double click 'Is group active'(under General) then type in the name of your group between the commas, in our case it is "Subroutine", it is important that you type it exactly the same as the name on the group and include the inverted commas.

Now we can add an action for our subroutine to perform,

click on the blue 'Add action' to the right of the System event, double click on 'TextBox',

in the new window, scroll down to the 'Text box' heading and double click on 'Set text' at the very bottom.

Now type in the message you wish to display when the button is pressed, I am going to put "Rubbish" myself but you can put whatever you like.

Click 'Done'

the action appears.

Click on the 'Add action' below the new action,

in the window double click 'System' then double click 'Set group active'

once again type in the group name ie Subroutine in between the commas then click on the little box on the right of the 'state' box (it has a 'v' label) select deactivated,

click on done.

We just need to do one more thing to our subroutine,

just to the left of the Subroutine title you will see a minus sign, click on it and our actions will disappear.

Now move into the subroutine box and right click, choose Edit,

untick the box on the left of 'Active on start' and click 'OK'.

Now our subroutine is greyed out.

Calling the Subroutine

In the 'Button' mouse click event at the top, add an action,

double click 'System'

then 'Set group active'

Type the name of the subroutine between the provided commas, leave it as Activated and press 'Done'

Thats it, we have called our subroutine. Now we just need to repeat the procedure for 'Button2'.

For the 'Reset' button add an action,

double click 'TextBox',

scroll down to 'Set text' double click

then click 'Done' without changing anything else.

Run the programme

Click on Button1 and your message should appear,

click on reset to delete it

now press Button2 and your message should appear again.

We have used the same code twice.

Conclusion

If you are new to programming you are probably thinking at this point, Why bother? afterall we could of just set each button to display the message directly.

I don't know if this is the way you are meant to make subroutines in Construct2, there may well be a better way but in a recent project I managed to condense over 300 lines of code down to just 12 using this technique.

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!