LUA - reordering tables doesn't seem to "stick"?

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

LUA - reordering tables doesn't seem to "stick"?

Post by heyvern »

I am at my wits end.


I am creating a table of bones and IDs based on bone count. Obviously this is not a proper sequentially listed table. So I have a function that should reorder a table with pairs in alphabetical order.

When I print the table using table.foreach the order goes back to what it was before I reordered it.

I even "copied" the table during reordering to a NEW table and the new table is also "out of order".

This is the function I am using that I found on the web:

Code: Select all

    function pairsByKeys(t, f)
      local a = {}
      for n in pairs(t) do table.insert(a, n) end
      table.sort(a, f)
      local i = 0      -- iterator variable
      local iter = function ()   -- iterator function
        i = i + 1
        if a[i] == nil then return nil
        else return a[i], t[a[i]]
        end
      end
      return iter
    end
This code works. It returns the key and value of pairs in a table in order based on the key. After I build a table using the new order the new table isn't in that order... shouldn't a table built from the results of this function maintain that order? Or am I missing something?

Let me rephrase that... I know I am missing something. I understand that tables aren't necessarily in any specific order. How the heck do I create a table in a specific order... or is that not possible? Do I need to run the table sort during what ever final process that needs to be done?

In the examples I've seen a new table created from the new order maintained the order... I may be missing something though.

-vern
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I think I understand now.

To reorder the bones from a list or table I need to do the ordering before the bones are "stuck back in".

The LUA tables don't maintain ordering based on creation. I need to sort when the script is ready to "do its stuff".

-vern
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

Perhaps you should read this article for a quick recap of what Lua is all about:
http://www.lua.org/spe.html
______

On a sidenote, I just listened to this hour-long podcast about The other digital divide on ITConversations. It discusses the profound notion that people who use computers, but don't know how to program, miss out on a lot of what they could do with their computers. So even if you're lucky enough to have access to a computer, you could still be on the wrong side of yet another digital divide. If you any bit interested in programming, I suggest you either download the mp3 file or listen to the audio on the ITConversations website with the Flash player.

Meaning that actually knowing something about programming (and having done some of your own) is an essential skill, even for animators in this day and age.

This means that what LM wrote some time ago, really makes no sense:
Writing scripts is a different kind of skill than animating. Some people can do both, but we don't want it to look like you need to understand scripting in order to use Moho. Scripting is kind of a "bonus" feature, and something that non-programmers should feel free to completely ignore if they wish.
I beg to differ with LM. Scripting is no "bonus" feature, it is an essential feature, in order to understand how to use Moho (now Anime Studio Pro) more effeciently. You don't have to be an active scripter, but you should at least have attempted to understand what goes on behind the scenes.

My opinion is that animators who have no understanding of programming languages and developing software in general, and of the Lua scripting interface of Moho or AS specifically, are missing out big time. They are on the wrong side of the digital divide.

There's my little rant in a thread. Sorry to have hijacked your thread, Vern, but I had to let this out.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Rasheed,

I agree 100%.

Over the last few days my knowledge of LUA has increased a million times! (not that amazing considering how little I knew to begin with. ;) )

I am starting to really get a grasp of it. One break through was FINALLY understanding how to create and access tables!!!

I found this link to a forum entry:
What are tables...

Lots of great stuff.

I've also learned about UI design with LUA and AS for the pop up windows. It's not that scary! It reminds me somewhat of the very early days of HTML... when you only had a few options for layout and that was it.

I still don't know how to do those amazing bits of sparse coding. In the copy/paste bones script that ThaNarie created there is some really amazing one line code bits that like... do 3 comparisons and stick the result in a table... without using if then or while... <sigh> I don't get it.

I am getting better though.

-vern
Post Reply