list functions

Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

list functions

Post by Metho »

So, I'm not sure what I'm doing wrong, as it's pretty much right out of the manual, but here goes:

Three aliases - en, ren, and showen.

Code: Select all

Alias: addEnemy
Pattern: ^en\s(.*)$
Script:
send( "enemy " .. matches[2] )
listAdd( myEnemies, matches[2] )
echo( "New enemy added: " .. matches[2] .. "\n" )
Result:
en Test
enemy Test
New enemy added: Test
>> You declare Test to be a formal enemy.

Code: Select all

Alias: removeEnemy
Pattern: ^ren\s(.*)$
Script:
send( "unenemy " .. matches[2] )
listRemove( myEnemies, matches[2] )
echo( "Enemy removed: " .. matches[2] .. "\n" )
Result:
ren Test
unenemy Test
Enemy removed: Test
>> You declare that Test will no longer be one of your enemies.
So, those look like they're working, but then I add a few names. Test, Whee, Blah, then I try invoking this alias:

Code: Select all

Alias: listEnemies
Pattern: ^showen$
Script:
listPrint( myEnemies )
Result:
showen
-------------------------------------------------------
Under debug:
LUA: ERROR running script listEnemies (Alias3) ERROR:Lua error:C:/Users/joshua.frazer/.config/mudlet/LuaGlobal.lua:115: bad argument #1 to 'ipairs' (table expected, got nil)
So it's like the table doesn't exist... but why?

Thanks for any help!

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Confused by simple list

Post by Heiko »

You forgot to declare your list as a Lua table. The list functions are just simple interface functions for normal Lua tables.
Put following line in your variable initialization script that is being run once when the profile is loaded.

myEnemies = {}

This declares myEnemies to be a table. Using variables that have not been declared properly prior to their first usage will be "nil" which will lead to a Lua error as the variable has not been defined.
Simplest way to handle global variable initialization:
Add a script item to "scripts" and call it initscript or similar. Declare all your global variables in this script. All scripts are run once when the profile is loaded or when you click on a script item in the editor. Check the manual section that explains variables and initialization scripts.
Normally, you'll want to declare a proper init() function that can be called by your scripts as well e.g.

function myGlobalVariable init()
myEnemies = {}
myHealth = 0
myName = "Jim"
end

Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

Re: Confused by simple list

Post by Metho »

Sorry, I forgot to mention that I did try that. I created two new folders (like in the manual picture) under scripts => Global => Variable Definitions.

Under the Variable Definitions folder, I created a new script:
Script name: Enemies
Script:
if myEnemies == nil then
myEnemies = {}
end

Reloaded the profile, but it still does the same thing.

Interesting to note, too. I created another alias:

Code: Select all

Alias: listTest
Pattern: ^testlist$
Script:
if next (myEnemies) == nil then
  echo( "List empty" )
else
  echo( "List not empty!" )
for k, v in ipairs (myEnemies) do
  print (k, v)
end -- for
end -- if empty
Result:
testlist
List not empty!
And there was no errors in debug when running testlist. So... the list isn't nil, but it's not listing the things I added to it, either.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Confused by simple list

Post by Heiko »

You can use the function display( myEnemies ) to get a full dump of a table of any complexity.

The listAdd/listRemove functions are simple interfaces for Lua tables. They aren't really needed and will disappear in the future as everybody uses Lua tables directly. There might be a bug in the list functions. I have a look.

Here is a nice table howto: http://lua-users.org/wiki/TablesTutorial
and here is a link to the rest of the Lua howtos (all good): http://lua-users.org/wiki/TutorialDirectory

Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

Re: Confused by simple list

Post by Metho »

Alright, fair enough. I think something must be wrong though, because I even tried to use an example from those links in mudlet and still nothing displayed:

Code: Select all

Alias: pleaseWork
Pattern: ^wrk$
Script:
t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do print(k,v) end
Result:
wrk
Nothing.
display( wrk )
Nothing.
I am totally confused.

Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

Re: Confused by simple list

Post by Metho »

Alright, fair enough. I think something must be wrong though, because I even tried to use an example from those links in mudlet and still nothing displayed:

Code: Select all

Alias: pleaseWork
Pattern: ^wrk$
Script:
t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do print(k,v) end
Result:
wrk
Nothing.
display( wrk )
Nothing.
I am totally confused.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Confused by simple list

Post by Heiko »

I have attached a working list demo package. The printList() function is indeed broken - use display() instead. The rest of the functions works fine.
Attachments
listDemo.xml
(1.33 KiB) Downloaded 422 times

Metho
Posts: 14
Joined: Wed Sep 16, 2009 10:20 pm

Re: Confused by simple list

Post by Metho »

I REALLY appreciate the effort, but this still isn't working for me. I imported, tried it out, and still nothing displays. Every time display() is called (Which it is for all three aliases in your demo), debug gives me:
LUA: ERROR running script ^remove enemyprint enemies (Alias10) ERROR:Lua error:[string "function Alias10()..."]:2: attempt to call global 'display' (a nil value)
So, if the demo works for you, but not me... obviously something else is borked. I'm running beta 14 - a fresh install from today, Vista 32.

But if you look at my post above (Which I accidentally posted twice), I can't even define a static table and then display it's contents right after creation. I don't know what is screwed up.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Confused by simple list

Post by Heiko »

This error is pretty straight forward. Your version doesn't yet have the display() function.
Change the calls to display( myEnemies ) to printTable( myEnemies ). Then it'll work - or make a script item e.g. "global functions" and put this code in there:

Code: Select all

----------------------------------------------------------------------------------
-- function by Ryan: pretty print function for tables
----------------------------------------------------------------------------------
-- usage: display( mytable )
----------------------------------------
-- pretty display function
function display(what, numformat, recursion)
  recursion = recursion or 0

  if recursion == 0 then
    echo("\n")
--    echo("-------------------------------------------------------\n")
  end
  echo(printable(what, numformat))

  -- Do all the stuff inside a table
  if type(what) == 'table' then
    echo(" {")

    local firstline = true   -- a kludge so empty tables print on one line
    for k, v in pairs(what) do
      if firstline then echo("\n"); firstline = false end
      echo(indent(recursion))
      echo(printable(k))
      echo(": ")
      display(v, numformat, recursion + 1)
    end

    -- so empty tables print as {} instead of {..indent..}
    if not firstline then echo(indent(recursion - 1)) end
    echo("}")
  end

  echo("\n")
  if recursion == 0 then
--    echo ("-------------------------------------------------------\n")
  end
end

-- Basically like tostring(), except takes a numformat
-- and is a little better suited for working with display()
function printable(what, numformat)
  local ret

  if type(what) == 'string' then
    ret = "'"..what.."'"
--    ret = string.format("%q", what)    -- this was ugly

  elseif type(what) == 'number' then
    if numformat then ret = string.format(numformat, what)
    else ret = what end

  elseif type(what) == 'boolean' then
    ret = tostring(what)

  elseif type(what) == 'table' then
    ret = what.__customtype or type(what)

  else
    ret = type(what)
--    ret = tostring(what)               -- this was ugly
  end

  return ret
end


-- Handles indentation
do local indents = {}  -- simulate a static variable
function indent(num)

  if not indents[num] then
    indents[num] = ""
    for i = 0, num do
      indents[num] = indents[num].."  "
    end
  end

  return indents[num]
end
end
-----------------------------------------------------------------------------------------------------------
Mudlet is nearly stable and the core functionality is extensively tested. Chances to find real bugs at this stage are very low. There are a few open bugs still, but they all relate to rarely used features or GUI improvements. Some users already have extensive profiles with curing systems that consit of hundrets of triggers and profile sizes of up to 2 MB.

Take a look at the Achaea demo package. It shows you how to do prompt information processing i.e. balance checks etc. more efficiently by using the more advanced Mudlet filter and chain trigger features.

There is also an enemy highlighter package that demonstrates mass trigger matching for people with thousands of city or clan enemies.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Confused by simple list

Post by Heiko »

Metho wrote:Alright, fair enough. I think something must be wrong though, because I even tried to use an example from those links in mudlet and still nothing displayed:

Code: Select all

Alias: pleaseWork
Pattern: ^wrk$
Script:
t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do print(k,v) end
Result:
wrk
Nothing.
display( wrk )
Nothing.
I am totally confused.
print() will print to the console and not on the screen. On windows you will not see anything - on Linux only if you start Mudlet from a terminal.
Use echo() or insertText() instead of print.

Code: Select all

t = { apple="green", orange="orange", banana="yellow" }
for k,v in pairs(t) do echo("key=" .. k .. " value=" .. v .. "\n") end

Post Reply