Best way and place to declare var ?

0 favourites
  • 5 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • Hello all,

    Wondering how to best declare var in events, for optimisation at first. As "old fashioned dev", I know about stack and so on, but don't now how javascript and Construct reacts.

    So is it better to declare on top an event as local, to declare inside the event at startup, anywhere in the event, in a group ?

    Thanks for your thoughts !

  • It doesn't matter. This is the wrong approach for performance. Do whatever suits you best, and then optimise later based on actual performance measurements. Anything else is basically a waste of time.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I'm not familiar with how construct resolves variables, as I haven't worked directly on that code, but language design and compilers are a bit of a hobby of mine so I can probably cast some light on it. I will say the performance cost is probably negligible, it's rarely an optimisation that is worth thinking about in most languages as well laid out code tends towards the more optimal version anyway.

    Short answer: it will either have no difference, or it will be faster the closer the variable is to the current scope.

    Long answer: Event sheets are basically interpreted like a scripting language. They have some rather unique specialisations and optimisations, but for this example we can consider them the same. Resolution of variables in an interpreter typically involves walking from the current scope to the top scope while searching for a matching variable name. The closer to the current scope it is the less scopes need to be checked and the less work needs to be done. However, it's more common for an interpreter to resolve the location of a variable before beginning code execution and store it for later. So instead of searching for the scope which contains the var on each get/set it know which scope contains it, and hence can modify it straight away. In that situation there is basically no performance difference between scope levels.

  • Precious advices of you both. Thanks you!

  • Since variables appear in the prompt list you want to keep them low in the scope so you don't have to see them all the time.

    For example if you are only using a variable inside a group don't make it global.

    And advice for global variables--put them all in one dictionary otherwise you will see them everywhere listed over and over everytime you want to select a variable. Instead of just one dictionary UID in global.

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