Scripts optimisation

So, if I need to set a variable in a update or in a ongui function, is it better to type for example

x=y

or it’s better

if (x != y)
    x=y;

?

The former would likely be more efficient. It’s really difficult to say, since this is one of those cases where checking and assigning take roughly the same amount of memory, but it’s a much better habit to simply avoid putting things like this in your update methods, as the numbers add up, over time. Try to find other ways of detection (interaction through triggers, collisions, or direct-activation via another class works pretty nicely).

Overall, though, for a one line assigning, the former of your two options is more efficient.

using #pragma strict will generally help code be a lot more efficient as it forces you to not use much in terms of references or searching for variables with things like the .Find function.

For your specific question, evaluating is going to use more processing power and memory than just doing the assignment. The best answer is to think of when the variable is going to be changed and only actually update it when necessary. The update function isn’t really needed all that often unless something is visible to the player in my experience.