 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Sun Aug 26, 2007 10:41 pm
Using Lua in CMUD |
So, I decided to post my tutorial a little bit before it's done. Expect chapters 8 and 10 to be up in a couple of days - 9 might take a bit longer, because I'm waiting for Zugg to document some of the features ;)
The tutorial is aimed at total beginners who've never written a word of scripting, but that doesn't mean that you oldies won't find it a nice introduction too. Even zScripting and zMUD/CMUD experience is optional (though it might help).
It's possible (some would say likely) that this version will have lots of errors in. If you find something that's got tags in the wrong place, or examples that don't work, or typos, or bits you think need expanding or contracting - in fact, if you have any feedback at all - post it here or as a comment on the chapter in question. It's only through your feedback that it's going to get any better, so make sure you comment.
If you get stuck, post in this thread and I'll try and help out. Don't just sit there boggling - if you're getting stuck on a section, it could be that that section needs improving. Better to post just to make sure.
And so, without further ado:
Using Lua in CMUD - the (Complete and Utter) Novice's Guide
by Fang Xianfu |
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Thu Dec 06, 2007 5:45 am |
I decided to do a bit of spring cleaning on the ol' guide. I'm deleting comments from the articles as I update things (or don't) and in the past when I'd done a bit of spot cleaning, I just deleted them and made the changes. This time I'll quote the relevant bits.
Nick Gammon wrote: |
Quote: |
You can have as many or as few spaces and new lines in your script as you like and Lua doesn't care.
|
With one exception. You must put the bracket following a function call on the same line as the function. For example:
Code: |
print
("One string")
--> error: ambiguous syntax (function call x new statement) near '('
|
|
I've added something to reflect that.
Nick Gammon wrote: |
Quote: |
More String Delimiters
|
Also, strings can be delimited by: [[ .... some string ... ]]
This lets you write multi-line strings which can be quite handy, and also imbed quotes in them, eg.
Code: |
print ( [[
Sally said, "Bob, you smell of cheese!"
"Well, I do work in a cheese factory!" Bob retorted.
He then put Jim's machine to work.
]] )
|
|
The original draft included this one, but I decided to remove it - it gets too complex for a beginner's guide when you start talking about nesting them using [=[whatever]=] and such. I'm going to leave it out.
Tech and Nick both commented on my pointing metaphor in chapter 3 - that's now gone, and replaced with the box metaphor that I'm using for tables anyway.
Nick Gammon wrote: |
The relevant point to make is that tables are (effectively) pointers, and that if you have multiple variables "pointing to" the same table, and you modify one of them (eg. by changing a key/value pair) then all the other ones are changed also. However, I would make this point in the tables section. |
And I've done just that.
Tech wrote: |
You should note that since Lua 4.0, all math.sin, math.tan, math.cos etc work with radians instead of degrees.
To convert from degrees to radians you use math.rad. [ You can also use math.deg to convert radians to degrees.]
What this means is that
Code: |
print (math.cos(90)) |
will result in
But
Code: |
print (math.cos(math.rad(90) ) ) |
will return
as expected. |
This is a bit beyond the scope of this tutorial. Since I'm urging people to look up the functions in the manual anyway, hopefully that'll explain this.
JQuilici wrote: |
Also, please note that the first argument to zs.func.prompt is the default value, and the second is the prompt question. So most of the examples should have something like:
Code: |
name=zs.func.prompt("","What is your name?") |
|
This was actually a real pisser. Since I hadn't actually explained about using multiple function arguments, I couldn't really use them. I decided just to leave it how it is, since it does the job.
Nick Gammon wrote: |
Quote: |
Anything that can be the value of a variable can be a value in a table, but anything can be a key, too.
|
Anything, except the "non-value" nil. You can't do this:
Code: |
t = {}
t[nil] = 42 --> error: table index is nil
|
Also, it might be worth pointing out that assigning nil as the value effectively deletes the item from the table. eg.
Code: |
t = { apples = "tasty" }
t.apples = nil --> item "apples" now deleted from table
|
|
I can't believe I missed out var=nil! Thanks for that.
And finally, to all those people who keep asking for updates - I know, I know, I need to finish it. And I will! I swear! I just decided to put off writing the rest until the Lua interface is documented, so I can finish it all at once instead of doing it in bits. The interface documentation should be done in a couple of weeks, which means I'll hopefully be done this month. |
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Dec 06, 2007 2:53 pm |
Thanks Fang. I'll try to get the zs object interface documented next week. It's probably a bad idea to document it *after* the public release, because I know that makes it hard to debug it. But since Lua is relatively new, it's been a bit lower on my priority list (it doesn't cause any compatibility issues with either zMUD or CMUD 1.34 for example). If you find Lua bugs after I document it more, then I will fix those early next year when I do additional public version updates. Thanks for your patience on this.
|
|
|
 |
Fang Xianfu GURU

Joined: 26 Jan 2004 Posts: 5155 Location: United Kingdom
|
Posted: Thu Dec 06, 2007 4:39 pm |
It's probably just semantics, but it's not just the zs object (though docs for zs.var would be sweet), it's stuff like the Window object type and what it's for (which is especially weird given that the zs object itself is a window), and the windows table that contains all your windows. I imagine that you're not supposed to interact with the new underscore-prefixed tables and userdatas like _p, _CMUD_sys, _CMUD_param, _mudobj and whatnot, but it'd be nice to know what they're for anyway. I notice that _CMUD_window contains functions to get, make and execute different settings, so I'm guessing that it's the metatable for Window objects. Knowing stuff like that would be useful if you want to do something really weird like create a wrapper that affects all calls from Lua.
|
|
|
 |
Zugg MASTER

Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Dec 06, 2007 4:45 pm |
Yeah, the zs object *is* the Window object type and that's what really needs documenting. As far as some of the internals...definitely don't mess with them, and I'll eventually document them, but probably won't go into a lot of details about them. Messing with them would just be asking for trouble ;)
|
|
|
 |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|