comiconor
|
  |
| Joined: 26 May 2009 |
| Total Posts: 16893 |
|
|
| 04 Feb 2012 04:12 PM |
| Do you think it's possible to run a function through a table in Lua? |
|
|
| Report Abuse |
|
|
|
| 04 Feb 2012 04:14 PM |
Look at your post in SH.
-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]- |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 04 Feb 2012 04:14 PM |
Could you rephrase the question? What do you mean "run functions though an array"? If you mean putting function in a table, yes. Example:
function add(a,b) return a + b end function sub(a,b) return a - b end function mul(a,b) return a * b end
local t = {add,sub,mul} print(t[3](3,4)) --12 |
|
|
| Report Abuse |
|
|
comiconor
|
  |
| Joined: 26 May 2009 |
| Total Posts: 16893 |
|
|
| 04 Feb 2012 04:16 PM |
| So, if you put them in a table, valued 1 to... however many, you could pick a random number and then that function runs, just as an example... |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 04 Feb 2012 04:37 PM |
local function one() print("One called") end local function two() print("Two called") end local function three() print("Three called") end
local t = {one,two,three} t[math.random(#t)]()
Yeah. Lua can do that, if that's what you mean. The above code segment should call a random function.
|
|
|
| Report Abuse |
|
|
comiconor
|
  |
| Joined: 26 May 2009 |
| Total Posts: 16893 |
|
|
| 04 Feb 2012 04:46 PM |
| You put in #t - is that 1-#t or 0-#t |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 04 Feb 2012 04:52 PM |
"You put in #t - is that 1-#t or 0-#t"
#t is the number of elements in t? t[#t] is an index, since Lua uses t[1] as the first index, not t[0]. |
|
|
| Report Abuse |
|
|
|
| 04 Feb 2012 06:12 PM |
| Lua, and C++ are very different languages. That's like comparing HTML, and Java. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 04 Feb 2012 06:30 PM |
Yes, in fact you can do even more in Lua since functions in Lua are first-class objects in Lua, unlike in C++. You can do anything with a Lua function that you can do with any other Lua value, including fun stuff like storing them to tables and passing them as arguments. You can even access local vars from the scope that the function was created in unlike in C++:
function blah() local a = 0 return function() a = a + 1 print(a) end end
count1 = blah() count2 = blah() count1() --> 1 count1() --> 2 count2() --> 1 count1() --> 3 |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 04 Feb 2012 06:31 PM |
| Stravant, that's now possible with C++11 lambdas :D |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 04 Feb 2012 06:55 PM |
| No it is not. If you write that and it does work that's just because you're lucky and it hasn't crashed or otherwise misbehaved yet. Lambdas in C++11 can only capture a copy of their environment by value, they are not like Lua closures in that regard. |
|
|
| Report Abuse |
|
|
comiconor
|
  |
| Joined: 26 May 2009 |
| Total Posts: 16893 |
|
|
| 04 Feb 2012 06:56 PM |
@Ninja
You do realise Lua and C++ are related, right? |
|
|
| Report Abuse |
|
|
myrkos
|
  |
| Joined: 06 Sep 2010 |
| Total Posts: 8072 |
|
|
| 04 Feb 2012 06:59 PM |
| Stravant, if you put a '&', doesn't that mean you access by reference? |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 04 Feb 2012 07:42 PM |
| Yes, but what that reference points to is only defined as long as the variable you captured is still in scope. |
|
|
| Report Abuse |
|
|
|
| 04 Feb 2012 08:18 PM |
SO THAT'S WHAT THEY'RE CALLED! "So, a value instantiated at run-time ..." is much more annoying to say than "So, a first-class object ...".
Anyway, t = { function() print("HI") end, function() print("Bye") end, function() print("Hiyakacha") end, function() print("Merlin") end }
t[1]() --> HI t[2]() --> Bye t[3]() --> Hiyakacha t[4]() --> Merlin
http://wiki.roblox.com/index.php/Function#Functions_Within_Tables |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 04 Feb 2012 09:19 PM |
| If you do want to do stuff like that in C++, you really ought to use classes that pretend to be functions by overloading the operator(). |
|
|
| Report Abuse |
|
|
|
| 05 Feb 2012 07:02 AM |
| wait how can you store pointers to a method in C++? :L |
|
|
| Report Abuse |
|
|
Varp
|
  |
| Joined: 18 Nov 2009 |
| Total Posts: 5333 |
|
|
| 05 Feb 2012 10:06 AM |
"wait how can you store pointers to a method in C++? :L"
int add(int a,int b){ return a+b; }
...
int(*f)(int,int) = add; //or &add in some compilers; I think the C++ standard doesn't require it std::cout << f(3,4) << "\n"; //Prints 3+4, 7
Alternatively, you could use the std::function template defined in < functional >; this is often preferable since std::function< int(int,int) > can bind to int(*)(int,int), but also anything with a method of the signature int operator()(int,int):
std::function< int(int,int) > f = add; std::cout << f(3,4) << "\n"; |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 05 Feb 2012 11:15 AM |
| Not to mention, std::function can also store the new lambdas from C++0x. |
|
|
| Report Abuse |
|
|
|
| 05 Feb 2012 01:36 PM |
hmm
maybe I will just stick with C#. O.o |
|
|
| Report Abuse |
|
|
mattchewy
|
  |
| Joined: 19 Feb 2008 |
| Total Posts: 7300 |
|
|
| 05 Feb 2012 01:41 PM |
| Functions are the same as any other variable in Lua... |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 05 Feb 2012 01:53 PM |
"SO THAT'S WHAT THEY'RE CALLED! "So, a value instantiated at run-time ..." is much more annoying to say than "So, a first-class object ..."."
One more thing to add, that's not quite true. Something can be a first-class object without it being allocated at run-time. First-class value just means something that abides by the normal value semantics of the language, it has nothing to do with the lifetime of the object.
The typical examples of non-first-class values are functions and type reflection metadata. Many languages have these, but not as first-class values, that is, they have special-case bits of syntax that is used only with them.
Java is an example of a language where pretty much everything is first class. In Java the only non-first-class concept is the types of the built-in numeric types. You cannot do something like "int a = 3; a instanceof Number;", that is, they are a special case in the language. |
|
|
| Report Abuse |
|
|