Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

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

- - - - -
4 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
  • Enthusiast

  • 43 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
  • Corona Geek

  • 1,178 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 and plicatibu like this

[TOPIC: post.html]
#3

stalxerhd

[GLOBAL: userInfoPane.html]
stalxerhd
  • Enthusiast

  • 43 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

stalxerhd

[GLOBAL: userInfoPane.html]
stalxerhd
  • Enthusiast

  • 43 posts
  • Corona SDK

I have a couple of questions that have refer lua syntax, will ask them here.
 
1) Now I divide code on to modules and looked at a few examples (Corona Cannon, Match Three Space RPG, Crush, and I have a question: why in some cases programmers write:
local M = {}
return M

and in others:

local _M =
return _M

What is the point of putting leading underscore when designating a table or variable?

 
2) I read Composer beginner's guide and learned this:
local sceneGroup = self.view

backGroup = display.newGroup ()
sceneGroup: insert (backGroup) 

If I understand correctly, self.view uses only for displaying something? So, any object can be added with 

sceneGroup: insert (object) 

But in Corona Cannon, game.lua file, I found such code:

local group = self.view
self.levelId = event.params
self.level = require ('levels.' .. self.levelId)
self.map = tiled.newTiledMap ({g = group, filename = 'maps.' .. self.level.map})
self.bugs = {}
self.map.physicsGroup

What is the point of adding ‘self’ everywhere? Instead of 'group'?



[TOPIC: post.html]
#5

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,178 posts
  • Corona SDK

1) Well, variables with underscore in front of them in Lua are often used with global variables, e.g. _G. In this case with the module, I might be wrong, but I think it is just a naming convention from some other programming language where underscore at the beginning just means that it's a value that will be returned and that nothing fancy will done with it. So, like in that module, _M is just the name of the local table and it is returned at the end of the function. It's a different style, but it is no different from just having M without the underscore.

 

2) What the first bit of code does is that it creates a reference to self.view, which the composer uses to handle the display objects that you assign to it. Inserting other display objects or display groups into self.view, or its reference, sceneGroup, you are telling Composer to handle those display objects.

 

I haven't played around with Corona Cannon, so I don't know its peculiarities, but essentially local group = self.view is the same thing as local sceneGroup = self.view, it's just using a different name for the reference. Naming conventions vary from programmer to programmer, so differences are bound to happen.

 

self itself is an argument to a method/function, e.g. scene:create(). A method is just a function that passes the table it is added to as the first argument every time it is called, so when scene:create() is called, self is always passed and in this case self is scene.

 

Whoever made that sample project also seems to add parameters to the scene table itself. When they write self.levelId = event.params, they are just adding that piece of information to the scene table itself. They could have just as well created a new local variable for it. So, the programmer created a new local variable, i.e. local group = self.view, that the composer uses to handle display objects/groups, but everything else seems to be about adding simple parameters to the scene table.


  • stalxerhd likes this


[topic_controls]
[/topic_controls]