Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

About Lua syntaxis in a VSCode
Started by stalxerhd Sep 18 2019 10:15 PM

- - - - -
2 replies to this topic

Best Answer XeduR @Spyric , 19 September 2019 - 12:50 AM

1) Creating several local variables at a time goes exactly like you originally posted, but if you want to assign a specific value to them as well, then you'd do the following:

-- Declare local variables without assigning values to them
local a, b, c, d

-- Declare local variables and assign values to them
local a, b, c, d = 1, 2, 3, 4

Lua is pretty smart when it comes to understanding your code and it doesn't necessarily require explicit symbols to signal that a line of code is ending like many languages do.

-- For Lua, this line is the same as writing it in four lines
local a=0 local b=0 local c=0 local d=0

-- Like this
local a=0
local b=0
local c=0
local d=0

Finally, these two lines crash simply because Lua isn't expecting there to be any commas there.

local a=0, b=0, c=0, d=0 
local a=0, local b=0, local c=0, local d=0 

2) For scope, you could check out: https://docs.coronalabs.com/tutorial/basics/scope/index.html

 

In your first sample code, a and b are local both inside and outside of the function. If you were to create new local variables a and b inside of the function, then those new a and b would overwrite the previous ones inside of the function.

 

Your question #2 wasn't as coherent as #1, so I'm hoping this'll answer your question.

local function test( a, b )
    local c, d = 99, 100 -- c and d are only local to the function, so they won't be accessible outside
    print( c, d ) -- 99, 100

    print( a, b ) -- 1, 2 -- these were passed to the function as arguments
    return a+1, b+1 -- return the arguments after changing them
end

print( a, b ) -- nil, nil (because we haven't declared anything

local a, b = test( 1, 2 )

print( a, b ) -- 2, 3
print( c, d ) -- nil, nil -- c and d exist as local variables inside of the function, so they aren't accessible here

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

stalxerhd

[GLOBAL: userInfoPane.html]
stalxerhd
  • Observer

  • 11 posts
  • Corona SDK

I now trying to migrate from LDT to VScode and see some tips from the new editor about lua syntaxis (I use Lua Diagnostics extension). I have not seen such tips before, so that's why I'm here.

 

The question may seem insignificant, but for me, it is important to understand this part of the syntax in my examples. I also want to check how correct the editor is.

 

1)

local a, b, c, d       -- All variables are local
local a=0 b=0 c=0 d=0      -- Editor says: "Global variable b,c,d in lowercase initial."

But if write:

local a=0 local b=0 local c=0 local d=0    -- All variables go local too

So there is no way to initialize variables with values (I use much of pre-set variables) without posting "local" every time before them? I mean one "local" for line...

 

Also syntax like:

local a=0, b=0, c=0, d=0 
local a=0, local b=0, local c=0, local d=0 

Showing an issues: DIAG_OVER_MAX_VALUES and the simulator gives me "unexpected symbol near "=" So it looks like a complete wrong declaration.

 

2) 

local a,b    -- Editor says: "Unused local a and b"
local function test (a,b)
 --function content
end

It looks like variable's a,b, that declared in a "local function test", are also local.  But if I will not declare it for "return":

local function test ()
--function content
return a,b  -- Editor says: "Global variable b,c,d in lowercase initial."
end

The a and b will be global.

 

So if I understand correctly, only variables that declared in () go local?



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 944 posts
  • Corona SDK

  Best Answer

1) Creating several local variables at a time goes exactly like you originally posted, but if you want to assign a specific value to them as well, then you'd do the following:

-- Declare local variables without assigning values to them
local a, b, c, d

-- Declare local variables and assign values to them
local a, b, c, d = 1, 2, 3, 4

Lua is pretty smart when it comes to understanding your code and it doesn't necessarily require explicit symbols to signal that a line of code is ending like many languages do.

-- For Lua, this line is the same as writing it in four lines
local a=0 local b=0 local c=0 local d=0

-- Like this
local a=0
local b=0
local c=0
local d=0

Finally, these two lines crash simply because Lua isn't expecting there to be any commas there.

local a=0, b=0, c=0, d=0 
local a=0, local b=0, local c=0, local d=0 

2) For scope, you could check out: https://docs.coronalabs.com/tutorial/basics/scope/index.html

 

In your first sample code, a and b are local both inside and outside of the function. If you were to create new local variables a and b inside of the function, then those new a and b would overwrite the previous ones inside of the function.

 

Your question #2 wasn't as coherent as #1, so I'm hoping this'll answer your question.

local function test( a, b )
    local c, d = 99, 100 -- c and d are only local to the function, so they won't be accessible outside
    print( c, d ) -- 99, 100

    print( a, b ) -- 1, 2 -- these were passed to the function as arguments
    return a+1, b+1 -- return the arguments after changing them
end

print( a, b ) -- nil, nil (because we haven't declared anything

local a, b = test( 1, 2 )

print( a, b ) -- 2, 3
print( c, d ) -- nil, nil -- c and d exist as local variables inside of the function, so they aren't accessible here

  • stalxerhd likes this

[TOPIC: post.html]
#3

stalxerhd

[GLOBAL: userInfoPane.html]
stalxerhd
  • Observer

  • 11 posts
  • Corona SDK

Thank you very much for responding in detail to explain everything.




[topic_controls]
[/topic_controls]