• AddOption
    24 replies, posted
How would I go about using "AddOption" alone to suspend a menu in the center of the screen. Not sure if you use AddOption or not, but when I saw the menu it reminded me of so. Example: [img]http://img252.imageshack.us/img252/254/menutn.png[/img]
What do you mean?
Make a DMenu: local asdf = DermaMenu() Add an option: asdf:AddOption("fefe", function() print('fefe') end) Open in the middle of the screen: asdf:Open(ScrW() / 2, ScrH() / 2)
When the player is "Law Enforcement" and they open the doors menu the following error shows up. Apparently the options all run on open of the menu? Error: "Attempt to call global "GetDoorOwner" a nil value". This is my current code. [lua] function DoorMenu() local client = LocalPlayer() local tr = client:EyeTrace( 150 ) Ow = tr.Entity:GetNWString("Owner") Fac = client:GetNWString("Faction") local DoorOptionMenu = DermaMenu() DoorOptionMenu:AddOption( " Door Options" ) DoorOptionMenu:AddOption( "______________" ) if(Ow != nil and Fac != "LawEnforcement") then DoorOptionMenu:AddOption( "No Options" ) end if(Ow != nil and Fac == "LawEnforcement" ) then DoorOptionMenu:AddOption( "Get Owner", GetDoorOwner() ) end if(Ow == nil ) then DoorOptionMenu:AddOption( "Buy Door", OwnDoor() ) end if(Ow == client:GetNWString("RPName") ) then DoorOptionMenu:AddOption( "Sell Door", SellDoor() ) DoorOptionMenu:AddOption( "Door Title", DoorTitle() ) DoorOptionMenu:AddOption( "Add/Remove Users", ManageDoorUsers() ) end DoorOptionMenu:AddOption( "______________" ) DoorOptionMenu:AddOption( "Close" ) DoorOptionMenu:Open( w / 2, h / 2 ) end [/lua]
[QUOTE=Lyeol;18126184]When the player is "Law Enforcement" and they open the doors menu the following error shows up. Apparently the options all run on open of the menu? Error: "Attempt to call global "GetDoorOwner" a nil value". This is my current code. [/QUOTE] The error has nothing to do with the code you posted. The error you're getting is from trying to call the function 'GetDoorOwner' when it doesn't exist.
[QUOTE=yakahughes;18124981]Make a DMenu: local asdf = DermaMenu() Add an option: asdf:AddOption("fefe", function() print('fefe') end) Open in the middle of the screen: asdf:Open(ScrW() / 2, ScrH() / 2)[/QUOTE] Thanks for this, you just solved a small issue I was having with SeriousRP :D
DoorOptionMenu:AddOption( "Get Owner", GetDoorOwner() ) DoorOptionMenu:AddOption( "Buy Door", OwnDoor() ) DoorOptionMenu:AddOption( "Sell Door", SellDoor() ) DoorOptionMenu:AddOption( "Door Title", DoorTitle() ) DoorOptionMenu:AddOption( "Add/Remove Users", ManageDoorUsers() ) All these are wrong. If you do it that way, right when you open the menu all those functions are run. Do it this way: DoorOptionMenu:AddOption( "Get Owner", GetDoorOwner) DoorOptionMenu:AddOption( "Buy Door", OwnDoor) DoorOptionMenu:AddOption( "Sell Door", SellDoor) DoorOptionMenu:AddOption( "Door Title", DoorTitle) DoorOptionMenu:AddOption( "Add/Remove Users", ManageDoorUsers) But still, you need to define GetDoorOwner() too. Thanks to entoros who showed me a shorter way to do it.
Yaka, if AddOption accepts one function argument then you can still run the functions outside of a "function() end", just without passing arguments to the given function. i.e. [code]DoorOptionMenu:AddOption( "Get Owner", GetDoorOwner )[/code] But you are right in that you have to create those functions before you can use them. They're not default global functions. *edit* Actually, will AddOption accept argument to the GetDoorOwner function as separate arguments to the AddOption function, like RunConsoleCommand? Like, [code]DoorOptionMenu:AddOption( "Get Owner", GetDoorOwner, LocalPlayer():GetEyeTrace().HitPos, true)[/code] There's no documentation for AddOption so I wonder.
No, it doesn't. [url]http://luabin.foszor.com/code/addons/derma/lua/vgui/DMenu.lua[/url] And thanks for telling me that, let me go test it :D EDIT: Actually I don't even need to test it because a function without () would be a global variable that holds the function, and () just tells it to DO IT NAO so yeah, that would work. Lemme change my other post.
Just talking syntax wise, you've always been able to pass a function argument to another function like that. :P Sure, tell me what you get.
Ninja'd. Yeah I knew you could, you're doing that even with function() end but I didn't know you could do a function name without the () and it would be the same thing.
Well, I'm getting some very mixed thought on this. For some odd reason, it doesn't notify the player with any function called client sided. As of right now it appears the function isn't being called, I also tried placing a print to detect whether it is called or not, this turned unsuccessful. But, for some odd reason when I "Accidentally" made a error, the error was printed in to console. Therefor the function is being called, but nothing within the function is actually calling. Both of the commands work when I type the command in console but not when called via the menu. [lua] function TestNotify(ply) ply:Notify("This is a damn test for notify.") end concommand.Add("TestNotify", TestNotify) function GetDoorOwner(ply) if(ply:GetNWString("Faction") != "LawEnforcement") then ply:Kick("Attempted Exploiting.") return false end local trace = ply:GetEyeTrace() Ow = trace.Entity:GetNWString("Owner") if(Ow == "Nil") then ply:Notify("That door is not registered to anyone.") return false end if(Ow == "LawEnforcement") then ply:Notify("That door belongs to The Law.") return false end if(Ow == "Unownable") then ply:Notify("That door can not have a Owner.") return false end ply:Notify("The deed to that door is registered to "..Ow) end [/lua] [lua] function META:Notify ( Text ) if SERVER then umsg.Start('_Notify', self) umsg.String(Text) umsg.End() else GAMEMODE:AddNotify(Text, NOTIFY_GENERIC, 15) end end [/lua] [lua] function DoorMenu() local client = LocalPlayer() local tr = client:EyeTrace( 150 ) Ow = tr.Entity:GetNWString("Owner") Fac = client:GetNWString("Faction") local DoorOptionMenu = DermaMenu() DoorOptionMenu:AddOption( " Door Options" ) DoorOptionMenu:AddOption( "______________" ) if(Ow != nil and Fac != "LawEnforcement") then DoorOptionMenu:AddOption( "No Options" ) end if(Ow != nil and Fac == "LawEnforcement" ) then DoorOptionMenu:AddOption( "Get Owner", GetDoorOwner ) end if(Ow == nil ) then DoorOptionMenu:AddOption( "Buy Door", OwnDoor ) end if(Ow == client:GetNWString("RPName") ) then DoorOptionMenu:AddOption( "Sell Door", SellDoor ) DoorOptionMenu:AddOption( "Door Title", DoorTitle ) DoorOptionMenu:AddOption( "Add/Remove Users", ManageDoorUsers ) end DoorOptionMenu:AddOption( "______________" ) DoorOptionMenu:AddOption( "Close" ) DoorOptionMenu:Open( w / 2, h / 2 ) end concommand.Add("DoorMenu", DoorMenu) [/lua]
To ensure that it's a problem with the functions being called, make a short and simple option like this: [code]DoorOptionMenu:AddOption( "Test", function() LocalPlayer():ChatPrint( "Is this working?" ) end )[/code]
Odd, that did work. Yet, the other features do not. I'm gonna try to revamp the notify system.
Because your functionstake ply as an argument, yoy will have to change the stuff in DoorOpenMenu:AddOption("Sell Door", SellDoor) for example, to DoorOpenMenu:AddOption("Sell Door", function() SellDoor(LocalPlayer()) end) But because this is client side it can be worked around to where people can see whatever menu they want.
No matter what I do, it keeps giving me the error "cl_init.lua:354: attempt to call global 'GetDoorOwner' (a nil value)". Line:354: DoorOptionMenu:AddOption( "Get Owner", function() GetDoorOwner(client) end ) I'm guessing this is because my function is not "Global"?
Is the GetDoorOwner function in the same code as the DoorMenu function?
No, I tried that and it led to the same error but with the Notify function because "SendLua" is not global.
[lua]DoorOptionMenu:AddOption( "Get Owner", function() GetDoorOwner() end )[/lua]
[QUOTE=octogon;18137650][lua]DoorOptionMenu:AddOption( "Get Owner", function() GetDoorOwner() end )[/lua][/QUOTE] Thank you for telling us what we already knew. Please leave.
GetDoorOwner is a nil value, are you defining it?
[QUOTE=Justin37111;18138710]GetDoorOwner is a nil value, are you defining it?[/QUOTE] Yes, the function can be used anywhere else. Just not apparently from ClientSide.
If it's defined server side then of course it can't be used client side.
[QUOTE=yakahughes;18143257]If it's defined server side then of course it can't be used client side.[/QUOTE] Well, yeah. But sendlua isn't client sided either, it is part of gmod's engine. So I can't really change that.
[QUOTE=Entoros;18138123]Thank you for telling us what we already knew. Please leave.[/QUOTE] Why reply?
Sorry, you need to Log In to post a reply to this thread.