4 years ago

Static local variables in Lua

Can it be done?

#dirtyluatricks #coding #scripting #lua #static #dirtycode


Officially no.... unofficially yes....

In case you didn't read my last article about globals and locals, let's get back on the subject of a static local variable.

Let's first demonstrate this with C

		
			#include <stdio.h>

void Count(void) {
     static int counter = 0;
     counter++;
     printf("Count #%d\n",counter);
}

int main(void) {
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();

    return 0;
}
		
	

Although counter is a local now... so the main() function cannot read it, nor can it alter it. Counter will always use the same memory address, and thus counter can keep counting it up, as the value remains even when Count() is exited.

And thus the output will be:

		
			Count #1
Count #2
Count #3
Count #4
Count #5
Count #6
Count #7
Count #8
Count #9
Count #10
		
	

Lua has no support for the code above, however there is a dirty trick to make it happen anyway. I hate that trick but it does work.

		
			do
 local counter = 0
 function Count()
     counter = counter + 1
     print(string.format("Count #%d",counter))
 end
end

Count()
Count()
Count()
Count()
Count()
Count()
Count()
Count()
Count()
Count()

		
	

The output will be the same, however the 'counter' variable can only be called within the 'do' scope.

What did actually happen. Normally any variable called within a scope will be disposed as soon as the scope is finished executing, however, since the Count() function was called as a global yet defined inside the do-scope, it has a reference inside this scope, forcing Lua to keep the scope active until that function is undefined, and since that function may need that variable, it stays active. Should you put in "Count=nil" later on, the reference inside that scope will be gone, and the scope as a whole will then disappear.

Now we're gonna make it even harder. I want my Count() function only to live inside this script, so if an other Lua script calls it with "require" the Count() function cannot be called by the script importing this with "require". Can that be done?

Yeah, it can be done. Only takes one extra line of code, but where to place it? I'll show you:

		
			local Count

do
 local counter = 0
 function Count()
     counter = counter + 1
     print(string.format("Count #%d",counter))
 end
end

Count()
Count()
Count()
Count()
Count()
Count()
Count()
Count()
Count()
Count()

		
	

Was that all? Yeah, that was all....
This is dirty code, but it works.

Now apart from Lua, I've tried if the same trick also works in JavaScript... I have not yet tried the second thing with making functions local, but when it comes to static local variables in general.... Yes!

		
			{
      let counter = 0;
 
      function Count() {
        counter++;
        console.log("Count #"+counter);
      }
}    

    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();
    Count();

    


		
	

Oh, brother.... Then again... JavaScript was made to write crappy code only, so why am I not surprised 😜😜

General Blogs: https://trickyjeroenbroks.tumblr.com/
Indie Reviews: https://jeroensindiereviews.tumblr.com/
Game Classics Reviews: https://trickygameclassics.tumblr.com/



0 comments

Loading...

Next up

Why do people wanna be a #moderator? Is it really such a #cooljob?

Game redo on Cynthia Johnson

Disney Princesses go to Hogwarts

We all hate doing it, but yet this part of a game is essential too. #coding #gamedev #error #errors

Can you see why this pyramid deal could never be solved from the start (regardless what the already removed cards are)?

Kthura, map system

BallPlay future

A few facts and fables about fire.

Globals.... Are they evil?

Killing only brings you so far!