Testing if a function exists ?

Discussion and feedback on Construct 2

Post » Thu Jan 15, 2015 11:00 am

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 ?
Image
Game Producer & Independent Developer - http://raphaelgervaise.com
B
24
S
9
Posts: 237
Reputation: 2,232

Post » Thu Jan 15, 2015 12:03 pm

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

Post » Thu Jan 15, 2015 12:51 pm

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 :D
Image
Game Producer & Independent Developer - http://raphaelgervaise.com
B
24
S
9
Posts: 237
Reputation: 2,232

Post » Thu Jan 15, 2015 3:56 pm

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.
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,028

Post » Thu Jan 15, 2015 4:16 pm

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 !
Image
Game Producer & Independent Developer - http://raphaelgervaise.com
B
24
S
9
Posts: 237
Reputation: 2,232

Post » Thu Jan 15, 2015 9:46 pm

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

Post » Fri Jan 16, 2015 6:17 am

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.
B
31
S
7
G
8
Posts: 232
Reputation: 6,274

Post » Fri Jan 16, 2015 10:05 am

"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).
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Fri Jan 16, 2015 12:06 pm

@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.
Image
Game Producer & Independent Developer - http://raphaelgervaise.com
B
24
S
9
Posts: 237
Reputation: 2,232

Post » Fri Jan 16, 2015 12:23 pm

Refeuh wrote: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 :D

very easy to use and very powerful!
B
21
S
9
Posts: 298
Reputation: 2,967

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 8 guests