0

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

Nifim
  • 4,758
  • 2
  • 12
  • 31
sidd
  • 33
  • 5

2 Answers2

1

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...
Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • I am not aware how to create a table with multiple different data types ! Can you please guide me ? – sidd Dec 09 '21 at 07:12
  • I am using sol2 library to get this ArgumentList in sol::variadic_args. But the problem is when I pass ArgumentList have 4 different elements, the size of ArgumentList on C++ side using sol2 is just 1. I need to explicitly pass it this way, ui.InvokeFunction(parameter1, parameter2, ArgumentList[1], ArgumentList[2], ArgumentList[3], ArgumentList[4]) But this is not the right way, as everytime I might not know the total number of elements in advance to pass it as index – sidd Dec 09 '21 at 08:56
  • You need to provide some specific example, as if you initialize `ArgumentList` as I described in my answer, I don't see why the size of that table would be 1 in your function handler. Maybe the misunderstanding stems from the fact that you're talking about ArgumentList as presented by `variadic_args` and I simply assume that it's a Lua table that can hold all the values you describe. It would be better if you can update your answer to provide more information on how you expect the Lua code to look like and why you can't simply use a table to pass all those values. – Paul Kulchenko Dec 10 '21 at 03:09
0

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.

RH2O
  • 11
  • 3