Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

quasty

LUA tuple and function "pointers"

This topic is 5264 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've read some about the LUA tables. I didn't have much success in realizing it but the thing I want would be a table with references to functions - I've seen that it's somehow possible (for example the table.foreach function) but how do I manage it: I want a set of values assigned to a function or simply callable code:
t = { 
{"valA1", "valA2", func_valA1},
{"valB1", "valB2", func_valB}, 
}
 
Now I want to search for certain value compinations and execute the given function to that entry. I don't really know how to do it.. ??? Here I've used tables in tables - is there another way to store "tuples" in lua - example like the multi return values of functions? thankx alot! [edited by - quasty on June 11, 2004 2:56:02 AM]

Share this post


Link to post
Share on other sites
Advertisement
Well, you're touching on a few things here.

First of all, your example is a bit unclear. Do you want to, say, execute func_valA1 when some variable is set to "valA1" and some other variable is set to "valA2"? Or do you want to execute func_valA1 when some variable is set to either valA1 or valA2? Either is possible, but the techniques will differ.

With that said, referring to functions in tables is easy. You can either make the functions and then refer to them by name, or you can declare them inline. Watch:


function calculate(op, a, b) do
local optable = {
add = function(a,b) do return a+b end,
sub = function(a,b) do return a-b end,
mul = function(a,b) do return a*b end
}

return optable[op](a,b)
end



-- Example of use
calculate("sub", 7, 3) -- prints 4

Even naming "optable" is optional, but I left it in there for the sake of readability.


"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on June 11, 2004 3:09:40 AM]

Share this post


Link to post
Share on other sites
Thank you, but I''m afraid I didn''t understand it right.

What I meant a pre-defined table with elements. Every element is composed of a set of (string) values in a certain way. The last element should be a "pointer" to a function.

At the moment I start my lua skript these table is already full of element. What I want is to search the list (I only have a set of strings) and look up the function assigned to the string elements.

Share this post


Link to post
Share on other sites
In that case, the only really nontrivial part is the lookup.

Your current representation is a little unwieldy. You''d need to do a linear search through it, which is much slower than it needs to be.

I suggest you have the table keys be the string attributes concatenated into single strings, and the values be the functions. That way, you can just concatenate the desired string values, look that key up in the table, and execute the resultant function.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
Hi,

I''m afraid I''ve again a bit of a problem...
I''ve been trying it over the weekend and got it finally working - for strings. The one thing i still don''t understand:


t = {["abc"] = foo }

function foo()
print("foo")
end
How do I execute the function foo? Simply >t["abc"] isn''t working and I couldn''t find anything about it in the ref-manual or Wiki.

thanks alot!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!