all in script part

Posted by Mahony on Thu 16 Oct 2014 09:05 PM — 8 posts, 27,298 views.

#0
Hi
I have all in script part and I suspect that this is the problem. OnPluginInstall, WindowCreate,...
See code


<script>
<![CDATA[
require "checkplugin"
require "tprint"
require "movewindow"

win = GetPluginID () .. "_ident"
font = "f"
report = {}

function OnPluginInstall ()
print("OnPluginInstall")
  -- install the window movement handler, get back the window position
  windowinfo = movewindow.install (win, 6)  -- default to 6 (on top right)
  -- make window so I can grab the font info
  WindowCreate (win, 0, 0, 0, 0, 1, 0, 0)

  -- add the font                 
  WindowFont (win, font, "Lucida Console", 9)  
    
end -- OnPluginInstall

function OnPluginSaveState ()
  -- save window current location for next time  
  movewindow.save_state (win)
end -- function OnPluginSaveState

local font_height = WindowFontInfo (win, font, 1)
local max_width = WindowTextWidth (win, font, "Report")
local window_width = max_width + 10
local window_height = font_height * (#report + 2) + 10

-- make window correct size

tprint(windowinfo)
--WindowCreate (win, 
--                 windowinfo.window_left, 
--                 windowinfo.window_top, 
--                 window_width, window_height,              -- width, height
--                 windowinfo.window_mode,   
--                 windowinfo.window_flags,
--                 ColourNameToRGB "#373737") 

WindowRectOp (win, 5, 0, 0, 0, 0, 5, 15 + 0x1000)

-- add the drag handler so they can move the window around
  movewindow.add_drag_handler (win, 0, 0, 0, font_height)

The client complains

[string "Plugin"]:185: bad argument #7 to 'WindowCreate' (number expected, got nil)


I found out that when I try tprint(windowinfo) in the OnPluginInstall it is ok but outside it complains

MUSHclient\lua\tprint.lua:34: bad argument #1 to 'pairs' (table expected, got string)

So it doesnt know anything about table windowinfo. I use this construct in another plugin where only the OnPluginInstall is in <script> section and rest is in alias body.

I don't know why it doesn't work. I'm lost. Help please.
Thank you
USA Global Moderator #1
Quote:
I have all in script part and I suspect that this is the problem.

That's irrelevant. The problem is that your code is wrong.

Quote:
See code

Most of your plugin is missing, so we can't see it. Post the whole thing.
Amended on Thu 16 Oct 2014 10:02 PM by Fiendish
#2
Sorry, I just didn't want to overload and I thought that the rest of the code is not important. I have a plugin that can report stats to the main window using Note. I just wanted to move the output to miniwin. I didn't get past the part I posted first... Thanx for help

<script>
<![CDATA[
require "checkplugin"
require "tprint"
require "movewindow"

win = GetPluginID () .. "_ident"
windowinfo = ""
font = "f"
report = {}

function OnPluginInstall ()
print("OnPluginInstall")
  -- install the window movement handler, get back the window position
  windowinfo = movewindow.install (win, 6)  -- default to 6 (on top right)
  -- make window so I can grab the font info
  WindowCreate (win, 0, 0, 0, 0, 1, 0, 0)

  -- add the font                 
  WindowFont (win, font, "Lucida Console", 9)  
    
end -- OnPluginInstall

function OnPluginSaveState ()
  -- save window current location for next time  
  movewindow.save_state (win)
end -- function OnPluginSaveState

local font_height = WindowFontInfo (win, font, 1)
local max_width = WindowTextWidth (win, font, "Report")
local window_width = max_width + 10
local window_height = font_height * (#report + 2) + 10

-- make window correct size

tprint(windowinfo)
--WindowCreate (win, 
--                 windowinfo.window_left, 
--                 windowinfo.window_top, 
--                 window_width, window_height,              -- width, height
--                 windowinfo.window_mode,   
--                 windowinfo.window_flags,
--                 ColourNameToRGB "#373737") 

WindowRectOp (win, 5, 0, 0, 0, 0, 5, 15 + 0x1000)

-- add the drag handler so they can move the window around
  movewindow.add_drag_handler (win, 0, 0, 0, font_height)

-- Tables --                                                           

All = {}                                  														--table will contain all info gotten from bid item#
flags = {}                                                                                      --table for working with ill,so,reson
--Other variables--
s = ""                                                                                          --string containing the output from the mud
a = 0
 
-- regtotable --                       
 
function regtotable (a,b)                                                                       --Fucntion to help with getting the data from bid item
        re = rex.new (a)                                                                        --and putting it in the All table
        _,_,t = re:match (s)
        if t ~= nil then
                table.remove (t,1)
                for k, v in pairs (t) do
                        if k == 1 then
                                table.remove (t,1)
                        elseif k == 2 then
                                table.remove (t,2)
                        else
                                All [k] = (v)
                        end -- if
                end -- for loop
        else
                All  [b] = "0"
                SaveState ()
        end
end -- regtotable
-- auction_to_channel--
function auction_to_channel(name,line,wildcards,styles)                                         --Function gets called by the alias or the triger
        if name == "AuctBroad" then                                                             --Alias calls the function
                SetVariable("aType", "bid ")
                if (wildcards.Auct_type) == "r" then
                        SetVariable("aType", "Rbid ")
                elseif  (wildcards.Auct_type) == "l" then
                        SetVariable("aType", "Lbid ")
                elseif (wildcards.Auct_type) == "" then
                        SetVariable("aType", "Bid ")
--moje uprava
				elseif (wildcards.Auct_type) == "o" then
                        SetVariable("aType", "oo ")
						if tostring(wildcards.Auct_nbr) then
						 All["Nbr"] = wildcards.Auct_nbr                                               --Add the item# to All table
						 EnableTrigger ("GetAuctLine", true)
						 Send ((GetVariable("aType")) .. wildcards.Auct_nbr)                           --Send bid item# to the mud
						end --if

				end --if
                if tonumber(wildcards.Auct_nbr) then
                 All["Nbr"] = wildcards.Auct_nbr                                                     --Add the item# to All table
				 EnableTrigger ("GetAuctLine", true)                                                 --Enables the trigger so the output from bid gets captured
				 SendNoEcho ((GetVariable("aType")) .. wildcards.Auct_nbr)                           --Send bid item# to the mud
                end --if
        end -- if
Amended on Fri 17 Oct 2014 06:09 AM by Nick Gammon
#3
...continue because I reached 6000chars and I had to cut something because I have still more than 6000ch


 

		Wstring = "report: "
        atype =  (GetVariable ("aType"))
                        if tonumber(All.Str) ~= 0 then
                                Wstring = (Wstring..string.format("%6s", All.Str).." Str")             
						else
								Wstring = (Wstring..string.format("%6s", " ").."    ")
						end
                        if tonumber(All.Int) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Int).." Int")
						else
								Wstring = (Wstring..string.format("%6s", " ").."     ")
                        end
                        if tonumber(All.Wis) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Wis).." Wis")             
						else
								Wstring = (Wstring..string.format("%6s", " ").."     ")
                        end
                        if tonumber(All.Dex) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Dex).." Dex")             
						else
								Wstring = (Wstring..string.format("%6s", " ").."     ")
                        end
                        if tonumber(All.Con) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Con).." Con")             
						else
								Wstring = (Wstring..string.format("%6s", " ").."     ")
                        end
                        if tonumber(All.Luck) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Luck).." Luc")            
						else
								Wstring = (Wstring..string.format("%6s", " ").."     ")
                        end
                        if tonumber(All.Dr) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Dr).." Dr")               
						else
								Wstring = (Wstring..string.format("%6s", " ").."     ")
                        end
                        if tonumber(All.Hr) ~= 0 then
                                Wstring = (Wstring.." "..string.format("%6s", All.Hr).." Hr")               
						else
								Wstring = (Wstring..string.format("%6s", " ").."    ")
                        end

				if All.Type == "Weapon" then
                        Wstring = (Wstring.." ["..All.Name.."]".." L "..All.Lvl.." "..All.Wtype.." -- |".."Dam Type "..All.Wdtype.."|, |".."AvD "..All.Wdmg.."|, & |"..All.Tstats.." TS")   
                        if flagstring ~= "" then
                                Wstring = (Wstring.." Fl ".."["..flagstring.."]")
                        end            
                else
                        Wstring = (Wstring.." ["..All.Name.."]".." L "..All.Lvl.." ["..All.Wear.."] -- ".."["..All.Tstats.." TS")   
						if tonumber(All.Tpoints) ~= 0 then        
                         Wstring = (Wstring.."] = ["..All.Tpoints.." TP".."]")
						else
                         Wstring = (Wstring.."]@w")
						end
						if flagstring ~= "" then
                                Wstring = (Wstring.." Fl ".."["..flagstring.."]")
                        end    
                end    
				Note(Wstring)
				table.insert (report, Wstring)
				local y = font_height * 2 + 5
			for k, v in pairs ( report ) do
				-- work out max width
				max_width = math.max (max_width, WindowTextWidth (win, font, v))
				x = x + WindowText (win, font, v, x, y, 0, 0, "yellow")
				y = y + font_height
			end --for
		WindowShow (win, true)
        SaveState ()
end -- Var_from_file
 
]]>
</script>
[/code
Amended on Fri 17 Oct 2014 06:09 AM by Nick Gammon
#4
I had to cut something from the middle that helps construct the Wstring. But it is not miniwin related. The important part is at the beginning. The WindowCreate is commented because it complains about argument #7 is nil. When I try to tprint(windowinfo) right before the WindowCreate it doesn't know the table windowinfo at all...
Australia Forum Administrator #5
You have put things in a really weird order. These lines:


local font_height = WindowFontInfo (win, font, 1)
local max_width = WindowTextWidth (win, font, "Report")
local window_width = max_width + 10
local window_height = font_height * (#report + 2) + 10

-- make window correct size

tprint(windowinfo)
--WindowCreate (win, 
--                 windowinfo.window_left, 
--                 windowinfo.window_top, 
--                 window_width, window_height,              -- width, height
--                 windowinfo.window_mode,   
--                 windowinfo.window_flags,
--                 ColourNameToRGB "#373737") 

WindowRectOp (win, 5, 0, 0, 0, 0, 5, 15 + 0x1000)

-- add the drag handler so they can move the window around
  movewindow.add_drag_handler (win, 0, 0, 0, font_height)

-- Tables --                                                           

All = {}     --table will contain all info gotten from bid item#
flags = {}    --table for working with ill,so,reson
--Other variables--
s = ""    --string containing the output from the mud
a = 0


... are not inside any function and are thus executed before anything else.

For example, you are trying to find the font height, before you have created the window. And you are using windowinfo before OnPluginInstall is called, and thus it won't be valid. You have to move that stuff inside OnPluginInstall.

More correct is this:


function OnPluginInstall ()

  print("OnPluginInstall")
  -- install the window movement handler, get back the window position
  windowinfo = movewindow.install (win, 6)  -- default to 6 (on top right)
  -- make window so I can grab the font info
  WindowCreate (win, 0, 0, 0, 0, 1, 0, 0)
  tprint(windowinfo)

  -- add the font                 
  WindowFont (win, font, "Lucida Console", 9)  

  font_height = WindowFontInfo (win, font, 1)
  max_width = WindowTextWidth (win, font, "Report")
  local window_width = max_width + 10
  local window_height = font_height * (#report + 2) + 10

    -- make window correct size
  
  WindowCreate (win, 
                   windowinfo.window_left, 
                   windowinfo.window_top, 
                   window_width, window_height,  -- width, height
                   windowinfo.window_mode,   
                   windowinfo.window_flags,
                   ColourNameToRGB "#373737") 
  
  WindowRectOp (win, 5, 0, 0, 0, 0, 5, 15 + 0x1000)
  
  -- add the drag handler so they can move the window around
    movewindow.add_drag_handler (win, 0, 0, 0, font_height)
  
  -- Tables --                                                           
  
  All = {}       --table will contain all info gotten from bid item#
  flags = {}     --table for working with ill,so,reson
  --Other variables--
  s = ""           --string containing the output from the mud
  a = 0
   
  
end -- OnPluginInstall


At least that displays this:


OnPluginInstall
"dragrelease"=function: 02E03BB0
"window_flags"=0
"preprocess":
"mousedown"=function: 02E05BF0
"window_left"=0
"window_mode"=6
"window_top"=0
"cancelmousedown"=function: 02E06320
"check_map_position"=function: 02E056B0
"mouseover"=function: 02E06440
"cancelmouseover"=function: 02E063B0
"win"="7cc9d7720077333c01c21372_ident"
"dragmove"=function: 02E04F30
"mouseup"=function: 02E00040
"window_friend_deltas":
"window_friends":
"margin"=20
Amended on Fri 17 Oct 2014 06:24 AM by Nick Gammon
#6
Quote:

... are not inside any function and are thus executed before anything else.

So here is the problem. It is obvious that it is not my code and I took it from somewhere and used it. Now when you say this it's all clear. I thought that it runs the function OnPluginInstall first because it is before the rest of the code.
Thank you! I'll try to adapt the code.
Australia Forum Administrator #7
The code outside any function is run when the plugin is loaded. Then it calls OnPluginInstall. I suppose the distinction might be fine, but it makes sense to have a function called rather than just say "leave anything you want done first outside a function".