简体   繁体   中英

Lua :- How to create a variable that can have mix data types and pass it as an argument

I have a requirement of passing data in arguments.

Which can be

1) A Single String -> 'StringData'
2) Multiple Strings -> 'StringData0', 'StringData1', 'StringData2'
3) Single Numeric data -> 10 OR 30.22
4) Multiple Numeric data -> 10, 20, 30 OR 30.22, 12.01, 1.4  
5) Mix of String, bool, int, double -> 'StringData', true, 10, 45.33, false

Is there any way that I can create a Variable that can accept any of the above possibility

ui.InvokeFunction(parameter1, parameter2, ArgumentList)

I want to fill the data in ArgumentList variable which can be any of the above 5 possibility. I did not find any way to insert multiple data types in ArgumentList

I don't see any problem, as you can pass any value in Lua as a parameter and check its type using type function. Also, if you need to pass multiple values, you can pass a table (which can have values of different types).

ArgumentList = {'StringData', true, 10, 45.33, false}
-- type(ArgumentList[1]) == 'string'
-- type(ArgumentList[2]) == 'boolean'
-- and so on...

I'm no expert at lua but I think that doing something like:



Argument_List = {
  1 = {
   boolean = true
   string = ""
   other = 69
   table = nil
  }
}

valid_types = {
 "string" = true
 "boolean" = true
 "number" = false
 "table" = false
}

filtered_list = {}

for _,v in pairs(Argument_List) do 
 for x in v._ do 
  if valid_types[type(x)] then 
   table.insert(filtered_list._, {type(x) = valid_types[type(x)]})
  else
   table.insert(filtered_list._, {type(x) = false})
  end
 end
end

I'm assuming that would be an inefficient way of what you're requiring but hopefully that helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM