0 Favourites

How do C2 User-defined functions work efficiently?

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

    <html>
       <head>
           <title>
               Javascript Function Sample
           </title>
           <script>
             function MyFunction()
              {
               window.alert("Function");
              }
           </script>
       </head>
    
       <body>
       
         <script>
         MyFunction();
         </script>    
     
       </body>
     
    </html>
    [/code:a7jvc6xm]
    
    C++ code:
    
    [code:a7jvc6xm]
    #include <stdio.h>
    #include <conio.h>
    
    void MyFunction()
            {
    
            printf("Function Test"); 
    
            }
    
    int main(void)
            
           {
     
            MyFunction();
    
           getch();
           return 0;
          }
    
    [/code:a7jvc6xm]
    
    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? 
    
    [img="http://i.imgur.com/pYg3rJg.png"]
    
    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.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • anywhere. if you coded in c# ever you'd know this. (or java)

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

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

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