16

I'm looking to develop for a game that uses Lua as a scripting language.

I have the following statement:

foe:add_component 'render_info':set_scale(0.5) 

I understand that : means that the calling parent object will be passed into called method as the first argument. But in this case, there are no parentheses after render_info; there's what seems to be a bare string literal with a method being called on it, which really seems to make no sense.

I also understand the concept of metatables and metamethods, which add_component may be an example of. However, there are no "blank space" operators to override that I have found. I am wondering if there's something major I'm missing about the grammar of Lua.

What does the above code statement mean? What is each part, and what is it doing?

2 Answers 2

31

If a function has only one argument and this argument is either a literal string or a table constructor, you can omit the parenthesis on function call.

Quote from the lua reference manual:

A call of the form f{fields} is syntactic sugar for f({fields}); that is, the argument list is a single new table. A call of the form f'string' (or f"string" or f[[string]]) is syntactic sugar for f('string'); that is, the argument list is a single literal string.

So this is equivalent to your call:

foe:add_component('render_info'):set_scale(0.5) 
Sign up to request clarification or add additional context in comments.

Comments

9

If the only argument is a string or a table constructor, there is no need for parenthesis for a function call. It might not be so clearly said in the manual though: http://www.lua.org/manual/5.3/manual.html#3.4.10

Here is a small example code you can try out:

foe = {} function foe:add_component(str) print(str) return foe end function foe:set_scale(scale) print(scale) return foe end foe:add_component 'render_info':set_scale(0.5) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.