I have been wondering about how functions work efficiently and I didn't see in the manual any explanations so I came here. In normal programming, User-defined functions are always placed above the in the code sheet, this is because it works efficiently that way since the cpu reads codes from top to down. And scattered functions can actually cause non-noticable delays.
For example: (Both of them always have their functions above)
An HTML code:
I sometimes think that if we put functions in the middle of the eventsheet especially with a big project containing about 4000 events. It will make an unnoticeable delay but if pushed through, it might just make an unexpected delay. My point is that event sheets are read by the cpu from topwards to downwards. That means if
a function is right above the call then it will travel 150% the normal distance, so basically it read longer by 50% and that is 1.5 * tick cycle travel. (See the below image) Is my observation right or does Construct 2 handle functions differently?
Summary: So should C2 functions be always on top?
Thanks for your time.
Functions dont run 'top down'. See the 'call' as a collapsed code of the 'on'. Function just run as they where 'in place'.
They can be anywhere, it doesn't affect performance at all. In other programming language the same applies, but generally the function is defined before it is used. In some languages it doesn't have to though, the compiler can find the function fine even if it's put below the place it's called.
anywhere. if you coded in c# ever you'd know this. (or java)
Develop games in your browser. Powerful, performant & highly capable.
The "events are read top-to-bottom" bit applies to normal (non-trigger) events. Triggers run outside of that top-to-bottom flow, and the position of the function has no effect. C2's functions are actually designed to be as close as possible to normal programming functions, but in events.
In fact, the "On function" trigger is so special and performance-sensitive, it has special engine code to ensure it runs as efficiently as possible.
Thanks guys, I get your point now. But how does it even know where the function is if it doesn't search through the whole sheet? Yeah a noob question but I just got curious again.