[Solved]Assign argument variables in function parameter?

Started by zutokaza, May 16, 2017, 07:20:13 AM

Previous topic - Next topic
How would I do something like this?

Code ( lua) Select
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:

Code ( lua) Select
function test(a, b, c)
a = 5
b = "hey"
c = true
  if a == 5 then
    print("Letter A is",a)
  end
end

test()
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616

May 16, 2017, 11:30:28 AM #1 Last Edit: May 16, 2017, 11:33:26 AM by MetalZelda
Quote from: zutokaza on May 16, 2017, 07:20:13 AM
How would I do something like this?

Code ( lua) Select
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:

Code ( lua) Select
function test(a, b, c)
a = 5
b = "hey"
c = true
  if a == 5 then
    print("Letter A is",a)
  end
end

test()


Code (lua) Select
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

May 24, 2017, 08:05:43 PM #2 Last Edit: May 24, 2017, 08:07:14 PM by zutokaza
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.

Code ( lua) Select
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)
    })
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616

In that case you simply make a function that takes a table for the first parameter:
Code (lua) Select
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!
Solarus Works on ReactOS Opensource Windows OS

https://www.reactos.org/forum/viewtopic.php?f=2&t=14759&p=120616#p120616