Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ General ➜ Lets start the conversion.

Lets start the conversion.

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  3  4  5  

Posted by David Berthiaume   (202 posts)  Bio
Date Sat 11 Dec 2004 02:55 PM (UTC)
Message
All right, I KNOW I'm going to have to switch to Lua eventually. The syntax is more or less the issue I'm going to have, being so used to VBscript.

The first thing I should switch over is my infobar script.

Quote:
<triggers>
<trigger
custom_colour="17"
enabled="y"
group="InfoBarScript"
keep_evaluating="y"
match="^\&lt;(.*)\%XTL\&gt;$"
omit_from_output="y"
regexp="y"
repeat="y"
send_to="12"
other_text_colour="darkcyan"
>
<send>world.setvariable "EXPTOLEVEL", "%1"
world.setvariable "XPNEW", "%1"
dim EXPTICK, TICK, TICKERNEW, TICKERBLANK, EXPTOLEVEL, EXPFROMLEVEL, count
dim XPOLD, XPNEW, XPDIFF
XPOLD = CInt(world.getvariable ("XPOLD"))
XPNEW = CInt(world.getvariable ("XPNEW"))
XPDIFF = (XPOLD - XPNEW)
if XPDIFF &lt; 0 then
XPDIFF = (XPDIFF + 100)
world.setvariable "XPOLD", XPNEW
world.setvariable "XPDIFF", XPDIFF
elseif XPDIFF &gt; 0 then
world.setvariable "XPOLD", XPNEW
world.setvariable "XPDIFF", XPDIFF
end if
EXPTOLEVEL = CInt(world.getvariable ("EXPTOLEVEL"))
EXPFROMLEVEL = (100 - EXPTOLEVEL)
world.setvariable "EXPFROMLEVEL", EXPFROMLEVEL
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""
for count = 1 to CInt(world.getvariable ("EXPFROMLEVEL"))
TICKERBLANK = (TICKERBLANK + TICKERNEW)
next
for count = 1 to CInt(world.getvariable ("EXPTOLEVEL"))
EXPTICK = (EXPTICK + TICK)
next
dim space
space = " "
world.infoclear
world.InfoBackground "black"
world.InfoFont "Arial", 12, 1
world.infocolour "white"
world.info "("
if CInt(world.getvariable ("aaHP")) &lt; 100 then
world.info space
world.infocolour "white"
world.info world.getvariable ("aaHP") &amp; "%"
else
world.infocolour "white"
world.info world.getvariable ("aaHP") &amp; "%"
end if
world.infocolour "red"
world.info "HP"
world.infocolour "white"
world.info "|"
if CInt(world.getvariable("aaMana")) &lt; 100 then
world.info space
world.info world.getvariable ("aaMana")
else
world.info world.getvariable ("aaMana")
end if
world.info "%"
world.infocolour "olive"
world.info "Mana"
world.infocolour "white"
world.info "|"
if CInt(world.getvariable ("aaMove")) &lt; 100 then
world.info space
world.info world.getvariable ("aaMove")
else
world.info world.getvariable ("aaMove")
end if
world.info "%"
world.infocolour "darkcyan"
world.info "Move"
world.infocolour "white"
world.info ") ("
world.infocolour "lightgreen"
world.info world.getvariable ("aaHunger")
world.infocolour "white"
world.info ") "
world.info "XTL: ["
if CInt(world.getvariable ("EXPTOLEVEL")) &lt; 5 then
world.InfoColour "red"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 10 then
world.InfoColour "lawngreen"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 20 then
world.InfoColour "green"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 30 then
world.InfoColour "darkorange"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 40 then
world.InfoColour "orange"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 50 then
world.InfoColour "goldenrod"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 60 then
world.InfoColour "gold"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 70 then
world.InfoColour "yellow"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 80 then
world.InfoColour "blue"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) &lt; 90 then
world.InfoColour "mediumblue"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
else
world.infocolour "darkblue"
world.InfoFont "Arial", 12, 0
world.info EXPTICK
end if
world.infocolour "dimgray"
world.info TICKERBLANK
world.InfoColour "white"

world.InfoFont "Arial", 12, 1
world.info "] ("
world.infocolour "yellow"
if CInt(world.getvariable ("EXPTOLEVEL")) &lt; 100 then
world.info space
world.info world.getvariable ("EXPTOLEVEL")
else
world.info world.getvariable ("EXPTOLEVEL")
end if
world.infocolour "white"
world.info "%) Last ("
world.infocolour "yellow"
world.info world.getvariable ("XPDIFF")
world.infocolour "white"
world.info "%) Level: "
world.infocolour "yellow"
if world.getvariable ("level2") = "subhero" then
world.info world.getvariable ("Level")
else
world.infocolour "red"
world.info world.getvariable ("level2") &amp; world.getvariable ("level")
end if

world.EnableGroup "SHighlight", 0</send>
</trigger>
<trigger
group="InfoBarScript"
ignore_case="y"
keep_evaluating="y"
match="^\&lt;(.*)\%HP\|(.*)\%Mana\|(.*)\%Move\&gt; \&lt;(.*)\&gt;$"
omit_from_output="y"
regexp="y"
repeat="y"
send_to="12"
sequence="1"
>
<send>world.setvariable "aaHP", "%1"
world.setvariable "aaMana", "%2"
world.setvariable "aaMove", "%3"
world.setvariable "aaHunger", "%4"
dim count, hunger
hunger= world.getvariable ("aaHunger")
if (hunger = "H") or (hunger = "T") or (hunger = "HT") then
for count = 1 to 2
world.sendimmediate "drink soup"
next
else
end if</send>
</trigger>
</triggers>
Top

Posted by David Berthiaume   (202 posts)  Bio
Date Reply #1 on Sat 11 Dec 2004 02:57 PM (UTC)
Message
As a side note, once I see the code for those Triggers, EVERYTHING else that I use I will be able to convert myself. Most of the code I use is involved in those 2 triggers. If I can lick those, I'll be able to do whatever it is I need to do.
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #2 on Sat 11 Dec 2004 07:01 PM (UTC)

Amended on Sat 11 Dec 2004 07:25 PM (UTC) by Nick Gammon

Message
Good idea. Let's do a "before" and "after". First thing I'll do is format with a bit of indenting, and take them out of the XML format, so we can see "<" as itself rather than &lt; (just to make it easier to read) ...

First trigger, reformatted in VBscript



world.setvariable "EXPTOLEVEL", "%1"
world.setvariable "XPNEW", "%1"
dim EXPTICK, TICK, TICKERNEW, TICKERBLANK, EXPTOLEVEL, EXPFROMLEVEL, count
dim XPOLD, XPNEW, XPDIFF
XPOLD = CInt(world.getvariable ("XPOLD"))
XPNEW = CInt(world.getvariable ("XPNEW"))
XPDIFF = (XPOLD - XPNEW)

if XPDIFF < 0 then
  XPDIFF = (XPDIFF + 100)
  world.setvariable "XPOLD", XPNEW
  world.setvariable "XPDIFF", XPDIFF
elseif XPDIFF > 0 then
  world.setvariable "XPOLD", XPNEW
  world.setvariable "XPDIFF", XPDIFF
end if

EXPTOLEVEL = CInt(world.getvariable ("EXPTOLEVEL"))
EXPFROMLEVEL = (100 - EXPTOLEVEL)
world.setvariable "EXPFROMLEVEL", EXPFROMLEVEL
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""

for count = 1 to CInt(world.getvariable ("EXPFROMLEVEL"))
  TICKERBLANK = (TICKERBLANK + TICKERNEW)
next

for count = 1 to CInt(world.getvariable ("EXPTOLEVEL"))
  EXPTICK = (EXPTICK + TICK)
next

dim space
space = "  "
world.infoclear
world.InfoBackground "black"
world.InfoFont "Arial", 12, 1
world.infocolour "white"
world.info "("

if CInt(world.getvariable ("aaHP")) < 100 then
  world.info space
  world.infocolour "white"
  world.info world.getvariable ("aaHP") & "%"
else
  world.infocolour "white"
  world.info world.getvariable ("aaHP") & "%"
end if

world.infocolour "red"
world.info "HP"
world.infocolour "white"
world.info "|"

if CInt(world.getvariable("aaMana")) < 100 then
  world.info space
  world.info world.getvariable ("aaMana")
else
  world.info world.getvariable ("aaMana")
end if

world.info "%"
world.infocolour "olive"
world.info "Mana"
world.infocolour "white"
world.info "|" 

if CInt(world.getvariable ("aaMove")) < 100 then
  world.info space
  world.info world.getvariable ("aaMove")
else
  world.info world.getvariable ("aaMove")
end if

world.info "%"
world.infocolour "darkcyan"
world.info "Move"
world.infocolour "white"
world.info ") ("
world.infocolour "lightgreen"
world.info world.getvariable ("aaHunger")
world.infocolour "white"
world.info ") "
world.info "XTL: ["

if CInt(world.getvariable ("EXPTOLEVEL")) < 5 then
  world.InfoColour "red"
  world.InfoFont "Arial", 12, 0
  world.info  EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 10 then
  world.InfoColour "lawngreen"
  world.InfoFont "Arial", 12, 0
  world.info  EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 20 then
  world.InfoColour "green"
  world.InfoFont "Arial", 12, 0
  world.info  EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 30 then
  world.InfoColour "darkorange"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 40 then
  world.InfoColour "orange"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 50 then
  world.InfoColour "goldenrod"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 60 then
  world.InfoColour "gold"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 70 then
  world.InfoColour "yellow"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 80 then
  world.InfoColour "blue"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
elseif CInt(world.getvariable ("EXPTOLEVEL")) < 90 then
  world.InfoColour "mediumblue"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
else
  world.infocolour "darkblue"
  world.InfoFont "Arial", 12, 0
  world.info EXPTICK
end if

world.infocolour "dimgray"
world.info TICKERBLANK
world.InfoColour "white"

world.InfoFont "Arial", 12, 1
world.info "] (" 
world.infocolour "yellow"  

if CInt(world.getvariable ("EXPTOLEVEL")) < 100 then
  world.info space
  world.info world.getvariable ("EXPTOLEVEL")
else
  world.info world.getvariable ("EXPTOLEVEL")
end if

world.infocolour "white"
world.info "%)   Last (" 
world.infocolour "yellow"
world.info world.getvariable ("XPDIFF")
world.infocolour "white"
world.info "%)   Level: "
world.infocolour "yellow"

if world.getvariable ("level2") = "subhero" then
  world.info world.getvariable ("Level")
else
  world.infocolour "red"
  world.info world.getvariable ("level2") & world.getvariable ("level")
end if

world.EnableGroup "SHighlight", 0

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 11 Dec 2004 07:25 PM (UTC)

Amended on Sat 11 Dec 2004 09:33 PM (UTC) by Nick Gammon

Message
The next thing I want to do is get rid of the "world." prefix, which is not necessary in VBscript or Lua, and just causes extra clutter and typing, and more importantly, fix the capitalisation of your script commands.

Whereas VBscript is not case-sensitive, Lua is. So things like "world.note" will not work, it must be "world.Note".

To save myself a certain amount of tedium, and possible errors, I wrote a small Lua script to do that for me ...

Script function capitalisation fixer


function fixscript (s)

  fixed = string.gsub (s, "world%.([%a%u]+)", function (w)
    for v in world do
      if string.upper (v) == string.upper (w) then
         return v;
      end -- if
    end -- for
    return w
  end)	-- of gsub function

  print (fixed)

end -- fixscript 



What this does is a single call of string.gsub (global substitute) looking for a word consisting of upper or lower-case letters. When found it calls the anonymous function passing down the word.

The function scans all known functions (in the world table) and tries to match the word in your script, converted to upper case, to the known function, also converted to upper case. If a match is found it returns the correctly capitalised word. If not (ie. it is one of your script variables) it simply returns the same word.

To use this I call it, passing the entire script as a multi-line string. The results appear in the output window, where I can copy and paste them back into the script file.

The double brackets are important before and after, they indicate a multi-line quoted string.

ie.


fixscript [[
... your script here, copied and pasted ...
]]


VBscript with "world." removed and functions correctly capitalised



SetVariable "EXPTOLEVEL", "%1"
SetVariable "XPNEW", "%1"
dim EXPTICK, TICK, TICKERNEW, TICKERBLANK, EXPTOLEVEL, EXPFROMLEVEL, count
dim XPOLD, XPNEW, XPDIFF
XPOLD = CInt(GetVariable ("XPOLD"))
XPNEW = CInt(GetVariable ("XPNEW"))
XPDIFF = (XPOLD - XPNEW)

if XPDIFF < 0 then
  XPDIFF = (XPDIFF + 100)
  SetVariable "XPOLD", XPNEW
  SetVariable "XPDIFF", XPDIFF
elseif XPDIFF > 0 then
  SetVariable "XPOLD", XPNEW
  SetVariable "XPDIFF", XPDIFF
end if

EXPTOLEVEL = CInt(GetVariable ("EXPTOLEVEL"))
EXPFROMLEVEL = (100 - EXPTOLEVEL)
SetVariable "EXPFROMLEVEL", EXPFROMLEVEL
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""

for count = 1 to CInt(GetVariable ("EXPFROMLEVEL"))
  TICKERBLANK = (TICKERBLANK + TICKERNEW)
next

for count = 1 to CInt(GetVariable ("EXPTOLEVEL"))
  EXPTICK = (EXPTICK + TICK)
next

dim space
space = "  "
InfoClear
InfoBackground "black"
InfoFont "Arial", 12, 1
InfoColour "white"
Info "("

if CInt(GetVariable ("aaHP")) < 100 then
  Info space
  InfoColour "white"
  Info GetVariable ("aaHP") & "%"
else
  InfoColour "white"
  Info GetVariable ("aaHP") & "%"
end if

InfoColour "red"
Info "HP"
InfoColour "white"
Info "|"

if CInt(GetVariable("aaMana")) < 100 then
  Info space
  Info GetVariable ("aaMana")
else
  Info GetVariable ("aaMana")
end if

Info "%"
InfoColour "olive"
Info "Mana"
InfoColour "white"
Info "|" 

if CInt(GetVariable ("aaMove")) < 100 then
  Info space
  Info GetVariable ("aaMove")
else
  Info GetVariable ("aaMove")
end if

Info "%"
InfoColour "darkcyan"
Info "Move"
InfoColour "white"
Info ") ("
InfoColour "lightgreen"
Info GetVariable ("aaHunger")
InfoColour "white"
Info ") "
Info "XTL: ["

if CInt(GetVariable ("EXPTOLEVEL")) < 5 then
  InfoColour "red"
  InfoFont "Arial", 12, 0
  Info  EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 10 then
  InfoColour "lawngreen"
  InfoFont "Arial", 12, 0
  Info  EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 20 then
  InfoColour "green"
  InfoFont "Arial", 12, 0
  Info  EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 30 then
  InfoColour "darkorange"
  InfoFont "Arial", 12, 0
  Info EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 40 then
  InfoColour "orange"
  InfoFont "Arial", 12, 0
  Info EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 50 then
  InfoColour "goldenrod"
  InfoFont "Arial", 12, 0
  Info EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 60 then
  InfoColour "gold"
  InfoFont "Arial", 12, 0
  Info EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 70 then
  InfoColour "yellow"
  InfoFont "Arial", 12, 0
  Info EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 80 then
  InfoColour "blue"
  InfoFont "Arial", 12, 0
  Info EXPTICK
elseif CInt(GetVariable ("EXPTOLEVEL")) < 90 then
  InfoColour "mediumblue"
  InfoFont "Arial", 12, 0
  Info EXPTICK
else
  InfoColour "darkblue"
  InfoFont "Arial", 12, 0
  Info EXPTICK
end if

InfoColour "dimgray"
Info TICKERBLANK
InfoColour "white"

InfoFont "Arial", 12, 1
Info "] (" 
InfoColour "yellow"  

if CInt(GetVariable ("EXPTOLEVEL")) < 100 then
  Info space
  Info GetVariable ("EXPTOLEVEL")
else
  Info GetVariable ("EXPTOLEVEL")
end if

InfoColour "white"
Info "%)   Last (" 
InfoColour "yellow"
Info GetVariable ("XPDIFF")
InfoColour "white"
Info "%)   Level: "
InfoColour "yellow"

if GetVariable ("level2") = "subhero" then
  Info GetVariable ("Level")
else
  InfoColour "red"
  Info GetVariable ("level2") & GetVariable ("level")
end if

EnableGroup "SHighlight", 0

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 11 Dec 2004 07:55 PM (UTC)

Amended on Sun 12 Dec 2004 05:11 AM (UTC) by Nick Gammon

Message
Syntax changes

Now, time to start doing it in Lua. The major things to change now are:


  • Function calls need to be in parenthese (like Jscript), so that:


    SetVariable "XPOLD", XPNEW


    becomes


    SetVariable ("XPOLD", XPNEW)


    I did a regular-expression to do that in my editor, and then highlighted the individual lines (or batches of lines).

  • You don't declare variables in Lua, so the "dim" lines went away.

  • CInt (convert to integer) becomes "tonumber", which does the same thing. A quick find-and-replace fixed that.

  • "end if" is simply "end". I did a find-and-replace to make it "end -- if" - the hyphens are a comment, and the comment makes it more obvious what we are ending.

  • For loops have a slightly different syntax.

    I changed, for example:


    for count = 1 to tonumber (GetVariable ("EXPFROMLEVEL"))
    ...
    next


    to


    for count = 1, tonumber (GetVariable ("EXPFROMLEVEL")) do
    ...
    end -- for


  • String concatenation is done with ".." rather than "&"

    eg..


    Info (GetVariable ("aaHP") & "%")


    becomes:


    Info (GetVariable ("aaHP") .. "%")


  • The equality test is "==" not "=" (like in JScript or C) so for example:


    if GetVariable ("level2") = "subhero" then


    becomes:


    if GetVariable ("level2") == "subhero" then




Lua version - compiles without errors ...



SetVariable ("EXPTOLEVEL", "%1")
SetVariable ("XPNEW", "%1")
XPOLD = tonumber (GetVariable ("XPOLD")) 
XPNEW = tonumber (GetVariable ("XPNEW")) 
XPDIFF = (XPOLD - XPNEW)

if XPDIFF < 0 then
  XPDIFF = (XPDIFF + 100)
  SetVariable ("XPOLD", XPNEW)
  SetVariable ("XPDIFF", XPDIFF)
elseif XPDIFF > 0 then
  SetVariable ("XPOLD", XPNEW)
  SetVariable ("XPDIFF", XPDIFF)
end -- if

EXPTOLEVEL = tonumber (GetVariable ("EXPTOLEVEL"))
EXPFROMLEVEL = (100 - EXPTOLEVEL)
SetVariable ("EXPFROMLEVEL", EXPFROMLEVEL)
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""

for count = 1, tonumber (GetVariable ("EXPFROMLEVEL")) do
  TICKERBLANK = (TICKERBLANK + TICKERNEW)
end -- for

for count = 1, tonumber (GetVariable ("EXPTOLEVEL")) do
  EXPTICK = (EXPTICK + TICK)
end -- for

space = "  "
InfoClear ()
InfoBackground ("black")
InfoFont ("Arial", 12, 1)
InfoColour ("white")
Info ("(")

if tonumber (GetVariable ("aaHP")) < 100 then
  Info (space)
  InfoColour ("white")
  Info (GetVariable ("aaHP") .. "%")
else
  InfoColour ("white")
  Info (GetVariable ("aaHP") .. "%")
end -- if

InfoColour ("red")
Info ("HP")
InfoColour ("white")
Info ("|")

if tonumber (GetVariable("aaMana")) < 100 then
  Info (space)
  Info (GetVariable ("aaMana"))
else
  Info (GetVariable ("aaMana"))
end -- if

Info ("%")
InfoColour ("olive")
Info ("Mana")
InfoColour ("white")
Info ("|" )

if tonumber (GetVariable ("aaMove")) < 100 then
  Info (space)
  Info (GetVariable ("aaMove"))
else
  Info (GetVariable ("aaMove"))
end -- if

Info ("%")
InfoColour ("darkcyan")
Info ("Move")
InfoColour ("white")
Info (") (")
InfoColour ("lightgreen")
Info (GetVariable ("aaHunger"))
InfoColour ("white")
Info (") ")
Info ("XTL: [")

if tonumber (GetVariable ("EXPTOLEVEL")) < 5 then
  InfoColour ("red")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 10 then
  InfoColour ("lawngreen")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 20 then
  InfoColour ("green")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 30 then
  InfoColour ("darkorange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 40 then
  InfoColour ("orange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 50 then
  InfoColour ("goldenrod")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 60 then
  InfoColour ("gold")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 70 then
  InfoColour ("yellow")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 80 then
  InfoColour ("blue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 90 then
  InfoColour ("mediumblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
else
  InfoColour ("darkblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
end -- if

InfoColour ("dimgray")
Info (TICKERBLANK)
InfoColour ("white")

InfoFont ("Arial", 12, 1)
Info ("] (" )
InfoColour ("yellow"  )

if tonumber (GetVariable ("EXPTOLEVEL")) < 100 then
  Info (space)
  Info (GetVariable ("EXPTOLEVEL"))
else
  Info (GetVariable ("EXPTOLEVEL"))
end -- if

InfoColour ("white")
Info ("%)   Last (" )
InfoColour ("yellow")
Info (GetVariable ("XPDIFF"))
InfoColour ("white")
Info ("%)   Level: ")
InfoColour ("yellow")

if GetVariable ("level2") == "subhero" then
  Info (GetVariable ("Level"))
else
  InfoColour ("red")
  Info (GetVariable ("level2") .. GetVariable ("level"))
end -- if

EnableGroup ("SHighlight", 0)



Note, there are still run-time problems, see the next post.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #5 on Sat 11 Dec 2004 08:13 PM (UTC)

Amended on Sat 11 Dec 2004 09:07 PM (UTC) by Nick Gammon

Message
Run-time changes

Having changed the above I now got runtime errors. The distinction is that Lua first does a syntax check, and then if that is OK, it actually attempts to run the code.

Things like bad arithmetic only show up at runtime, as Lua has untyped variables. For example:


b = "x"
a = b + 2


This is only a runtime error, as it doesn't know at compile time that you will attempt to add 2 to a string.



  • The first errors I got were "attempting to do arithmetic on a nil value", caused by the lines:


    XPOLD = tonumber (GetVariable ("XPOLD"))
    XPNEW = tonumber (GetVariable ("XPNEW"))


    To work around that, I used a Lua trick of using the short-circuit evaluation of an "or" ...


    XPOLD = tonumber (GetVariable ("XPOLD")) or 0
    XPNEW = tonumber (GetVariable ("XPNEW")) or 0


    What this does is take the variable, however if it is not present (ie. nil) then it substitutes zero.

  • I got an error on this line:


    EXPTICK = (EXPTICK + TICK)


    Again, for string concatenation you need ".." and not "+".


    EXPTICK = (EXPTICK .. TICK)


  • I had a few problems with "doing arithmetic on nil values", principally caused by missing variables. This might not be an issue for you, if the variables exist in advance, but a workaround is something like this:

    Change:


    if tonumber (GetVariable ("aaHP")) < 100 then


    to:


    if tonumber (GetVariable ("aaHP")) or 0 < 100 then


    Again, this substitutes zero if the variable doesn't exist.

  • Similarly, near the bottom:

    Change:


    Info (GetVariable ("level2") .. GetVariable ("level"))


    to:


    Info ((GetVariable ("level2") or "") .. (GetVariable ("level") or ""))




Below is the script converted to Lua, which compiles and runs OK (I'm not sure about all the figures, I think I need some more variables for it to work perfectly) ...

Lua version - compiles and runs ...



SetVariable ("EXPTOLEVEL", "%1")
SetVariable ("XPNEW", "%1")
XPOLD = tonumber (GetVariable ("XPOLD")) or 0
XPNEW = tonumber (GetVariable ("XPNEW")) or 0
XPDIFF = (XPOLD - XPNEW)

if XPDIFF < 0 then
  XPDIFF = (XPDIFF + 100)
  SetVariable ("XPOLD", XPNEW)
  SetVariable ("XPDIFF", XPDIFF)
elseif XPDIFF > 0 then
  SetVariable ("XPOLD", XPNEW)
  SetVariable ("XPDIFF", XPDIFF)
end -- if

EXPTOLEVEL = tonumber (GetVariable ("EXPTOLEVEL"))
EXPFROMLEVEL = (100 - EXPTOLEVEL)
SetVariable ("EXPFROMLEVEL", EXPFROMLEVEL)
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""

for count = 1, tonumber (GetVariable ("EXPFROMLEVEL")) do
  TICKERBLANK = (TICKERBLANK .. TICKERNEW)
end -- for

for count = 1, tonumber (GetVariable ("EXPTOLEVEL")) do
  EXPTICK = (EXPTICK .. TICK)
end -- for

space = "  "
InfoClear ()
InfoBackground ("black")
InfoFont ("Arial", 12, 1)
InfoColour ("white")
Info ("(")

if tonumber (GetVariable ("aaHP")) or 0 < 100 then
  Info (space)
  InfoColour ("white")
  Info (GetVariable ("aaHP") or "" .. "%")
else
  InfoColour ("white")
  Info (GetVariable ("aaHP") or "" .. "%")
end -- if

InfoColour ("red")
Info ("HP")
InfoColour ("white")
Info ("|")

if tonumber (GetVariable("aaMana")) or 0 < 100 then
  Info (space)
  Info (GetVariable ("aaMana"))
else
  Info (GetVariable ("aaMana"))
end -- if

Info ("%")
InfoColour ("olive")
Info ("Mana")
InfoColour ("white")
Info ("|" )

if tonumber (GetVariable ("aaMove")) or 0 < 100 then
  Info (space)
  Info (GetVariable ("aaMove"))
else
  Info (GetVariable ("aaMove"))
end -- if

Info ("%")
InfoColour ("darkcyan")
Info ("Move")
InfoColour ("white")
Info (") (")
InfoColour ("lightgreen")
Info (GetVariable ("aaHunger"))
InfoColour ("white")
Info (") ")
Info ("XTL: [")

if tonumber (GetVariable ("EXPTOLEVEL")) < 5 then
  InfoColour ("red")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 10 then
  InfoColour ("lawngreen")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 20 then
  InfoColour ("green")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 30 then
  InfoColour ("darkorange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 40 then
  InfoColour ("orange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 50 then
  InfoColour ("goldenrod")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 60 then
  InfoColour ("gold")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 70 then
  InfoColour ("yellow")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 80 then
  InfoColour ("blue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (GetVariable ("EXPTOLEVEL")) < 90 then
  InfoColour ("mediumblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
else
  InfoColour ("darkblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
end -- if

InfoColour ("dimgray")
Info (TICKERBLANK)
InfoColour ("white")

InfoFont ("Arial", 12, 1)
Info ("] (" )
InfoColour ("yellow"  )

if tonumber (GetVariable ("EXPTOLEVEL")) < 100 then
  Info (space)
  Info (GetVariable ("EXPTOLEVEL"))
else
  Info (GetVariable ("EXPTOLEVEL"))
end -- if

InfoColour ("white")
Info ("%)   Last (" )
InfoColour ("yellow")
Info (GetVariable ("XPDIFF"))
InfoColour ("white")
Info ("%)   Level: ")
InfoColour ("yellow")

if GetVariable ("level2") == "subhero" then
  Info (GetVariable ("Level"))
else
  InfoColour ("red")
  Info ((GetVariable ("level2") or "") .. (GetVariable ("level") or ""))
end -- if

EnableGroup ("SHighlight", 0)


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #6 on Sat 11 Dec 2004 08:50 PM (UTC)

Amended on Sat 11 Dec 2004 09:08 PM (UTC) by Nick Gammon

Message
Now let's see if we can neaten it all up a bit. First, there is a lot of GetVariable/SetVariable stuff in it.

Based on ideas in my post:


http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=4904


... let's change all GetVariable/SetVariable to use the var idea ...



var.EXPTOLEVEL = "%1"
var.XPNEW = "%1"
XPOLD = tonumber (var.XPOLD) or 0
XPNEW = tonumber (var.XPNEW) or 0
XPDIFF = (XPOLD - XPNEW)

if XPDIFF < 0 then
  XPDIFF = (XPDIFF + 100)
  var.XPOLD = XPNEW
  var.XPDIFF = XPDIFF
elseif XPDIFF > 0 then
  var.XPOLD = XPNEW
  var.XPDIFF = XPDIFF
end -- if

EXPTOLEVEL = tonumber (var.EXPTOLEVEL)
EXPFROMLEVEL = (100 - EXPTOLEVEL)
var.EXPFROMLEVEL = EXPFROMLEVEL
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""

for count = 1, tonumber (var.EXPFROMLEVEL) do
  TICKERBLANK = (TICKERBLANK .. TICKERNEW)
end -- for

for count = 1, tonumber (var.EXPTOLEVEL) do
  EXPTICK = (EXPTICK .. TICK)
end -- for

space = "  "
InfoClear ()
InfoBackground ("black")
InfoFont ("Arial", 12, 1)
InfoColour ("white")
Info ("(")

if tonumber (var.aaHP) or 0 < 100 then
  Info (space)
  InfoColour ("white")
  Info (var.aaHP or "" .. "%")
else
  InfoColour ("white")
  Info (var.aaHP or "" .. "%")
end -- if

InfoColour ("red")
Info ("HP")
InfoColour ("white")
Info ("|")

if tonumber (var.aaMana) or 0 < 100 then
  Info (space)
  Info (var.aaMana)
else
  Info (var.aaMana)
end -- if

Info ("%")
InfoColour ("olive")
Info ("Mana")
InfoColour ("white")
Info ("|" )

if tonumber (var.aaMove) or 0 < 100 then
  Info (space)
  Info (var.aaMove)
else
  Info (var.aaMove)
end -- if

Info ("%")
InfoColour ("darkcyan")
Info ("Move")
InfoColour ("white")
Info (") (")
InfoColour ("lightgreen")
Info (var.aaHunger)
InfoColour ("white")
Info (") ")
Info ("XTL: [")

if tonumber (var.EXPTOLEVEL) < 5 then
  InfoColour ("red")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 10 then
  InfoColour ("lawngreen")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 20 then
  InfoColour ("green")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 30 then
  InfoColour ("darkorange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 40 then
  InfoColour ("orange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 50 then
  InfoColour ("goldenrod")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 60 then
  InfoColour ("gold")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 70 then
  InfoColour ("yellow")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 80 then
  InfoColour ("blue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif tonumber (var.EXPTOLEVEL) < 90 then
  InfoColour ("mediumblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
else
  InfoColour ("darkblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
end -- if

InfoColour ("dimgray")
Info (TICKERBLANK)
InfoColour ("white")

InfoFont ("Arial", 12, 1)
Info ("] (" )
InfoColour ("yellow"  )

if tonumber (var.EXPTOLEVEL) < 100 then
  Info (space)
  Info (var.EXPTOLEVEL)
else
  Info (var.EXPTOLEVEL)
end -- if

InfoColour ("white")
Info ("%)   Last (" )
InfoColour ("yellow")
Info (var.XPDIFF)
InfoColour ("white")
Info ("%)   Level: ")
InfoColour ("yellow")

if var.level2 == "subhero" then
  Info (var.Level)
else
  InfoColour ("red")
  Info ((var.level2 or "") .. (var.level or ""))
end -- if

EnableGroup ("SHighlight", 0)



This looks a bit less cluttered. It relies upon this being in your main script file:


var = {}	-- variables table

-- warning - parenthesis must be on same line as setmetatable

setmetatable (var, 
 { 
 -- called to access an entry
 __index = 
 function (t, name) 
  return GetVariable (name) 
 end;
 
 -- called to change or delete an entry
 __newindex = 
 function (t, name, val) 
 local result
   if val == nil then	-- nil deletes it
     result = DeleteVariable (name)
   else
     result = SetVariable (name, tostring (val)) 
   end 
   -- warn if they are using bad variable names
   if result == error_code.eInvalidObjectLabel then
     error ("Bad variable name '" .. name .. "'")
   end
 end;
 })

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #7 on Sat 11 Dec 2004 09:05 PM (UTC)

Amended on Sat 11 Dec 2004 09:09 PM (UTC) by Nick Gammon

Message
We still have a lot of "tonumber" in it. Perhaps a rejig of the "var" table will eliminate them?

Let's give ourselves three pseudo-tables.


  • var - for straight variables of any type

  • nvar - for numeric variables.

    Getting a variable from this table will automatically convert it to a number, and substitute zero if not there.

  • svar - for string variables.

    Getting a variable from this table will automatically substitute an empty string if the variable is not there.



Here is the code for the new tables (put that in your global script file) ...


var = {}	-- variables table
nvar = {}	-- number variables table
svar = {}	-- string variables table

-- function called to change variables
function changevar (t, name, val)
local result
 if val == nil then	-- nil deletes it
   result = DeleteVariable (name)
 else
   result = SetVariable (name, tostring (val)) 
 end 
 -- warn if they are using bad variable names
 if result == error_code.eInvalidObjectLabel then
   error ("Bad variable name '" .. name .. "'")
 end
end  -- function changevar 

setmetatable (var, 
 { 
 __index = 
 function (t, name) 
  return GetVariable (name) 
 end;

 __newindex = changevar; 
 }
)

setmetatable (nvar, 
 { 
 __index = 
 function (t, name) 
   return tonumber (GetVariable (name)) or 0
 end;

 __newindex = changevar; 
 }
)

setmetatable (svar, 
 { 
 __index = 
 function (t, name) 
   return GetVariable (name) or ""
 end;

 __newindex = changevar; 
 }
)




Now we can get rid of all the "tonumber" references in the script, and the "or 0" stuff which was in case the variable did not exist ...

Lua script using nvar and svar for variable access



var.EXPTOLEVEL = "%1"
var.XPNEW = "%1"
XPOLD = nvar.XPOLD 
XPNEW = nvar.XPNEW 
XPDIFF = (XPOLD - XPNEW)

if XPDIFF < 0 then
  XPDIFF = (XPDIFF + 100)
  var.XPOLD = XPNEW
  var.XPDIFF = XPDIFF
elseif XPDIFF > 0 then
  var.XPOLD = XPNEW
  var.XPDIFF = XPDIFF
end -- if

EXPTOLEVEL = nvar.EXPTOLEVEL
EXPFROMLEVEL = (100 - EXPTOLEVEL)
var.EXPFROMLEVEL = EXPFROMLEVEL
TICKERNEW = "|"
TICKERBLANK = ""
TICK = "|"
EXPTICK = ""

for count = 1, nvar.EXPFROMLEVEL do
  TICKERBLANK = (TICKERBLANK .. TICKERNEW)
end -- for

for count = 1, nvar.EXPTOLEVEL do
  EXPTICK = (EXPTICK .. TICK)
end -- for

space = "  "
InfoClear ()
InfoBackground ("black")
InfoFont ("Arial", 12, 1)
InfoColour ("white")
Info ("(")

if nvar.aaHP < 100 then
  Info (space)
  InfoColour ("white")
  Info (svar.aaHP .. "%")
else
  InfoColour ("white")
  Info (svar.aaHP .. "%")
end -- if

InfoColour ("red")
Info ("HP")
InfoColour ("white")
Info ("|")

if nvar.aaMana < 100 then
  Info (space)
  Info (svar.aaMana)
else
  Info (svar.aaMana)
end -- if

Info ("%")
InfoColour ("olive")
Info ("Mana")
InfoColour ("white")
Info ("|" )

if nvar.aaMove < 100 then
  Info (space)
  Info (svar.aaMove)
else
  Info (svar.aaMove)
end -- if

Info ("%")
InfoColour ("darkcyan")
Info ("Move")
InfoColour ("white")
Info (") (")
InfoColour ("lightgreen")
Info (svar.aaHunger)
InfoColour ("white")
Info (") ")
Info ("XTL: [")

if nvar.EXPTOLEVEL < 5 then
  InfoColour ("red")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif nvar.EXPTOLEVEL < 10 then
  InfoColour ("lawngreen")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif nvar.EXPTOLEVEL < 20 then
  InfoColour ("green")
  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)
elseif nvar.EXPTOLEVEL < 30 then
  InfoColour ("darkorange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif nvar.EXPTOLEVEL < 40 then
  InfoColour ("orange")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif nvar.EXPTOLEVEL < 50 then
  InfoColour ("goldenrod")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif nvar.EXPTOLEVEL < 60 then
  InfoColour ("gold")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif nvar.EXPTOLEVEL < 70 then
  InfoColour ("yellow")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif nvar.EXPTOLEVEL < 80 then
  InfoColour ("blue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
elseif nvar.EXPTOLEVEL < 90 then
  InfoColour ("mediumblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
else
  InfoColour ("darkblue")
  InfoFont ("Arial", 12, 0)
  Info (EXPTICK)
end -- if

InfoColour ("dimgray")
Info (TICKERBLANK)
InfoColour ("white")

InfoFont ("Arial", 12, 1)
Info ("] (" )
InfoColour ("yellow"  )

if nvar.EXPTOLEVEL < 100 then
  Info (space)
  Info (svar.EXPTOLEVEL)
else
  Info (svar.EXPTOLEVEL)
end -- if

InfoColour ("white")
Info ("%)   Last (" )
InfoColour ("yellow")
Info (svar.XPDIFF)
InfoColour ("white")
Info ("%)   Level: ")
InfoColour ("yellow")

if svar.level2 == "subhero" then
  Info (svar.Level)
else
  InfoColour ("red")
  Info (svar.level2 .. svar.level)
end -- if

EnableGroup ("SHighlight", 0)

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #8 on Sat 11 Dec 2004 09:27 PM (UTC)
Message
Finally let's do a bit more tidying up. One thing you do a lot is use InfoColour followed by Info. We can make a local function to do that (infoct), thus saving typing. Also we can use string.rep to make a repeated string (for your bars).

Final Lua version


do

  local function infoct (c, t)
    assert (c and t, "Info Colour and text must be specified")
    InfoColour (c)
    Info (t)
  end -- function infoct

  var.EXPTOLEVEL = "%1"
  var.XPNEW = "%1"
  XPOLD = nvar.XPOLD 
  XPNEW = nvar.XPNEW 
  XPDIFF = (XPOLD - XPNEW)

  if XPDIFF < 0 then
    XPDIFF = (XPDIFF + 100)
    var.XPOLD = XPNEW
    var.XPDIFF = XPDIFF
  elseif XPDIFF > 0 then
    var.XPOLD = XPNEW
    var.XPDIFF = XPDIFF
  end -- if

  EXPTOLEVEL = nvar.EXPTOLEVEL
  EXPFROMLEVEL = (100 - EXPTOLEVEL)
  var.EXPFROMLEVEL = EXPFROMLEVEL
  TICKERNEW = "|"
  TICKERBLANK = ""
  TICK = "|"
  EXPTICK = ""


  TICKERBLANK = string.rep (TICKERNEW, nvar.EXPFROMLEVEL)
  EXPTICK = string.rep (TICK, nvar.EXPTOLEVEL)

  space = "  "
  InfoClear ()
  InfoBackground ("black")
  InfoFont ("Arial", 12, 1)

  -- ---------- HP ---------------

  infoct ("white", "(")

  if nvar.aaHP < 100 then
    Info (space)
  end -- if

  Info (svar.aaHP .. "%")

  infoct ("red", "HP")
  infoct ("white", "|")

  -- ---------- Mana ---------------

  if nvar.aaMana < 100 then
    Info (space)
  end -- if

  Info (svar.aaMana .. "%")

  infoct ("olive", "Mana")
  infoct ("white", "|")

  -- ---------- Move ---------------

  if nvar.aaMove < 100 then
    Info (space)
  end -- if

  Info (svar.aaMove .. "%")

  infoct ("darkcyan", "Move")

  infoct ("white", ") (")
  infoct ("lightgreen", svar.aaHunger)

  -- ---------- XTL ---------------

  infoct ("white", ") XTL: [")

  if nvar.EXPTOLEVEL < 5 then
    InfoColour ("red")
  elseif nvar.EXPTOLEVEL < 10 then
    InfoColour ("lawngreen")
  elseif nvar.EXPTOLEVEL < 20 then
    InfoColour ("green")
  elseif nvar.EXPTOLEVEL < 30 then
    InfoColour ("darkorange")
  elseif nvar.EXPTOLEVEL < 40 then
    InfoColour ("orange")
  elseif nvar.EXPTOLEVEL < 50 then
    InfoColour ("goldenrod")
  elseif nvar.EXPTOLEVEL < 60 then
    InfoColour ("gold")
  elseif nvar.EXPTOLEVEL < 70 then
    InfoColour ("yellow")
  elseif nvar.EXPTOLEVEL < 80 then
    InfoColour ("blue")
  elseif nvar.EXPTOLEVEL < 90 then
    InfoColour ("mediumblue")
  else
    InfoColour ("darkblue")
  end -- if

  InfoFont ("Arial", 12, 0)
  Info ( EXPTICK)

  infoct ("dimgray", TICKERBLANK)
  InfoFont ("Arial", 12, 1)

  infoct ("white", "] (" )
  InfoColour ("yellow"  )

  if nvar.EXPTOLEVEL < 100 then
    Info (space)
  end -- if

  Info (svar.EXPTOLEVEL)

  infoct ("white", "%)   Last (" )
  infoct ("yellow", svar.XPDIFF)
  infoct ("white", "%)   Level: ")
  
  InfoColour ("yellow")

  if svar.level2 == "subhero" then
    Info (svar.Level)
  else
    InfoColour ("red")
    Info (svar.level2 .. svar.level)
  end -- if

  EnableGroup ("SHighlight", 0)

end -- do

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Shadowfyr   USA  (1,788 posts)  Bio
Date Reply #9 on Sat 11 Dec 2004 10:09 PM (UTC)
Message
Couple of points:

"You don't declare variables in Lua, so the "dim" lines went away."

You don't have to in VBScript or most others either. It is still done by convention to pre-declare global variables or to make it easier to keep track of what variables you are using, since you can stick all the 'dim' statements at the start of a script/function.

"Things like bad arithmetic only show up at runtime, as Lua has untyped variables."

Ok.. This is one major strike against it imho. I am tired of languages you can't define the types for. You get the same rediculous runtime issues in VBScript, for precisely the same reason. Strict typing helps avoid mistakes, it isn't a 'feature' as far as I am concerned for a language to be missing it. There is after all a reason why most languages have strict types. ;)

Anyway, nice to know before hand that these issues exist.
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #10 on Sat 11 Dec 2004 10:39 PM (UTC)
Message
No, you don't have to declare them in VBscript, unless you use "option explicit". I was explaining why those lines had to go.

Lua gives you the flexibility to do that or not. As the Lua author points out in his book, you can add explicit variables if you want to.

In chapter 14.2 of his book (or see the equivalent in the online manual) he points out that you can make a metatable for the global environment, that will catch attempts to access non-existent variables and throw an error.

Then to create variables in the first place you add a method "declare" that creates them. This effectively gives you "dim" and "option explicit" back.

Quote:

This is one major strike against it imho. I am tired of languages you can't define the types for. You get the same rediculous runtime issues in VBScript, for precisely the same reason.


Well, Lua has automatic type conversion, so most of the problems go away, depending on the arithmetic operator. For example, adding is for numbers, right? So if you add together two things it tries to convert them to numbers, unlike VBscript which gives strange behaviour.

eg.

Lua:

Note ("10" + "5") --> result 15

VBscript:

Note "10" + "5" ' result 105


Lua has a separate operator for concatenation, so if that is what you want, you use the appropriate operator.

Similarly in Lua, if you want to concatenate, numbers get converted to strings:

Note (10 .. 5) --> 105

In most cases you are controlling the data, so you know what type it is anyway (and if you use the explicit declarations idea, you shouldn't get nil data).

If you aren't sure you can find the type:

Note (type ("10")) --> string

And in my example above, you can make a special table (like nvar) that takes things like strings from GetVariable and converts them to numbers for you.

If you are writing pre-canned functions, then it is a simple matter to check the types of the variables as they are being passed in, eg.


function foo (x, y)

assert (type (x) == "string", "X must be a string")
assert (type (y) == "number", "Y must be a number")

end -- foo


Admittedly this is a runtime check, not a compile-time check, but in things like "send to script" the difference is moot, as they aren't compiled anyway until runtime (that is, until the trigger fires).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #11 on Sat 11 Dec 2004 10:42 PM (UTC)
Message
My other comment about explicitly typed languages is this. Although I like the idea personally, it tends to cut down on coding errors, in a scripting environment you tend to simply move the problem.

Say we have a trigger script. The stuff arriving is text anyway, so in order to store it into a (typed) variable we need a runtime conversion, and that conversion may still fail.

eg. int x = ConvertToNumber (wildcard (5))

So, we still have the possibility of runtime errors, we have just moved where they occur around a bit.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #12 on Sat 11 Dec 2004 10:58 PM (UTC)

Amended on Sat 11 Dec 2004 11:00 PM (UTC) by Flannel

Message
Nothing saying you will have to switch to Lua anyway.

If you have a feature you want, make it a plugin. Thats the beauty of the plugin system, different script languages.
So you can (in certain cases) have the majority of your scripts/etc in one language that you are comfortable with, and then have a plugin to say... make another window, in lua. You just call the plugin (from the script, in VB) and then the lua stuff (which you didnt write per say, you took it from nicks example and adapted it, or whatever) will get called.
Its the equivalent of calling a subroutine in VBScript, except youre calling a subroutine in a plugin (which is language independant).

Of course, thats assuming Nick will still answer questions in the other language forum sections ;)

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

Posted by David Berthiaume   (202 posts)  Bio
Date Reply #13 on Sun 12 Dec 2004 05:45 PM (UTC)
Message
That's amazing... The only problem is now that it's converted I don't have the slightest idea what to do with it.


In the VBscript version, I had 2 triggers, even though I could have condensed it down to 1.

The first set all my variables, and the second ran the script and made the infobar.


Do I place this script code in a trigger?
Or in the script file?
If in the trigger, which one?
The old variable trigger?(The second xml format trigger in my first posting)
Or in the execute trigger, the one with all the infobar code?
Also, In the sandbox in the global preferences, do I need to add the script file?(vaguely recalling something in another post: dofile something or other).

Believe it or not, I actually understand the scripting and how the language works, the problem I'm having, like I did with vbscript when I started with it, is how to tie MC and Lua together.

I can code the script, but how do I make it work with MC?

Top

Posted by Nick Gammon   Australia  (23,121 posts)  Bio   Forum Administrator
Date Reply #14 on Sun 12 Dec 2004 06:12 PM (UTC)
Message
I only converted one trigger, you said if you saw how it was done you could do the rest.

The trigger itself will be the same, just in the "send" box replace the VBscript stuff with the Lua stuff.

Then you would change the scripting language to Lua and instead of myfile.vbs for your script commands that are not "send to script" use myfile.lua.

It's not supposed to be that complicated. Just do whatever you used to do in VBscript, just using Lua as the language.

However you can't have some triggers using Lua and others using VBscript. If you have a lot you'll have to convert them all, OR put the Lua ones into a plugin.

Plugins can have a different language from the main file.

Don't just change over for the sake of it, if it is all working now I wouldn't bother changing.

Don't touch the global preferences unless you have a reason to. All I was saying was that if you wanted to have a script shared between every world you could put that into a file, and then just put:

dofile "myfile.lua"

into the global preferences.

However just stick to what you normally do - put the script file name into the box per world, in the scripting prefs.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


146,669 views.

This is page 1, subject is 5 pages long: 1 2  3  4  5  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.