Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

When to use local variables


Question

Posted

When should I use local variables and what exactly is the difference in terms of usage and efficiency?

5 answers to this question

Recommended Posts

  • 0
Posted

If you use a local variable, it will be contained to the specific function or subfunction you define it in. I.e. If you run an if or a while condition and define a local variable inside that, the variable will be confined to that condition. This means that it is released from memory straight after the condition ends and it can't be used outside this condition. This is a good thing if you have many many scenes or a system with very restricted RAM (remember, Lua was written for low footprint applications, so it is often run on very low memory platforms). But RAM shouldn't be a huge issue on the HC2, so the efficiency gain in containing the variables by declaring them locals is probably negligeble and often the use of local can be confusing because you might end up declaring something a local variable even though you need to use it outside the specific body it is declared in.

I just declare the variables without the local. Then, they will be contained to the scene, but will stay in memory until the scene dies. If I have a persistent loop though, I will sometimes declare it local to reduce persistent memory consumption.

  • 0
  • Inquirer
  • Posted

    Thanks.

    So why is this not working?

    Please login or register to see this code.

    only works if not local

    • 0
    Posted

    In your example the value is only known in the if statement and not outside the if statement.

    • 0
  • Inquirer
  • Posted

    Wow that's local. Well I changed all my locals back to normal variables. This is just too dangerous for the few kBytes of RAM.

    • 0
    Posted

    Yup... What he said.

    As I wrote, it is contained to the condition you set it in. It is also a bit misleading as in other languages, it is required to define a variable as local or it will persist in memory until the scripting engine is shut down (which might actually be what is happening here, each scene is a separate virtual lua instance - ehich also explains the high load scenes put on the device).

    But just avoid declaring local and it will be "global" to the scene.

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...