syntax for calling modules oddity (hump timer)

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

syntax for calling modules oddity (hump timer)

Post by parallax7d »

I'm looking through some code of other games, and notice that one is using the hump timer module.

I thought the normal way of calling it was something like this:

Code: Select all

Timer.addPeriodic(10, function()
  ghost:stop()
end)
But I see in this code they do this:

Code: Select all

TIMER = Timer()
and their calls are like this:

Code: Select all

TIMER:addPeriodic(10, function()
  ghost:stop()
end)
I'm fairly new to method calls, and I'm wondering what reasons someone would have for doing this? And also, how does it actually work? Is it because hump.timer has a __call meta method that allows it to be called like a function? Honestly, after staring at the hump timer module I can't wrap my head around it's return statement.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: syntax for calling modules oddity (hump timer)

Post by Ranguna259 »

In the particular case, the game you were looking at simply used : for aesthetics, I actually prefer to use : over . in some cases.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
parallax7d
Citizen
Posts: 82
Joined: Wed Jul 02, 2014 11:44 pm

Re: syntax for calling modules oddity (hump timer)

Post by parallax7d »

Oh, ok. I thought I was missing some fundamental thing about working with modules. So it's just a personal preference, there isn't any advantage to it?
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: syntax for calling modules oddity (hump timer)

Post by Ranguna259 »

No, not at all. BTW sorry for the late reply :P
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: syntax for calling modules oddity (hump timer)

Post by Kingdaro »

In this case, it wasn't "just for aesthetics". That timer library specifically is part of vrld's hump library. The differentiation here is that, when using the dot, you are calling a function on the required library itself, and when using the colon, you are calling a function on an instance of the library. The difference between these two methods is either having just one place to use the library, or multiple versions of the library to be used and updated separately from one another, usually for organization and functionality.

For instance, you could create two different instances of the library, one to always be updated, and the other to only be updated while the game is unpaused.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: syntax for calling modules oddity (hump timer)

Post by Ranguna259 »

Ah since I didn't understand the nature of the lib I assumed it was just for the looks :P
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: No registered users and 211 guests