Testing if a function exists ?

  • Greetings !

    I'm wondering if there's a way to check that a function exists, before calling it Basically I'm after the C2-equivalent for the JavaScript "if (typeof yourFunctionName == 'function')"

    I'm creating a set of generic event sheets to handle the logic of all my levels, and I have a callback-like mechanism as well as custom-triggers using dynamically generated function names, e.g. "Call LayoutName & "_EntitiesDestroyed"(), etc.

    Some levels will want to do something on "xxx_EntitiesDestroyed", and some won't, depending on the gameplay of that specific layout. This solution lets me implement bits of logic only for when it matters ; but has the disadvantage of generating some invalid function calls on level that don't need this trigger.

    This a literally no impact on the game logic or performance, but doesn't look very neat in the JavaScript console.

    Thoughts ?

  • AFAIK, no. Could you have some do-nothing functions, hidden away somewhere out of sight, or will there be too many of them?

    Or maybe you could have some type of registration mechanism where a level registers it's need for certain functions (and supplies its own callback function), a bit like publisher-subscriber pattern.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for the input ! I like the idea of registering callbacks, it's a good alternative to not being able to check if functions exist, I'll go with that

  • Yes. Call backs are marvelous. However I want to add just a little bit of tech to this question.

    Reflection is the term used in programming languages that allow the ability to examine the program iteself. Reflection allows for seeing if function/methods exist, allows for checking if variabls, objects and classes exist. However Reflection mostly works on a scripting level, and is one of the technology slow points.

    Older languages such as C/C++ don't support Reflection at all. Also newer compiled langauges may support Relfection, but if you want the faster version of the compiled language then Reflection should turned off. Such as C# offers Reflection, but if you wan the code to be faster don't use it, and then tag the class to have Reflection off.

    So just as a general bit of practice avoid Reflection based programming, unless you have no choice.. such as javascript.

  • Indeed -

    I personally find reflection to be most useful for tools development, where runtime performance is usually less of an issue ; being able to expose engine modules and game components for abstract tools to interface with is always very practical.

    Nowadays, that's pretty much the direction everybody is going : Entity/Systems component-based game framework, remote tools with reflection (networked if possible, to connect to consoles and mobiles), unified cross-platform mid-layer engine, and platform-specific low-level modules for performance and unique features.

    Stepping back a bit, I'm mostly after a way to make something reasonably reusable and flexible ; I'm no web or JavaScript expert, but I'm fairly hopeful I'll have enough optimisation to do at game-logic level before I start to hit any actual language bottleneck !

  • Going from C++ to Java I could never find myself using reflection/introspection quite as freely as the pure Java guys either. Partly for performance reasons but partly because I think good design removes the need for it in general coding. (And in C++ you just don't have the choice). Of course there are some amazing frameworks, etc. that use it in Java to great effect. But agree with , it can be a bit of a crutch. Refeuh I don't think he's suggesting you can get into any trouble like that in C2, although arguably it does have some introspectіon features.. Anyway that callback technique I've used in generic UI code in C2 and it works fine.

  • A DoesFunctionExist method wouldn't be too difficult to add. Given that functions can't be created/destroyed/modified during run-time, Construct would simply compile a hash list of the function names as part of the build process, and then a DoesFunctionExist event would be a matter of a single dictionary lookup. Negligible as far as performance loss is concerned.

    In terms of these sorts of "reflection"-esque types of methods, I'm still waiting for ObjectInstance.Name which would return the asset library name of any instance at run-time.

  • "A DoesFunctionExist method wouldn't be too difficult to add. Given that functions can't be created/destroyed/modified during run-time"

    cacotigon IIRC, they can, if a function is not included into one of the used event sheet, it cannot be used, and since an include can be conditionnal, it may be a problem (maybe not, I can be totally wrong on that).

  • Aphrodite

    Interesting question -

    While it is possible to include even sheets in conditional statements, I would be tempted to think that this is actually resolved at export time, and that functions are either defined or not, but never generated or attached dynamically. I might be totally wrong as well, 'just a guess

    Btw I find it amusing that we have that many users well-versed in the art of programming C2 claims "no programming required" (which I personally find a bit misleading, the logic of event sheets is similar to light/gameplay-programming), yet it attracts people with a technical background ! Mostly for the productivity when creating small applications or testing prototypes, I guess.

  • Btw I find it amusing that we have that many users well-versed in the art of programming C2 claims "no programming required" (which I personally find a bit misleading, the logic of event sheets is similar to light/gameplay-programming), yet it attracts people with a technical background ! Mostly for the productivity when creating small applications or testing prototypes, I guess.

    yes, you are right, construct2 it's a powerfull tool and you can do the most of thing just with your logic...

    if you programming, construct2 it's like a piece of cake, I remember when I tried the first time, I just learn in 2 days how to do the most of thing, (I read the manual just for the expression or something else) but because I was using action script and a little bit of lua .... I remember, with Lua for do the same thing here you have to waste a lot of time, really... with construct2, in 1 hour you can make a easy gameplay and see how works well

    very easy to use and very powerful!

  • Aphrodite

    It is true that functions cannot be referenced from an event sheet that does not include the relevant sheet containing the function itself. However, event sheets cannot be included conditionally.

    You will notice that when you include an event sheet, the INCLUDE statement is always placed at the top of the event sheet.

    A conditional include would look something like this:

    IF X == TRUE THEN INCLUDE EVENTSHEET1

    ELSE INCLUDE EVENTSHEET2

    All event sheets (which are represented as XML) are converted to Javascript and bundled in the final exported code during the build process. When a project is previewed/exported, Construct knows exactly what functions are available at a per-event sheet level.

    Refeuh

    You're right about that. As a software engineer by trade, Construct is a way for me to wind-down at the end of the day. It's definitely my favorite RAD tool for games.

  • Aphrodite

    It is true that functions cannot be referenced from an event sheet that does not include the relevant sheet containing the function itself. However, event sheets cannot be included conditionally.

    You will notice that when you include an event sheet, the INCLUDE statement is always placed at the top of the event sheet.

    A conditional include would look something like this:

    IF X == TRUE THEN INCLUDE EVENTSHEET1

    ELSE INCLUDE EVENTSHEET2

    All event sheets (which are represented as XML) are converted to Javascript and bundled in the final exported code during the build process. When a project is previewed/exported, Construct knows exactly what functions are available at a per-event sheet level.

    Refeuh

    You're right about that. As a software engineer by trade, Construct is a way for me to wind-down at the end of the day. It's definitely my favorite RAD tool for games.

    an include can be a subevent, thus conditionnal, C2 may place the include at the top, but you can move it freely.

  • > Aphrodite

    >

    > It is true that functions cannot be referenced from an event sheet that does not include the relevant sheet containing the function itself. However, event sheets cannot be included conditionally.

    >

    > You will notice that when you include an event sheet, the INCLUDE statement is always placed at the top of the event sheet.

    >

    > A conditional include would look something like this:

    > IF X == TRUE THEN INCLUDE EVENTSHEET1

    > ELSE INCLUDE EVENTSHEET2

    >

    > All event sheets (which are represented as XML) are converted to Javascript and bundled in the final exported code during the build process. When a project is previewed/exported, Construct knows exactly what functions are available at a per-event sheet level.

    >

    > Refeuh

    >

    > You're right about that. As a software engineer by trade, Construct is a way for me to wind-down at the end of the day. It's definitely my favorite RAD tool for games.

    >

    an include can be a subevent, thus conditionnal, C2 may place the include at the top, but you can move it freely.

    Aphrodite It doesn't matter. The include statements are not processed at a run-time conditional level. All event sheets are bundled into the final JS code of a project irrespective of whether they are explicitly included. So you can move the INCLUDE blocks around wherever you want, but it makes no difference.

    Here's a capx demonstrating it and the associated code.

    Capx

    https://dl.dropboxusercontent.com/u/12667027/IncludeTest/includetest.capx

  • Btw I find it amusing that we have that many users well-versed in the art of programming C2 claims "no programming required" (which I personally find a bit misleading, the logic of event sheets is similar to light/gameplay-programming), yet it attracts people with a technical background ! Mostly for the productivity when creating small applications or testing prototypes, I guess.

    Yes somehow after looking at many game dev tools, I knew immediately that C2 was for me. Don't ask me how, it was kind of weird. I also find a knowledge of concurrent programming concepts, not to mention traditional coding in general, helps a lot (despite having to unlearn a lot of stuff). I think those without prior coding experience perhaps miss out on some aspects, but it's not really missing out because each user finds what they need I guess.

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