Plugin Help - complete newbie to Lua

Posted by Eios on Sun 27 Sep 2020 11:18 AM — 9 posts, 31,694 views.

#0
I've found a plugin made by Nick for HP, MV and MANA. This creates a small window that can be moved with a graphic representation of the status'.

What I'm try to do is use parts of that for another plugin, to show whether or not my character is "Cloaked".

I'm new to Lua and from what I can tell with the error I'm getting, the issue lies within my function cloak_status, line 116 onwards.

The TLDR is that I'm after a moveable window that has the text in it showing Cloak: ON or Cloak: OFF.

Cloak: ON is fired off the following triggers:

1. ^You blend into the surroundings and hide yourself\.$
2. ^You are already cloaked\.$

Cloak: OFF is fired off when:

1. ^You step out of the shadows\.$

Here's the code so far, thank for all your help in advance.


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, September 27, 2020, 3:04 AM -->
<!-- MuClient version 5.06 -->

<!-- Plugin "Cloak_Status" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Cloak_Status"
   author="Eios"
   id="015ba97d6b9bc32363bb04b3"
   language="Lua"
   purpose="Shows wether you're cloaked or not"
   date_written="2020-09-27 03:02:50"
   requires="5.06"
   version="1.0"
   >

</plugin>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Triggers  -->

<triggers>

  <trigger
   enabled="y"
   match="^You blend into the surroundings and hide yourself\.$"
   regexp="y"
   name="cloak"
   script="cloak_status"
   sequence="100"
  >
  </trigger>
  
    <trigger
   enabled="y"
   match="^You are already cloaked\.$"
   regexp="y"
   name="cloaked"
   script="cloak_status"
   sequence="100"
  >
  </trigger>
  
    <trigger
   enabled="y"
   match="^You step out of the shadows\.$"
   regexp="y"
   name="uncloaked"
   script="cloak_status"
   sequence="100"
  >
  </trigger>

</triggers>  

<!--  Script  -->

<script>

<![CDATA[

WINDOW_WIDTH = 200
WINDOW_HEIGHT = 65

BACKGROUND_COLOUR = ColourNameToRGB "rosybrown"
FONT_COLOUR = ColourNameToRGB "black"
BORDER_COLOUR = ColourNameToRGB "#553333"

function mousedown(flags, hotspot_id)

  -- find where mouse is so we can adjust window relative to mouse
  startx, starty = WindowInfo (win, 14), WindowInfo (win, 15)
  
  -- find where window is in case we drag it offscreen
  origx, origy = WindowInfo (win, 10), WindowInfo (win, 11)
end -- mousedown

function dragmove(flags, hotspot_id)

  -- find where it is now
  local posx, posy = WindowInfo (win, 17),
                     WindowInfo (win, 18)

  -- move the window to the new location
  WindowPosition(win, posx - startx, posy - starty, 0, 2);
  
  -- change the mouse cursor shape appropriately
  if posx < 0 or posx > GetInfo (281) or
     posy < 0 or posy > GetInfo (280) then
    check (SetCursor ( 11))   -- X cursor
  else
    check (SetCursor ( 1))   -- hand cursor
  end -- if
  
end -- dragmove

function dragrelease(flags, hotspot_id)
  local newx, newy = WindowInfo (win, 17), WindowInfo (win, 18)
  
  -- don't let them drag it out of view
  if newx < 0 or newx > GetInfo (281) or
     newy < 0 or newy > GetInfo (280) then
     -- put it back
    WindowPosition(win, origx, origy, 0, 2);
  end -- if out of bounds
  
end -- dragrelease

function cloak_status (name, line, wildcards)

  local status, on1 = ("ON")
  local cloaked, on2 = ("ON")
  local uncloaked, off = ("OFF")
  
end -- function
  
function ShowStatus
  
  cloak_status ("Cloak: ", on1, ColourNameToRGB "black")
  cloak_status ("Cloak: ", on2, ColourNameToRGB "black")
  cloak_status ("Cloak: ", off, ColourNameToRGB "black")
  
  WindowShow (win, true)

end -- shows the status

function OnPluginInstall ()
  
  win = GetPluginID ()
  font_id = "fn"
  
  font_name = "Courier New"    -- the actual font

  local x, y, mode, flags = 
      tonumber (GetVariable ("windowx")) or 0,
      tonumber (GetVariable ("windowy")) or 0,
      tonumber (GetVariable ("windowmode")) or 8, -- bottom right
      tonumber (GetVariable ("windowflags")) or 0
    
  -- make miniwindow so I can grab the font info
  check (WindowCreate (win, 
                 x, y, WINDOW_WIDTH, WINDOW_HEIGHT,  
                 mode,   
                 flags,   
                 BACKGROUND_COLOUR) )

  -- make a hotspot
  WindowAddHotspot(win, "hs1",  
                   0, 0, 0, 0,   -- whole window
                   "",   -- MouseOver
                   "",   -- CancelMouseOver
                   "mousedown",
                   "",   -- CancelMouseDown
                   "",   -- MouseUp
                   "Drag to move",  -- tooltip text
                   1, 0)  -- hand cursor
                   
  WindowDragHandler(win, "hs1", "dragmove", "dragrelease", 0) 
                 
  check (WindowFont (win, font_id, font_name, 9, false, false, false, false, 0, 0))  -- normal
  
  font_height = WindowFontInfo (win, font_id, 1)  -- height
  
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    check (EnablePlugin(GetPluginID (), false))
    return
  end -- they didn't enable us last time
 
end -- OnPluginInstall

function OnPluginDisable ()
  WindowShow (win, false)
end -- OnPluginDisable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
  SetVariable ("windowx", tostring (WindowInfo (win, 10)))
  SetVariable ("windowy", tostring (WindowInfo (win, 11)))
  SetVariable ("windowmode", tostring (WindowInfo (win, 7)))
  SetVariable ("windowflags", tostring (WindowInfo (win, 8)))
end -- OnPluginSaveState

]]>

</script>

</muclient>
Australia Forum Administrator #1
Post the error message please (copy and paste).
#2
Morning, this is what I'm receving.


Compile error
Plugin: Cloak_Status (called from world: Merentha - Eios)
Immediate execution
[string "Plugin: Cloak_Status"]:211: '(' expected near 'cloak_status'
[WARNING] C:\MUSHclient\worlds\plugins\Cloak_status.xml
Line 65: Error parsing script (Cannot load)
#3
Managed to get it to load, however when the text fires off that should fire the triggers, NIL window appears.


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, September 27, 2020, 3:04 AM -->
<!-- MuClient version 5.06 -->

<!-- Plugin "Cloak_Status" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Cloak_Status"
   author="Eios"
   id="015ba97d6b9bc32363bb04b3"
   language="Lua"
   purpose="Shows wether you're cloaked or not"
   date_written="2020-09-27 03:02:50"
   requires="5.06"
   version="1.0"
   >

</plugin>


<!--  Get our standard constants -->

<include name="constants.lua"/>

<!--  Triggers  -->

<triggers>

  <trigger
   enabled="y"
   match="^You blend into the surroundings and hide yourself\.$"
   regexp="y"
   name="cloak"
   script="cloak_status"
   sequence="100"
  >
  </trigger>
  
    <trigger
   enabled="y"
   match="^You are already cloaked\.$"
   regexp="y"
   name="cloaked"
   script="cloak_status"
   sequence="100"
  >
  </trigger>
  
    <trigger
   enabled="y"
   match="^You step out of the shadows\.$"
   regexp="y"
   name="uncloaked"
   script="cloak_status"
   sequence="100"
  >
  </trigger>

</triggers>  

<!--  Script  -->

<script>

<![CDATA[

WINDOW_WIDTH = 200
WINDOW_HEIGHT = 65

BACKGROUND_COLOUR = ColourNameToRGB "rosybrown"
FONT_COLOUR = ColourNameToRGB "black"
BORDER_COLOUR = ColourNameToRGB "#553333"

function mousedown(flags, hotspot_id)

  -- find where mouse is so we can adjust window relative to mouse
  startx, starty = WindowInfo (win, 14), WindowInfo (win, 15)
  
  -- find where window is in case we drag it offscreen
  origx, origy = WindowInfo (win, 10), WindowInfo (win, 11)
end -- mousedown

function dragmove(flags, hotspot_id)

  -- find where it is now
  local posx, posy = WindowInfo (win, 17),
                     WindowInfo (win, 18)

  -- move the window to the new location
  WindowPosition(win, posx - startx, posy - starty, 0, 2);
  
  -- change the mouse cursor shape appropriately
  if posx < 0 or posx > GetInfo (281) or
     posy < 0 or posy > GetInfo (280) then
    check (SetCursor ( 11))   -- X cursor
  else
    check (SetCursor ( 1))   -- hand cursor
  end -- if
  
end -- dragmove

function dragrelease(flags, hotspot_id)
  local newx, newy = WindowInfo (win, 17), WindowInfo (win, 18)
  
  -- don't let them drag it out of view
  if newx < 0 or newx > GetInfo (281) or
     newy < 0 or newy > GetInfo (280) then
     -- put it back
    WindowPosition(win, origx, origy, 0, 2);
  end -- if out of bounds
  
end -- dragrelease

function cloak_status(name, line, wildcards)

  local cloak, on1 = ("ON")
  local cloaked, on2 = ("ON")
  local uncloaked, off = ("OFF")
  
end -- function
  
function ShowStatus(name, line, wildcards)
  
  cloak_status ("Cloak: ", on1, ColourNameToRGB "black")
  cloak_status ("Cloak: ", on2, ColourNameToRGB "black")
  cloak_status ("Cloak: ", off, ColourNameToRGB "black")
  
  WindowShow (win, true)

end -- shows the status

function OnPluginInstall ()
  
  win = GetPluginID ()
  font_id = "fn"
  
  font_name = "Courier New"    -- the actual font

  local x, y, mode, flags = 
      tonumber (GetVariable ("windowx")) or 0,
      tonumber (GetVariable ("windowy")) or 0,
      tonumber (GetVariable ("windowmode")) or 8, -- bottom right
      tonumber (GetVariable ("windowflags")) or 0
    
  -- make miniwindow so I can grab the font info
  check (WindowCreate (win, 
                 x, y, WINDOW_WIDTH, WINDOW_HEIGHT,  
                 mode,   
                 flags,   
                 BACKGROUND_COLOUR) )

  -- make a hotspot
  WindowAddHotspot(win, "hs1",  
                   0, 0, 0, 0,   -- whole window
                   "",   -- MouseOver
                   "",   -- CancelMouseOver
                   "mousedown",
                   "",   -- CancelMouseDown
                   "",   -- MouseUp
                   "Drag to move",  -- tooltip text
                   1, 0)  -- hand cursor
                   
  WindowDragHandler(win, "hs1", "dragmove", "dragrelease", 0) 
                 
  check (WindowFont (win, font_id, font_name, 9, false, false, false, false, 0, 0))  -- normal
  
  font_height = WindowFontInfo (win, font_id, 1)  -- height
  
  if GetVariable ("enabled") == "false" then
    ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
    check (EnablePlugin(GetPluginID (), false))
    return
  end -- they didn't enable us last time
 
end -- OnPluginInstall

function OnPluginDisable ()
  WindowShow (win, false)
end -- OnPluginDisable

function OnPluginSaveState ()
  SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
  SetVariable ("windowx", tostring (WindowInfo (win, 10)))
  SetVariable ("windowy", tostring (WindowInfo (win, 11)))
  SetVariable ("windowmode", tostring (WindowInfo (win, 7)))
  SetVariable ("windowflags", tostring (WindowInfo (win, 8)))
end -- OnPluginSaveState

]]>

</script>

</muclient>
Amended on Mon 28 Sep 2020 01:17 AM by Eios
Australia Forum Administrator #4
What are you doing here?


function cloak_status(name, line, wildcards)

  local cloak, on1 = ("ON")
  local cloaked, on2 = ("ON")
  local uncloaked, off = ("OFF")
  
end -- function


Local variables are local to the function (they don't exist elsewhere). So setting three local variables, and doing nothing else, seems to me to achieve nothing.




Meanwhile, here:


  cloak_status ("Cloak: ", on1, ColourNameToRGB "black")
  cloak_status ("Cloak: ", on2, ColourNameToRGB "black")
  cloak_status ("Cloak: ", off, ColourNameToRGB "black")


The variables on1, on2, and off are all global variables (different to the ones in the other function) and as you don't declare them anywhere else, will be nil.
#5
Ok, I sort of get what you're putting down.

This was my thought pattern...

Within the triggers, each one has it's specific name, being either;

1. cloak
2. cloaked
3. uncloaked

the function cloak_status would check which version of the trigger has fired from the name, and the ("ON") or ("OFF") was for the text I wanted to it show.

The function ShowStatus, then grabs the on1, on2 or off from the function cloak_status and prints within the window either Cloak: ON or OFF.

I'll play with it tonight/ do some more research and see what I can do.
Australia Forum Administrator #6

function cloak_status(name, line, wildcards)


The trigger name is in the "name" argument.
#7
I'm so close!

My only issue now is that the text seems to stack upon itself.

I've tried this, which didn't help.


  if cloak_status_cloak true then
  WriteText ("Cloak: ON")
  elseif cloak_status_cloaked true then
  WriteText ("Cloak: ON") true then
  else if cloak_status_uncloaked true then
  WriteText ("Cloak: OFF")
  end


I guess I need some way of clearning what was previously sent, but I have no idea how. I've also currently split this up into 3 separate functions, to try fix- it didn't.


function WriteText(sPrompt, Colour)

  WindowText (win, font_id, sPrompt, 
			  16, 2.5, 0, 0, FONT_COLOUR)

end -- write the text in the loaded window and font size
 
function cloak_status_cloak(name)
  
  WriteText ("Cloak: ON")

  WindowShow (win, true)

end

function cloak_status_cloaked(name)
  
  WriteText ("Cloak: ON")
  
  WindowShow (win, true)
  
end

function cloak_status_uncloaked(name)

  WriteText ("Cloak: OFF")
  
  WindowShow (win, true)
  
end


Once again, any help is apprecieated!
Amended on Mon 28 Sep 2020 01:53 PM by Eios
Australia Forum Administrator #8
WindowText doesn't clear what is under it. You need to do a WindowRectOp with selector 2 (miniwin.rect_fill) with the background colour.


Template:function=WindowRectOp
WindowRectOp

The documentation for the WindowRectOp script function is available online. It is also in the MUSHclient help file.