ULX

Author Topic: Need help with tweaking, code attached  (Read 7813 times)

0 Members and 2 Guests are viewing this topic.

Dripfed

  • Guest
Need help with tweaking, code attached
« on: February 13, 2007, 03:36:59 PM »
I'm adding support for wire-mod cvars with ULX Admin Menu. Simple (or so I thought, lol) tweaks to cl_menu.lua
Thing is, I have been staring at the code for a little while, and I KNOW that I have made just a simple mistake somewhere. So simple that I can't even see it. With your fresh eyes and knowledge of ULX, hope you guys can help me.

The error code I'm getting is

LC: ulx/cl_menu.lua:445: bad argument #1 to 'ipairs' (table expected, got nil)

Line 445 looks like this:-

for _, key in ipairs( ulx.wiresmenu_values ) do

And here is the chunk of code it is in (aping the AdminMenu style).
Code: [Select]
--------------
--Wires Menu--
--------------
local function rcvValues( um, updateFn )
local values = {}

for _, key in ipairs( ulx.wiresmenu_values ) do
values[ key ] = um:ReadShort()
end

updateFn( values )
end
-- No hook! We'll hook this later so we can tell it what to call for update.

local wiresRes = [["wires"
{
"window"
{
"ControlName" "Frame"
"fieldName" "window"
"xpos" "-200"
"ypos" "100"
"zpos" "290"
"wide" "1000"
"tall" "600"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
"settitlebarvisible" "1"
"title" "ULX Wires Menu"
"sizable" "0"
}
"divider"
{
"ControlName" "Divider"
"fieldName" "divider"
"xpos" "180"
"ypos" "20"
"wide" "2"
"tall" "460"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
}
"applylimits"
{
"ControlName" "Button"
"fieldName" "applylimits"
"xpos" "385"
"ypos" "430"
"zpos" "290"
"wide" "100"
"tall" "40"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
"labelText" "Apply"
"textAlignment" "center"
"dulltext" "0"
"brighttext" "0"
"font" "Default"
"wrap" "0"
"Command" "apply"
"Default" "0"
}
]]

local buttons = {
{ var="sbox_allownpcs", off="Allow NPCs", on="Disallow NPCs" },
{ var="sbox_godmode", off="Enable Global Godmode", on="Disable Global Godmode" },
{ var="sbox_plpldamage", off="Disable PvP Damage", on="Enable PvP Damage" },
{ var="sbox_noclip", off="Enable Noclip", on="Disable Noclip" },
}

for mul, info in ipairs( buttons ) do
wiresRes = wiresRes .. [[
"]] .. info.var .. [["
{
"ControlName" "Button"
"fieldName" "]] .. info.var .. [["
"xpos" "20"
"ypos" "]] .. 50 + (mul-1)*30 .. [["
"zpos" "290"
"wide" "150"
"tall" "24"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
"labelText" ""
"textAlignment" "center"
"dulltext" "0"
"brighttext" "0"
"font" "Default"
"wrap" "0"
"Command" "]] .. info.var .. [["
"Default" "0"
}
]]
end

local limits = {
{ name="wire_wheels", max=32 },
{ name="wire_waypoints", max=32 },
{ name="wire_values", max=32 },
{ name="wire_twoway_radioes", max=32 },
{ name="wire_turret", max=32 },
{ name="wire_thrusters", max=32 },
{ name="wire_target_finders", max=32 },
{ name="wire_speedometers", max=32 },
{ name="wire_emitters", max=32 },
{ name="wire_simple_explosive", max=32 },
{ name="wire_sensors", max=32 },
{ name="wire_screens", max=32 },
{ name="wire_relays", max=32 },
{ name="wire_rangers", max=32 },
{ name="wire_radioes", max=32 },
{ name="wire_sockets", max=32 },
{ name="wire_plugs", max=32 },
{ name="wire_pixels", max=32 },
{ name="wire_panels", max=32 },
{ name="wire_outputs", max=32 },
{ name="wire_oscilloscopes", max=32 },
{ name="wire_locators", max=32 },
{ name="wire_lights", max=32 },
{ name="wire_inputs", max=32 },
{ name="wire_indicators", max=32 },
{ name="wire_hoverballs", max=32 },
{ name="wire_gyroscopes", max=32 },
{ name="wire_gpss", max=32 },
{ name="wire_gate_trigs", max=32 },
{ name="wire_gate_times", max=32 },
{ name="wire_gate_selections", max=32 },
{ name="wire_gate_memorys", max=32 },
{ name="wire_gate_logics", max=32 },
{ name="wire_gate_comparisons", max=32 },
{ name="wire_gates", max=32 },
{ name="wire_explosive", max=32 },
{ name="wire_dual_inputs", max=32 },
{ name="wire_detonators", max=32 },
{ name="wire_buttons", max=32 },
{ name="wire_adv_inputs", max=32 },
}

for mul, info in ipairs( limits ) do
local name = info.name
local mulx = math.floor( (mul-1) / 14 ) -- Make rows of 14
local muly = math.fmod( mul-1, 14 )
wiresRes = wiresRes .. [[
"]] .. name .. [[lbl"
{
"ControlName" "Label"
"fieldName" "]] .. name .. [[lbl"
"xpos" "]] .. 10 + mulx*250 .. [["
"ypos" "]] .. muly*50 + 20 .. [["
"wide" "128"
"tall" "24"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
"labelText" "Max ]] .. string.upper( name:sub( 1, 1 ) ) .. name:sub( 2 ) .. [[:"
"textAlignment" "west"
"dulltext" "0"
"brighttext" "0"
"wrap" "0"
}
"]] .. name .. [[slider"
{
"ControlName" "Slider"
"fieldName" "]] .. name .. [[slider"
"xpos" "]] .. 10 + mulx*250 .. [["
"ypos" "]] .. muly*50 + 40 .. [["
"wide" "200"
"tall" "20"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
}
"]] .. name .. [[num"
{
"ControlName" "Label"
"fieldName" "]] .. name .. [[num"
"xpos" "]] .. 210 + mulx*250 .. [["
"ypos" "]] .. muly*50 + 40 .. [["
"wide" "64"
"tall" "24"
"autoResize" "0"
"pinCorner" "0"
"visible" "1"
"enabled" "1"
"tabPosition" "0"
"labelText" ""
"textAlignment" "west"
"dulltext" "0"
"brighttext" "0"
"wrap" "0"
}
]]
end

wiresRes = wiresRes .. "}"

local wiresMenu

function ulx.showwiresMenu()
if GetConVarString( "sv_gamemode" ) ~= "sandbox" then
ULib.csay( LocalPlayer(), "Sorry, this menu is only available in sandbox mode." )
return
end
-- We COULD use this line if we weren't taking such a hackish approach with the checkboxes.
-- if wiresMenu then wiresMenu:SetVisible( true ) return end
if wiresMenu then wiresMenu:SetVisible( false ) end

wiresMenu = vgui.Create( "Frame" )
wiresMenu:SetName( "window" )

local btns = {}
local function button_route( panel, action, cvar )
local value = math.fmod( btns[ cvar ].value + 1, 2 )
ULib.redirect( LocalPlayer(), "ulx", _, string.format( "rcon %s %i\n", cvar, value ) ) -- Garry broke our best way to do this.
btns[ cvar ].value = value

local info
for _, data in ipairs( buttons ) do
if data.var == cvar then
info = data
break
end
end

if value == 1 then
        btns[ cvar ].panel:SetText( info.on )
else
        btns[ cvar ].panel:SetText( info.off )
end
end

for _, info in ipairs( buttons ) do
local button = vgui.Create( "Button", wiresMenu, info.var )
button:SetText( info.off )
button:SetActionFunction( button_route )
btns[ info.var ] = { panel=button, value=0 }
end

local sliders = {}
for _, info in ipairs( limits ) do
local name = info.name
local cvar = "sbox_max" .. name
local max = info.max
local num = vgui.Create( "Label", wiresMenu, name .. "num" )
num:SetText( "0" )
local slider = vgui.Create( "Slider", wiresMenu, name .. "slider" )
slider:PostMessage( "SetInteger", "b", "1" )
slider:PostMessage( "SetLower", "f", "0" )
slider:PostMessage( "SetHigher", "f", max )
slider:SetActionFunction( function ( panel, action, value )
sliders[ cvar ].new = value
num:SetText( tostring( value ) )
end )
sliders[ cvar ] = { panel=slider, num=num, value=0, new=0 } -- So we can reference it in our update
end

local button = vgui.Create( "Button", wiresMenu, "applylimits" )
button:SetActionFunction( function ()
for cvar, _ in pairs( sliders ) do
local value = sliders[ cvar ].new
if value ~= sliders[ cvar ].value then
ULib.redirect( LocalPlayer(), "ulx", _, string.format( "rcon %s %i\n", cvar, value ) ) -- Garry broke our best way to do this.
        sliders[ cvar ].num:SetText( value )
        sliders[ cvar ].value = value
end
end
end )

wiresMenu:LoadControlsFromString( wiresRes )
wiresMenu:SetKeyBoardInputEnabled( true )
wiresMenu:SetMouseInputEnabled( true )

wiresMenu:SetPos( ScrW()*0.3, ScrH()-500 )
wiresMenu:SetVisible( true )

-- Update function...
local function update( values )
for cvar, value in pairs( values ) do
if sliders[ cvar ] then -- This is a cvar
local slider = sliders[ cvar ]
slider.value = value
slider.panel:PostMessage( "SetValue", "f", value )
slider.num:SetText( value )
else
local info
for _, data in ipairs( buttons ) do
if data.var == cvar then
info = data
break
end
end

if value ~= 0 then
local btn = btns[ cvar ]
btn.panel:SetText( info.on )
btn.value = 1
else
local btn = btns[ cvar ]
btn.panel:SetText( info.off )
btn.value = 0
end
end
end
end

usermessage.Hook( "ulx_menuvalues", rcvValues, update ) -- The hook will get our function and call it.
LocalPlayer():ConCommand( "ulx_valueupdate\n" )
end

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #1 on: February 13, 2007, 04:24:55 PM »
I'm the lua novice of the team, so I'll let my mates slap me if I'm wrong here, but,
~Line 214?

Code: [Select]

local wiresMenu

function ulx.showwiresMenu()
Blank local variable, whats this do?

and if that isn't it, should it be ulx.wiresMenu_values? or perhaps .values

Ok, my three guesses for now.

(and, even if I'm wrong, I will have learned new stuff here I'm sure)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline spbogie

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 456
  • Karma: 41
Re: Need help with tweaking, code attached
« Reply #2 on: February 13, 2007, 08:37:12 PM »
JamminR,
The local on its own like that reserves the name to be local so when used later as wiresMenu = <whatever> it will be local.

Dripfed,
It doesn't seem that you have defined ulx.wiresmenu_values anywhere, so it makes sense that it would be nil (unless it's defined outside the posted block).
I have not failed. I've just found 10,000 ways that won't work. - Thomas A. Edison
I reject your reality and substitute my own. - Adam Savage

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #3 on: February 13, 2007, 09:05:42 PM »
Hmm. well, yeah, what spbogie said.
I just presumed he had defined it outside the block somewhere :)
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Need help with tweaking, code attached
« Reply #4 on: February 14, 2007, 12:11:00 AM »
Yes the variable isn't defined anywhere. That's what you get for copying and pasting code when you don't know how it works. ;)
Experiencing God's grace one day at a time.

Dripfed

  • Guest
Re: Need help with tweaking, code attached
« Reply #5 on: February 14, 2007, 03:39:54 AM »
Ah yes! Thou Shalt define all thy variables!

Copying and pasting something that already works, saves reinventing the wheel ;) But you're right, makes it a nightmare to spot anything.

Plus, I only ever took a subsidiary module in computer programming whilst at uni'. This was when Java was expetected to be the big thing and hence Java, HTML, BASIC is all I can dabble in really, although slowly beginning to get my head around lua via immitating people's styles on gmod.


Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #6 on: February 14, 2007, 05:38:14 PM »
Sounds like you're my age Dripfed.
Keep on learning!

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Dripfed

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Need help with tweaking, code attached
« Reply #7 on: February 15, 2007, 04:15:03 AM »
Mid 20's and above....Jammin? Coding/scripting...All it ever did was make me drink heavily. Hours in the computer labs at uni' working on something ridiculously simple like making a functioning desktop calculator, and the bloody compiler would hate me. ERROR 'Unexpected ; at line 150,' so you look, you remove the ; and recompile. ERROR 'Expected ; at line 150' Made me want to take a fire axe to the mainframe.

Ok....

Got back round to this again. How, specifically, did you define your adminmenu variables? Online tutorials are far too basic, and I ain't going to buy a book (yet).

because I think I found the chunk

Code: [Select]
--------------
--Admin Menu--
--------------
local function rcvValues( um, updateFn )
local values = {}

for _, key in ipairs( ulx.adminmenu_values ) do
values[ key ] = um:ReadShort()
end

updateFn( values )
end

But...Replacing the names adminmenu with wiresmenu, does diddly squat. Even though, it is wiresmenu I am using throughout.

I'm at the stage in scripting now where ulx menu brings up the little button bar with admin menu, client menu, and the new wires menu on it. Clicking wires menu brings up a new table with all the various sliders for all the wires mod cvars, and an apply button. The sliders all function, but clicking the apply button brings up the initial error.

You say the variable is undefined. Probably is, but am unable to find specifically where it is undefined since, I didn't write the code, but am merely tweaking and customising to allow my admins to alter wires mod limits depending on what we're hosting. Essentially, I'm trying to add anything with cvar limits to ULX so that the none tech savvy admins don't have to type the sbox commands or know the rcon password etc.

Find the full cl_menu.lua here:-
www.gravesweb.pwp.blueyonder.co.uk/cl_menu.lua
« Last Edit: February 15, 2007, 04:18:09 AM by Dripfed »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #8 on: February 15, 2007, 05:56:10 PM »
By no means do I guarantee this will fix anything, however, I believe it will make for easier code separation later.

ulx.showMainMenu.
You have two buttons share the same variable.
Possible, yes, but, I'd still recommend using a different variable for the wires.
Line 107 and 108.
Code: [Select]
local admin = vgui.Create( "Button", mainMenu, "admin" )
admin:SetActionFunction( mainMenuCallback )

--->>>THIS ONE>> local client = vgui.Create( "Button", mainMenu, "wires" )
--->>>THIS ONE too> client:SetActionFunction( mainMenuCallback )

local client = vgui.Create( "Button", mainMenu, "client" )
client:SetActionFunction( mainMenuCallback )
I recommend exchanging 'wires' for 'client'. Unless you already use wires elsewhere.

Still looking....
« Last Edit: February 15, 2007, 06:01:22 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Dripfed

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Need help with tweaking, code attached
« Reply #9 on: February 16, 2007, 06:55:03 AM »
Last night I thought, "why make it over complex?"

The simple solution was obvious.

Reduce the width of the sliders, realign the 'apply' button, increase the size of the admin menu, and simply add all the cvars, I require into the expanded admin menu.

So that's what I've done, and it works.

If you fancy adding wire-mod cvar support to ULX Admin Menu, then can download cl_menu.lua here:-

www.gravesweb.pwp.blueyonder.co.uk/cl_menu.lua

Obviously, the ability add extra windows to ULX still exists, and should more cvars become required, I'll go back to adding an extra page into ULX.

Thanks for all your help anyway, certainly helps learning.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #10 on: February 16, 2007, 04:37:12 PM »
Dripfed, I now ask you to take on another challenge, with much learning.

I applaud your effort.
However, the next time ULX is updated, cl_menu will get overwritten, possibly updated, and you (or, we, which will then forward questions to you, pointing big fingers ;) ) will get asked what happen to the wires control.

So, now that you're on your way... could you turn this into a ULib module, that would simply go into the ULib\Modules folder?
I'm sure this would require a major re-write of what you've already done.

I believe a separate menu, with access given to ULib admins, to adjust for the various settings, should be possible not relying on "ulx." functions.

That way, no matter how many updates we do, it wouldn't overwrite your great effort.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Dripfed

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Need help with tweaking, code attached
« Reply #11 on: February 16, 2007, 05:18:42 PM »
It was a 2 minute job ;) Once I'd decided not to complicate things.

Regards making a module... Depends what time I have. Am in the middle of writing my end of module report for module 3 of my MA.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #12 on: February 16, 2007, 07:18:59 PM »
I just realized, not only would it prevent overwriting, it would allow some of those stubborn people out there who don't like ULX (*gasp*shock*) to not be required to use it., and expand your user base. (and, maybe even Ulysses team, when they realized what ucl control ULib can have, even without using ULX)

:)

Congratulations on your MA progress.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline Sc00by22

  • Jr. Member
  • **
  • Posts: 98
  • Karma: 0
Re: Need help with tweaking, code attached
« Reply #13 on: May 09, 2008, 11:48:04 PM »
Dead link :O I really wanted this

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Need help with tweaking, code attached
« Reply #14 on: May 10, 2008, 11:00:56 AM »
Dead link :O I really wanted this
Dripfed was last active here about the same time he left his last post, Feb 2007
Over a year ago. Perhaps you can find him active on FP or elsewhere. Just google his nickname, or search on Facepunch
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming