How would I do something like this?
function test(a = 5, b = "hey", c = true)
if a == 5 then
print("Letter A is",a)
end
end
test()
I know it can be done like this:
function test(a, b, c)
a = 5
b = "hey"
c = true
if a == 5 then
print("Letter A is",a)
end
end
test()
Quote from: zutokaza on May 16, 2017, 07:20:13 AM
How would I do something like this?
function test(a = 5, b = "hey", c = true)
if a == 5 then
print("Letter A is",a)
end
end
test()
I know it can be done like this:
function test(a, b, c)
a = 5
b = "hey"
c = true
if a == 5 then
print("Letter A is",a)
end
end
test()
function test(a, b, c)
if a == 5 then
print("letter A is " .. a)
-- You can even modify your variables from there
end
return a, b, c
end
test(5, "hey", true)
The arguments should be when you call that function, not when you define it.
Of course, "return a, b, c" is optionnal, it's only when you want to store a, b or / and c in a variable, but since the function is called "test", you might wanna return some result somewhere else
Yes, I know of that method too. I want to know if it is possible to do like the surface create function. I would like to assign variables in a table for easier reading.
sol.text_surface.create({ -- name a local variable something and assign it to the sol.text_surface
font = "minecraftia", -- font name
text = "what", -- text you want to show
font_size = 50, -- font size obviously
horizontal_alignment = "center", -- default "left"
vertical_alignment = "bottom", -- default "middle"
rendering_mode = "antialiasing", -- "solid" (faster) and default
color = {0,0,0}, -- color must be in a table RGB (http://www.rapidtables.com/web/color/RGB_Color.htm)
})
In that case you simply make a function that takes a table for the first parameter:
function test(info)
if info.a == 5 then
print("Letter A is",info.a)
end
end
test{a=5, b="hey", c=true}
Keep in mind that the table passed to the function may not have values defined for every entry in the table. It also might not even be a table that gets passed to the function, so be sure your code handles all the possibilities.
@llamazing
Exactly what I wanted. Thanks again for your assistance!