Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Mother LUA post (or just my question...)

KillyonKillyon Member Posts: 13
This worked in other games, and all I did was replace parts of it. I'm about...hrmm...kindergarten level in Lua, so -any- help would be appreciated:

PATTERN: ^e(w|ma|k|o|n|g|mi)$
----------------------------------------------------------------

local herbtable = {

w = "wormwood",

ma = "mandrake",

k = "kelp",

o = "orphine",

n = "nightshade",

g = "galingale",

mi = "maidenhair"

}


local herbvar = herbtable[matches[2]]


sendall("outr " ..herbvar.."","eat " ..herbvar.."",false)




Basically...I haven't looked for a mother Lua thread and really want help. I'm also buying @(anybody with a system)'s system for ~100 credits.............






Comments

  • IniarIniar Member Posts: 3,213 ✭✭✭✭✭
    edited June 2014
    If you're using Mudlet, you need to define what sendall is, as I don't think it's a native function. I suspect it's something like:

    function sendall(...)
    for k, v in pairs(arg) do
    send(v)
    end
    end

    Breaking it down:
    Killyon said:

    PATTERN: ^e(w|ma|k|o|n|g|mi)$ 

    The parentheses "(" and ")" capture data in-between unless specified otherwise notated by "?:", that is (?:dashing)
    These 'captures' are stored in a global table called matches, also accessed by _G.matches, unless it is a multiline trigger, then it is accessed by multimatches[line][index], e.g. multimatches[1][3] refers to the first line, and third 'capture'

    local herbtable = {

    w = "wormwood",

    ma = "mandrake",

    k = "kelp",

    o = "orphine",

    n = "nightshade",

    g = "galingale",

    mi = "maidenhair"

    }


    local denotes that this variable, in this case "herbtable" will be deleted after this script is run (not technically correct, but close enough to the truth). You won't be able to access it again after this script is run.


    local herbvar = herbtable[matches[2]]


    essentially, this line takes your 'capture', that is em or emi and checks herbtable for the value by doing herbtable[mi], which is "maidenhair". It then stores it in a local variable called "herbvar"


    sendall("outr " ..herbvar.."","eat " ..herbvar.."",false)


    this is a function called 'send all' that sends three arguments (or options) with it

    1. "outr "..herbvar

    2. "eat "..herbvar

    3. false


    A function is essentially a machine. Mudlet provides a few "machines" like send() and display(), but with Lua you can write your own machines, in this case "sendall()"

    With this script, we're asking Mudlet and Lua to run the machine "sendall()" but telling it to run with the three arguments specified above.

    The first argument ("outr "..herbvar) is a string, which is a type of variable. Like Mastercard and Visa are different brands of credit companies, string, table, number are also different "brands" of variable, because they let you do different things.

    The first argument is a string from the formation of two strings, "outr " and herbvarAs you remember, herbvar is "maidenhair" (when you input emi), so the first argument becomes "outr maidenhair". The ".." between "outr " and herbvar is known as the concatenation operator which literally joins two strings (or more).

    The second argument is also similar to the first; it is two strings ("eat " and herbvar) joined together.

    The third argument probably specifies some option that is built into "sendall()"


    function sendall(...)
    for k, v in pairs(arg) do
    send(v)
    end
    end

    Now looking at our function, the first line specifies that what we name next is a function:

    function sendall(...)

    sendall is the name of the function, and the parentheses "(" and ")" in this case denote potential arguments (or information) for the sendall() machine to work on.
    for example, I could do this:

    function sendall(myName,yourName)
    send("say "..myName.." thinks "..yourName.." is full of cheese")
    end

    Then run
    sendall("Iniar","Randall") -> say Iniar thinks Randall is full of cheese
    sendall("Killyon","Iniar") -> say Killyon thinks Iniar is full of cheese
    sendall("Jeremy","Tomato") -> say Jeremy thinks Tomato is full of cheese
    and so on and so forth

    and within the scope of the function, I could use the variables myName and yourName to do whatever I want.
    In this case, ... is what is known as a variable number of arguments and is accessed by using the table arg.

    Next we look at the for loop;

    for k, v in pairs(arg) do send(v) end

    Essentially, it means for every pair of words and meanings, or keys and values (k,v), within the table arg, do the following code;

    send(v)

    send() is Mudlet's internal machine that sends the argument to Imperian, such that in this case, v is sent for every entry in arg.
    Putting it all together,
    sendall("outr "..herbvar, "eat "..herbvar, false)
    means this,
    send every specified argument in the brackets as input I would execute myself.

    wit beyond measure is a Sidhe's greatest treasure
  • VaxVax Member Posts: 13
    Not that it makes Iniar's post any less helpful, but Mudlet did add a sendAll command (see here).
  • IniarIniar Member Posts: 3,213 ✭✭✭✭✭
    edited June 2014
    There are two lessons here;
    1. Case matters, that is sendall != sendAll
    2. We can now write sendAll as given by Mudlet;

    function sendall(...)

    local pass = arg[#arg] -- #arg gives us the last element of the table arg, and we store this to (1) check if it is a boolean, (2) manipulate further actions based on (1)

    local show = true -- default state

    if type(pass) == "boolean" and not pass then show = false end

    -- here type(pass) is checked to see if it is a true/false variable (boolean), if it is not, we do nothing, and show defaults to the default value of true.

    -- however, if type(pass) is a boolean, we next check if it is false because this is the only time it matters to us, therefore:

    --  not pass is evaluated to true only if pass is false

    -- therefore, in this case, if the last argument is a boolean and only if it is false, do we change our default value show

    for i=1,#arg do

    send(arg[i],show) -- send the code

    end

    end


    Final function:


    function sendall(...)

    local pass = arg[#arg]

    local show = true 

    if type(pass) == "boolean" and not pass then show = false end

    for i=1,#arg do

    send(arg[i],show)

    end

    end


    One could argue with server side separators, this could be more elegant; 


    sp = "|" -- naming my global separator


    function send_all(...)

    local str = ""

    for i=1,#arg do

    if type(arg[i]) == "string" then

    str = str..arg[i]..sp

    end

    end

    if type(arg[#arg]) == "boolean" then

    send(str,arg[#arg])

    else

    send(str)

    end

    end

    wit beyond measure is a Sidhe's greatest treasure
  • KillyonKillyon Member Posts: 13
    @Iniar much appreciated!! But that's like Japanese to me :( gives me something to translate and work on though! Again thank you!

    (read: I'll decipher when I'm sober :P)
  • RylaeisRylaeis Member Posts: 33 ✭✭
    edited June 2014
    Killyon said:
    This worked in other games, and all I did was replace parts of it. I'm about...hrmm...kindergarten level in Lua, so -any- help would be appreciated:

    PATTERN: ^e(w|ma|k|o|n|g|mi)$
    ----------------------------------------------------------------

    local herbtable = {

    ["w"] = "wormwood",

    ["ma"] = "mandrake",

    ["k"] = "kelp",

    ["o"] = "orphine",

    ["n"] = "nightshade",

    ["g"] = "galingale",

    ["mi"] = "maidenhair"

    }


    local herbvar = herbtable[matches[2]]


    sendall("outr " ..herbvar.."","eat " ..herbvar.."",false)



    A, it should be sendAll(whatever), as Vax said. Capitalisation DOES matter.
    Secondly, you have an extra " in it. Should be sendAll("outr "..herbvar, "eat "..herbvar, false)

    Alternatively, send("outr "..herbvar..";eat "..herbvar, false) [change ; to whatever your command separator is]
    (If that doesn't work, alter your table as done in the quote)
    image
  • KhizanKhizan Member Posts: 2,648 ✭✭✭✭✭

    In non-coding based technicalities, you don't need to outr plants anymore, here. You can just straight up "eat kelp".

    "On the battlefield I am a god. I love war. The steel, the smell, the corpses. I wish there were more. On the first day I drove the Northmen back alone at the ford. Alone! On the second I carried the bridge! Me! Yesterday I climbed the Heroes! I love war! I… I wish it wasn’t over."

  • MenochMenoch Member Posts: 594 ✭✭✭
    It's also going to be impossible for you with your level of coding proficiency to implement a clientside healing system faster or more reliable than autocuring, which is what I'm assuming this thing is for, somehow. A pretty lightweight set of toggles and the like for switching priorities on the fly is all that's required.
Sign In or Register to comment.