How do C2 User-defined functions work efficiently?

Discussion and feedback on Construct 2

Post » Thu Jul 14, 2016 4:22 pm

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:

Code: Select all
<html>
   <head>
       <title>
           Javascript Function Sample
       </title>
       <script>
         function MyFunction()
          {
           window.alert("Function");
          }
       </script>
   </head>

   <body>
   
     <script>
     MyFunction();
     </script>   
 
   </body>
 
</html>


C++ code:

Code: Select all
#include <stdio.h>
#include <conio.h>

void MyFunction()
        {

        printf("Function Test");

        }

int main(void)
       
       {
 
        MyFunction();

       getch();
       return 0;
      }



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?

Image

Summary: So should C2 functions be always on top?
Thanks for your time.
The Things you can create is only limited by your imagination. If you don't have the skills then use your motivation as a natural force to exceed all expectations. Chadori RebornXD
B
67
S
22
G
90
Posts: 1,125
Reputation: 60,036

Post » Thu Jul 14, 2016 6:16 pm

Functions dont run 'top down'. See the 'call' as a collapsed code of the 'on'. Function just run as they where 'in place'.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Thu Jul 14, 2016 7:04 pm

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.
B
94
S
33
G
118
Posts: 5,396
Reputation: 75,853

Post » Thu Jul 14, 2016 7:06 pm

anywhere. if you coded in c# ever you'd know this. (or java)
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
43
S
14
G
12
Posts: 626
Reputation: 9,455

Post » Thu Jul 14, 2016 10:57 pm

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.
Scirra Founder
B
403
S
238
G
89
Posts: 24,654
Reputation: 196,155

Post » Fri Jul 15, 2016 9:52 am

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.
The Things you can create is only limited by your imagination. If you don't have the skills then use your motivation as a natural force to exceed all expectations. Chadori RebornXD
B
67
S
22
G
90
Posts: 1,125
Reputation: 60,036

Post » Fri Jul 15, 2016 10:54 am

because background is javascript and your event sheet is represented in javascript, then calling a function actually works calling that function by memory location through default javascript handlers.

more details:
http://www.w3schools.com/js/js_functions.asp
Sea Monsters template - Isometric
Also includes 40 pages PDF of optimizations and "how-to" for your games, and how the "sea monsters" template was built. Follow link for details :)

sea-monsters-templates-and-assets_t162705
B
43
S
14
G
12
Posts: 626
Reputation: 9,455


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 10 guests