Difference between : and . ?

Hello,

I am pretty new to lua, may i know what is the difference between : and . and how am i suppose to use them and create them?

Thanks [import]uid: 74723 topic_id: 12623 reply_id: 312623[/import]

Dot vs Colon

What does the colon operator do? It is actually a shortcut. In most languages, an object method call is just like a plain function call except there’s a hidden first argument to the function that is the object itself. This hidden argument is known as self in Lua. Therefore, the colon operator is merely a shortcut to save you from extra typing. You could call an object method using the dot operator if you pass the object as the first argument:

Object call with dot
object.translate( object, 10, 10 )

Object call with colon
object:translate( 10, 10 ) [import]uid: 71210 topic_id: 12623 reply_id: 46187[/import]

:slight_smile: [import]uid: 71210 topic_id: 12623 reply_id: 46188[/import]

Thanks for the reply!
I have a new question.

I am confuse with how global variable works.
I was testing the below example:

  • I have 3 .lua files, main.lua, test1.lua, test2.lua

[lua]-- main.lua
local test1 = require( “test1.lua” )
local test2 = require( “test2.lua” )

testglobal = false

local function main()
test1.Run()
test2.Run()
end

main()[/lua]

[lua]-- test1.lua
module(…, package.seeall)

function Run()
testglobal = true
print(testglobal) – Output true
end[/lua]

[lua]-- test2.lua
module(…, package.seeall)

local function test( event )
print(testglobal) – Output false
end

function Run()
timer.performWithDelay(1000, test, 0)
end[/lua]

Shouldn’t editing a global update the global variable itself? [import]uid: 74723 topic_id: 12623 reply_id: 46193[/import]

when you give testglobal = true in test1.lua its creating a variable local to that file
to refer to the real global global you should use _G. in front of your variable…

you should change the variable to _G.testglobal in your assignment statement.

print function will be looking at the global from main.lua itself. [import]uid: 71210 topic_id: 12623 reply_id: 46194[/import]

Thank you so much! [: [import]uid: 74723 topic_id: 12623 reply_id: 46196[/import]