Create a Function in the IDE?

Get help using Construct 2

Post » Mon Apr 18, 2016 2:25 am

Hi All,

Newbie question. How do I create a function in the IDE? There doesn't seem to be an option 'Add Function' that I can see?

https://www.scirra.com/manual/149/function

Thanks, John
B
17
S
5
G
2
Posts: 14
Reputation: 2,042

Post » Mon Apr 18, 2016 2:31 am

You have to add the Function plugin first, to the project.
ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198

Post » Mon Apr 18, 2016 4:03 am

Hi blackhornet,

Thanks for the reply.

OK.. I've right clicked on the layout, selected Insert New Object, selected 'Function' and a new Object type appears in the Project list called 'Function'.

How do I add the code to the Function?

I would expect that I could right click on the Event sheet and select 'Add function' and type it in, but this isn't an option.

Regards, John
B
17
S
5
G
2
Posts: 14
Reputation: 2,042

Post » Mon Apr 18, 2016 5:30 am

Add Event, Function, "On function".
ImageImageImage
B
71
S
22
G
258
Posts: 3,791
Reputation: 143,198

Post » Tue Apr 19, 2016 3:05 am

OK thanks. I'm not sure that adding an event makes sense in the context of a function. Is there any documentation on this that you know of? There doesn't seem to be any information in the manual on basics like this, or even on defining parameters for functions.

Regards, John
B
17
S
5
G
2
Posts: 14
Reputation: 2,042

Post » Tue Apr 19, 2016 7:01 am

johnwalker wrote:OK thanks. I'm not sure that adding an event makes sense in the context of a function. Is there any documentation on this that you know of? There doesn't seem to be any information in the manual on basics like this, or even on defining parameters for functions.

Regards, John


I would say there is quite a bit of information in the manual.

In the context of the Event system methodology, the way it works is entirely in keeping with the design philosophy.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
137
S
31
G
87
Posts: 5,554
Reputation: 60,452

Post » Tue Apr 19, 2016 9:19 am

New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
296
S
119
G
96
Posts: 7,329
Reputation: 71,300

Post » Mon Apr 25, 2016 1:50 am

I'm a newb on Construct 2 so this maybe inaccurate (more experienced Construct 2-ers feel free to correct). But for anyone else struggling with Construct 2's idea of a function:

- The manual doesn't mention that you must first include a function 'object' (thanks blackhornet) before you can use them (even though it creates that object as some kind of global type... you'll have to let that go). To use Functions you need to include an object on the Layout view, right click Insert New Object - Function.
- Once the 'object' is added you can must then add it as an Event. On the Event Sheet, click Add event, select Function - On Function to define the Function name block. This can only define the Function name it's not possible to declare the parameters. This is defining the Function (As an Event, which it isn't).
- To include parameters you have to reference them in the Function block code as Function.Param(0) etc. (It appears the parameters are being passed in some sort of JS array and Scirra haven't bothered to transpose them for us.)

IMO
- The IDE using 'Objects' and 'Object Types' interchangeably is confusing.
- Functions do not follow the idea of an 'Event Sheet'. Since the definition of a Function is not an Event, it should be in it's own Sheet.
- It should be possible to assign Functions to objects.
- It should be possible to define the parameters with a variable name at design time.
- 'On Function' is confusing, 'Define Function' or even 'Function' followed by it's name would be more clear as to it's purpose.

I get that Construct 2 is only $150 so these things probably wouldn't fit in the development budget. But boy the time I've wasted working this out! Hopefully this post will save some other people the pain.

Regards, John
B
17
S
5
G
2
Posts: 14
Reputation: 2,042

Post » Mon Apr 25, 2016 2:12 am

I understand it is always a bit hard when you start with new toy (Construct 2). But once you get familiar with what is going on then you gonna love it.

Functions in C2 are really very close to the ones in the "normal" programming. By adding the Function object to the project you simply informing C2 that you gonna use functions in general. It's not a particular function, but just a "schema", consider it like a class maybe or rather "include lib/function". I bet you get the point.

So once you've informed C2 about functions you can create your own functions. Name them as you wish, you can call them whenever you want, send numeric and string params. As String params you can send data structures like Dictionary/arrays as JSON. You can even send "callbacks" in params. Functions also return a value if you need. So it is really very close to the normal function. The only one messy thing is that all functions are global, so you have to deal with this one issue. Oh and return value event doesn't stop the function. Apart of that functions are the essence here as in regular programming.
ImageImageImage
B
31
S
19
G
82
Posts: 1,039
Reputation: 46,207

Post » Sat Sep 09, 2017 9:25 am

what had me confused in the beginning was that you need to put quotes ("") around the name of the function you are making
B
9
S
3
G
1
Posts: 1
Reputation: 1,129

Next

Return to How do I....?

Who is online

Users browsing this forum: dop2000, Scofano and 11 guests