Create a Function in the IDE?

  • 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

  • You have to add the Function plugin first, to the project.

  • 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

  • Add Event, Function, "On function".

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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

  • 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.

  • 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

  • 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.

  • what had me confused in the beginning was that you need to put quotes ("") around the name of the function you are making

  • what had me confused in the beginning was that you need to put quotes ("") around the name of the function you are making

    Yeah, but this allows you to call functions using string instead of fixed name, and do cool things:

    On Button clicked -> Function.call(Button.buttonFunctionName)

    Function.call(b=1 ? "EnableSomething" : "DisableSomething")

    Function.call("Spawn" & EnemyType)

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