Lua basics

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
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Lua basics

Post by synthsin75 »

I'm no programmer, and I've been hunting around for a really basic Lua tutorial. Really basic. I think I've found one. I think Lua is starting to make sense at last. Everything else I've seen assumes previous experience with programming languages.

Anyway, I figured I'd share it. http://nixstaller.berlios.de/manual/0.2 ... ler_9.html

Hopefully I can get somewhere with this.
User avatar
mkelley
Posts: 1647
Joined: Fri Nov 02, 2007 5:29 pm
Location: Sunny Florida
Contact:

Post by mkelley »

Yes, that's a pretty good one for someone who has never programmed before.

Most programmers won't have any troubles with Lua -- it's about as straightforward as they come. Objects and properties might give those who have never dealt with an object orientated language pause, but that's rarer and rarer these days.

What I'd *really* like is something that mapped out the objects and functions available to us in AS -- there is some VERY ragged documentation that was released early on by Mike (and is must reading for anyone doing AS programming) but it either wasn't complete when he wrote it or has become obsolete since. Studying the scripts done by others will reveal much, but because not everyone writes in a clear and concise manner it's probably not the best place for a beginner to start.

I've done mods of some scripts to suit me personally, but nothing really useful to others like Vern has accomplished.
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Yeah Mike, I don't even know html. I've been told that the best place to start is with menu scripts, then tool scripts, then layer scripts. But what I really need is to modify a layer script.

I have the feeling it's a fairly easy mod, but I just don't understand enough yet to pull it off.
User avatar
mkelley
Posts: 1647
Joined: Fri Nov 02, 2007 5:29 pm
Location: Sunny Florida
Contact:

Post by mkelley »

If you get stuck PM me and I'll see if I can help.
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Well I've backed up a bit. Since there don't seem to be any really basic, as in nonprogrammer, tutorials for lua, I've decided to learn any basic programming language in the hopes of it translating well.

I've found a very dumbed down java tutorial that I am going through now. It seems to have much in common with lua, but I still wonder if I'm going to find any pitfalls learning one language in order to use another.

:roll:

::So far I notice that the unequality symbols are different (!= in java, ~= in lua). Also I'm not sure if lua has 'show' or 'input' commands.
User avatar
mkelley
Posts: 1647
Joined: Fri Nov 02, 2007 5:29 pm
Location: Sunny Florida
Contact:

Post by mkelley »

Wes,

As a long time (30+ years) programmer I can tell you that almost anything you learn in one programming language will stand you in good stead in another.

All of the concepts like looping, if-then tests and branching, functions and objects are in about every language nowadays (and have been for the most part for decades). So find any language which speaks to you, learn to use it, and you'll be fine.

The biggest advantage with learning Lua is you can be doing something useful while you learn -- biggest disadvantage is that using AS as a debugging/learning tool ain't easy (I kind of hate you have to reboot AS just to get it to look at a menu script, etc.)
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

(I kind of hate you have to reboot AS just to get it to look at a menu script, etc.)
Ctrl>F5 reloads scripts (menu & tools) without having to restart AS. So as long as your test files are located in the AS scripts folder, you just save your changes and ctrl>F5 in AS to test.


Yeah, I'd much rather learn using lua. I have the lua interpreter, and have worked through some of the lua tutorials, but there seemed to be some basic info missing. I doesn't really help to know how to do something if you don't have a clue as to what it is you are doing.

I quickly gave up on flash because of the scripting, but AS is too addictive. It's kind of hard since AS lua is really all I'm interested in at the moment. :roll:
User avatar
mkelley
Posts: 1647
Joined: Fri Nov 02, 2007 5:29 pm
Location: Sunny Florida
Contact:

Post by mkelley »

Hey, thanks -- that does help a lot.

What looks like it's missing is an actual project to do -- something like the "hello world" (and building from there) that we teachers all love to give to students.

Now, you *could* take a very beginning language (say Basic -- is line command Basic even around anymore?) and use their beginning tutorials but just do them in Lua, accounting for the syntax differences. That's really all it is, is just syntax. I'd try looking around the web for you, but my heart just isn't into it anymore (and I have no idea where the tutorials are that I used to teach from -- that was decades ago). But that's what I would do if I were you -- find something that just shows you how to DO something with a loop or object construct, and just translate it into Lua.

I suppose if I were back into teaching again I could even write an AS Lua tutorial -- best way to learn is to teach something -- but I'd much rather draw and animate nowadays. Still, if you do get stuck on any concepts or even actual procedures let me know because I really will be more than glad to help (and it will help me).
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

IMPORTANT!

AS uses lua v5.0.1

Most of the stuff out there is for 5.1 or later. Much of it won't work and it could get very confusing.

There is one online book first edition that covers 5.0.1. It really makes you frustrated when you see all the stuff we can't use in the later versions of lua.

I hope Mike updates it on the next version. Although it could break some of the scripts because of deprecated code.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Okay, first question:

Code: Select all

string.sub(s, i [, j])
s:sub(i [,j])
Are these just different ways the same thing can be expressed in lua?
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Okay, I've tried a few changes, but I don't know what "mi[2]" means in this code.

Code: Select all

if (string.sub(name,-4) == ".dup") then
--copy instance
		if (mi and ownLayer.FA_MeshInstName == string.sub(name,1,-10)) then
	--cached
			if (mi[2] ~= moho.frame) then
				print("MeshInstance: wrong order - swap the source and the copy!")
				return
			end
			tab= mi[1]

			if (mi[3] < len) then len= mi[3] end

			for p= 1, len do
				local pt= mesh:Point(p - 1)
				pt.fPos.x= tab[p][1]
				pt.fPos.y= tab[p][2]
			end
		else
			local srcName= string.sub(name,1,-10)
I think that first "string.sub" is fine for finding the instance. I changed the second and third "string.sub" in the hope of being able to use five extra characters in the instance names(ignored by the script). The problem now, I think, is that I've unbalanced the "if (mi[2] ~= moho.frame) then
print("MeshInstance: wrong order - swap the source and the copy!")" portion of the script.

This is the only lua error it is spitting out now, but I'm not really sure if the stuff I've done is valid. I'm guessing that this "mi[2] == moho.frame" equality is the problem.

Of course, I could be completely wrong. Please let me know if I'm anywhere near right. :? :roll:


::Later in the script are more 'name' references and that 'mi[2]' appears to be assigned later in the script??? Do scripts not run linearly?
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

Well Vern kindly removed my current motivation for learning this, so now I need to find me another target.

Anyone know what the simplest AS lua script is? Maybe I can start modifying something simple enough to understand what I'm doing.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

Basically all I did was to change the stored "srcName" variable.

It is the name of the layer being stored in a "permanent" global variable.

With the "old" script this variable was the whole name minus the .dup part to find the "master" layer. I used a regex search pattern to search for anyname.arm.dup and strip out the "arm.dup" part.

Code: Select all

			local __,__,bob = string.find(name, "%.(.+)")
			local srcName= string.sub(bob,1,-5)
Local "bob" is my silly name for a variable. Don't make fun of me. It is a string.find regex search pattern that simply returns "arm.dup" as a new string.

name is the name of the layer. "%." is a "period" or ".", the % is the escape so it looks for the actual character not the regex pattern for ALL characters.

The next part is "(.+)" which really is looking for 1 or more of ANY character to the end of the string (arm.dup). The "()" around that indicates a "capture" so that part of the string is "returned" to the variable "bob". Notice the "()" are only around the last part of the search, it doesn't include the first "." found.

"_,_,bob" the underscores indicate that I don't need the first two parts of the string.find. string.find returns up to 3 values, the search result beggining postition, last postition, and any captures. The undescores are just "empty" holders to skip them. Bob gets assigned the returned capture.

I then add that to the existing code:

Code: Select all

			local srcName= string.sub(bob,1,-5)
Instead of:

Code: Select all

			local srcName= string.sub(name,1,-5)
Now "srcName" is "arm" (counting back 5 from the end of "bob"). Then, it works just like the old script works. It is looking for "arm.dup" so I give it "arm.dup" and "delete" the first part of the name.

That is what I meant when I said I could "tag on" my changes on top of the existing code. Easier than trying to figure out exactly all the crazy stuff going on and "do it right".

EDIT:

There is one more spot where "bob" gets used towards the middle of the script:

Code: Select all

		   ownLayer.FA_MeshInst= mi
		   ownLayer.FA_MeshInstName= bob
I am not clear exactly what "mi" is or does. All I know is that "bob" is what the original script was looking for (arm.dup). Once that's there the script works exactly the same.

-vern
User avatar
synthsin75
Posts: 10280
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Post by synthsin75 »

That sounds like what I was trying to do, but I had no idea about needing that "string.find" (regex search?).

This good stuff. Let me see if I have that right. I know that 'string.sub' returns a portion of the string. I assume 'string.find' works in a similar manor.

So the '%.' finds strings from the starting point of '.'? And the escape symbol is to keep it from just stopping at '.'? I'm sure I'm not getting that bit right.

Then the capture '()' returns everything after that first '.' to the variable 'bob'? (.+) means from character '.' to the end of the string?

I've found the references to all this on lua.org, but it seems to assume some programming knowledge.

A really appreciate you explaining this bit. Thanks.
User avatar
heyvern
Posts: 7042
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

string.find is very similar to string.sub. String.find can find AND return the value. string.sub needs to have the search start and end points assigned. If you want a specific portion of the string you have to know where it would start and stop.

When doing a "regex" or regular expression search (also called "grep") there are certain special characters. Each language has its own way of doing things.

In lua special character types can preceded by a % sign (%d for any digit). Any time you need to actually search for a REAL character and not use it's special regex meaning you have to escape it by putting a % in front of it.

. is the symbol for any character.
[] indicates a "set" [0-9 A-Z a-z l-q 3-6]
* after a character says find 0 or more of that pattern.
+ means find 1 or more.
- means find 0 or 1

When doing stuff with lua and AS, regex is a huge part of it. You are either looking for layer or bone names, or portions of names like extensions etc. It can be a bit confusing but the more you know about regex the better.

You can also do search and replace. This is what I used for the save bone animation scripts. When I read the file in I have to replace the stupid line breaks because they are different on each platform (max/win).

I actually have to run it through like 3 different patterns to get the right format for lua to read each line. I kept getting weird errors when a file came from PC to mac or visa versa. The lua script interface in AS assumes that a line on a PC is a specific character. When reading it in on a different platform the characters aren't right.

-vern
Post Reply