Building my Reflexes

by Severian

Back to Mechanic's Corner.

Xinael2007-10-27 06:14:23
Function definition format is either
CODE
function name (params) body end

or
CODE
name = function (params) body end

where name is a variable name, params is a list of params separated by commas (local variable names), and body is whatever you want the function to do. The book Programming in Lua is pretty comprehensive. It's a bit dry, but if you've scripted before it's not too bad. The online version is for an old version of Lua, but the only thing that's different (discounting the C API) is the syntax for a variable number of arguments to a function, which you probably won't need.

And @Faymar: you're right, sort of, but remember that the expression "elixaff.aeon ~= nil" is going to return a boolean, which will then itself be checked against nil and false. That's the extra check.
Severian2007-10-27 06:23:03
Ah this is so much to take in at once. Does Programming in Lua cost anything? I've browsed through the online version and haven't found much helpful, but I'm a bit unfocused generally, and can't make it through a few pages without being distracted into something else. I'll look again over the mushclient forums and see how far in I can get on that tutorial. Thanks!
Xinael2007-10-27 06:40:29
The book does, of course, but the online version that I linked to there is free. And like I said, the only part of it that's out of date for the main body of Lua is one you'll probably not need.

Reading the whole thing (except the C API section) would help, but there's no reason why you can't dip into, say, the functions section and just read that.

Also, it was written by the chief designer of Lua, so it's going to be accurate ;P
Severian2007-10-27 06:43:13
Right, I feel the only drawback is that, while his information is accurate, it's not specific for coding in MUDs, let alone Lusternia. Mushclient forums and documentation have a more focused look at that. It's an almost negligible toss up, but perhaps worth mentioning.
Faymar2007-10-27 08:09:09
QUOTE(Xinael @ Oct 27 2007, 09:14 AM) 453754
And @Faymar: you're right, sort of, but remember that the expression "elixaff.aeon ~= nil" is going to return a boolean, which will then itself be checked against nil and false. That's the extra check.

I never realized that. Thank you for pointing it out.
Severian2007-10-27 16:47:32
I've decided to start out easy, and instead of jumping right into curing stuff, I'm going to make a debating script and a mob counter script(used for counting how many times I've killed any type of mob). My first question is about the mob script... I want to make it pop out in its own window so that it doesn't clutter my command screen... I don't know what I'm looking for in the functions list, but I haven't found what I want yet. Does anyone know off hand how to do this in Lua?

And perhaps a stupid question, when I'm scripting in mushclient, if I have lua selected as my language, and scripting enabled, do i need to declare lua in my script, or does it automatically do that?

ie



blah blah
blah blah



Thanks!
Severian2007-10-27 17:32:39
Well this is what I have so far. Is it better to use a table or an array to store values such as how many times you've killed a mob?

CODE
ArrayCreate "mobcounter"

function mobadd (name, lines, wildcard)
    if ArrayKeyExists ("mobcounter", "%1") == "false"
        then ArraySet ("mobcounter", "%1", "0")
            else ArraySet ("mobcounter", "%1", "


I left the end of my else statement open because I'm not sure how to grab the previous value of the array... I may stumble across the answer in the next couple of hours, but if anyone else can help me, that'd be great. I also don't know if I am using the if then else control operand correctly. The last thing I'm not sure about is the parameters to my function... (name, lines, wildcard) I wasn't sure if Nick Gammon was placing those in there to denote that I have to fill in the name, the lines and the wildcard (in which case I have no idea what to do) or if you just place, like I did, the words that he put in?

Thanks!
Daniel
Faymar2007-10-27 20:08:22
QUOTE(Severian @ Oct 27 2007, 08:32 PM) 453812
Well this is what I have so far. Is it better to use a table or an array to store values such as how many times you've killed a mob?

CODE
ArrayCreate "mobcounter"

function mobadd (name, lines, wildcard)
    if ArrayKeyExists ("mobcounter", "%1") == "false"
        then ArraySet ("mobcounter", "%1", "0")
            else ArraySet ("mobcounter", "%1", "


I left the end of my else statement open because I'm not sure how to grab the previous value of the array... I may stumble across the answer in the next couple of hours, but if anyone else can help me, that'd be great. I also don't know if I am using the if then else control operand correctly. The last thing I'm not sure about is the parameters to my function... (name, lines, wildcard) I wasn't sure if Nick Gammon was placing those in there to denote that I have to fill in the name, the lines and the wildcard (in which case I have no idea what to do) or if you just place, like I did, the words that he put in?

Thanks!
Daniel

How about this:
CODE
function mobadd (sName, sLine, tWildcards)
if (mobcounter]) then
mobcounter] = mobcounter] + 1
end -- if
end -- function
Severian2007-10-27 23:56:06
Could you explain what each part of that means? I'm afraid I'm a bit slow when it comes to programming, and I'm having troubles conceptualizing what each part means.
Xinael2007-10-28 03:26:40
The words "names, lines, wildcard" in the function definition are the names of local variables. For example, you could do this:

CODE
function SomeFunc (string1, string2)
  print(string1 .. string2)
end


Now, when you use this function, you do SomeFunc("Hello", "World") and the string1 variable points to the string "Hello" and the string2 variable points to the string "World". You can use those variables any way you like within the body of the function, but like I say, they're local to the function.

If it were me, I'd just do

CODE
function addMob (name, number)
  number = number or 1
  MobKills=MobKills + number
end


And then serialise the variable at disconnect so it's stored for the next session. If you don't understand this quite basic code, I really recommend reading PiL, which will explain this in some detail. All it does is add 1 or the number you specify to the item in the table with that mob's name.

Unless there's some element here you're not explaining, I don't think it needs to be more complex than that.
Severian2007-10-28 05:21:42
That's essentially it. Only other thing I'd want to do is output it into a separate window, or display it somehow outside of the main command window.
Unknown2007-10-31 21:54:13
I have read through this and many other posts and have come to the sad realization that I am in need of a lot of help.
I run CMUD, and have no clue how to use it. If someone would be willing to help me out, I would be more than willing to toss a few credits your way.
Thanks
MeghanO
Unknown2007-10-31 23:14:36
QUOTE(meghano @ Oct 31 2007, 09:54 PM) 454969
I have read through this and many other posts and have come to the sad realization that I am in need of a lot of help.
I run CMUD, and have no clue how to use it. If someone would be willing to help me out, I would be more than willing to toss a few credits your way.
Thanks
MeghanO

Building a comprehensive system for a complicated combat setup like Lusternia's is a pretty big task, but if you want someone to show you the basics about how to code in ZMud/CMud, I can show you how to use the necessary commands. Once you've got the basics down, the rest is really just looking up additional functions and/or commands in the documentation when you come up against unique problems.
Severian2007-11-01 01:57:17
Could always talk to Zarquan too. If you don't mind though could you make your own topic about it? Thanks!
Forren2007-11-01 02:07:20
QUOTE(meghano @ Oct 31 2007, 05:54 PM) 454969
I have read through this and many other posts and have come to the sad realization that I am in need of a lot of help.
I run CMUD, and have no clue how to use it. If someone would be willing to help me out, I would be more than willing to toss a few credits your way.
Thanks
MeghanO

I built a CMud system from scratch. Would be glad to help you. Contact me on AIM. Best way to reach me. Busy these next two days though - Halloween tonight and a big compsci project due Friday night.
Simimi2007-11-01 05:35:51
I do not know why the plugins stopped working on the forums, I asked in the thread for a mod to have a look but I didn't notice a reply... I can try to post it again and see if that will help?
Unknown2007-11-01 11:14:01
The plugins work just fine. It's the fact that trigger patterns in MUSHclient XML are not properly encoded for browsers to understand them, so you have to open the attachment and then to a File/Save As to get it on your own drive.
Unknown2007-11-03 18:04:19
QUOTE(Severian @ Oct 31 2007, 08:57 PM) 455024
Could always talk to Zarquan too. If you don't mind though could you make your own topic about it? Thanks!


I actually tried making a new topic, but I wasn't allowed... probably because I am brand new to the forums.
Xinael2007-11-03 18:08:34
Yeah, you need at least one post before you can make your own topics. Helps stop spambots.
Unknown2007-11-03 18:10:44
QUOTE(Harrow @ Oct 31 2007, 06:14 PM) 454991
Building a comprehensive system for a complicated combat setup like Lusternia's is a pretty big task, but if you want someone to show you the basics about how to code in ZMud/CMud, I can show you how to use the necessary commands. Once you've got the basics down, the rest is really just looking up additional functions and/or commands in the documentation when you come up against unique problems.


That would be a great help. My problem is that I know nothing at all about this kind of thing, so...

My AIM is: megortiz429