Discord
Login
Community
DARK THEME

Visibility of a global variable.

I detected a global variable visibility bug.

The error occurs when it uses the given variable label twice. The first time this variable is local and will be declared as a 'for' loop variable, for example.

The microSript engine manages local variables and frees memory when no longer needed - (frees memory and name) Then, for the same name, after the 'for' loop, I pass this variable a value that is to be global, eg I assign a new object to this variable.

Now the variable should be global - however, the script engine releases the memory (I have no way to operate on a given object) which was indicated by this variable.

I am posting sample code >>> https://microstudio.dev/i/Loginus/globalvirableerror/ <<< You have to run the program in the editor and watch the console.

It looks like a mistake. I haven't come across this because I use very short names for variables in a loop, most often it's a single character.

https://microstudio.io/i/Loginus/globalvirableerror2/ <<< here code with short name ( "a") .

A for loop actually automatically allocates the iterator as a local variable at the block level before the loop ; the iterator thus remains visible outside (after) the for loop. It can be useful for example to check at which value of the iterator the loop was broken. In other words, this code:

for i=1 to 10
  print(i)
end
print(i)

translates exactly to this code:

local i = 1
for i=1 to 10
  print(i)
end
print(i)

This is why your example is not accessing the global variable after the for loop ; if you need to access a global variable with the same name as the iterator, after the loop, you have to use global.myvar = ...

I guessed it was a variable management bug.

Are you going to leave it like that?

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community