• Need help with lua addon for Gmod
    36 replies, posted
Hello I have spent the last 2 days designing an addon for Gmod.But I came up with a lua problem. The question is can I set a button so when I click it,it will define a variable,and If it isnt click it wont? I tried this with this code local seab = vgui.Create ( "DButton" ); seab:SetSize ( 50 , 20) seab:SetPos (10 , 50) seab:SetText( "Near Sea" ) seab:SetParent ( window ) seab.DoClick = function () answer1="nearsea" end But it doesnt seem to work. Thanks in advance.
What do you mean it doesn't work? Does it error?
I added this on my lua to test if the variable it defined when I click the button if answer1==nearsea then Msg("lol") I click the button and I dont see the message on the console.
Where did you add that code?
In the lua file near the end of the script.
When that piece of code is being run, the answer1 variable isn't define. answer1 will only be defined after the button is clicked, but that code will be run before. Also: [lua]if answer1==nearsea then[/lua] should be [lua]if answer1=="nearsea" then[/lua]
[QUOTE=MakeR;20681073]When that piece of code is being run, the answer1 variable isn't define. answer1 will only be defined after the button is clicked, but that code will be run before. Also: [lua]if answer1==nearsea then[/lua] should be [lua]if answer1=="nearsea" then[/lua][/QUOTE] So when I click the button will I get "lol" on the console?
No, the code that you added to check if answer1 is "nearsea" will be run once only, before you get a chance to click the button. If you want to check if the variable is being set check it in the buttons click function: [lua]seab.DoClick = function () answer1="nearsea" print(answer1) end[/lua]
Well actually I let me be more specific about the script.There is a question with 3 answers ,each answer has a button.When you click one of the buttons I want it to be assigned as nearthesea for example so I will able to use later on the script with some <if>.So the think I tried to do is when the 1st button is clicked it will be assigned answer1="nearthesea" so I can use it later on the script.If there another way to do that?Because I want the script to make a conclusion based on the answers the user gives. Thanks
There is nothing wrong with the way you are doing it, I would just suggest making the answer variable local and declare it beforehand, like this: [lua]local answer; -- create the buttons button.DoClick = function() answer = "whatever" end[/lua]
Ok thanks a lot for your quick reply,I will check if it works later this day:)
Let me restate my problem Hello again,I am making a script which besically asks some questions and ends up to a conclusion.To be more specific so far I added all the questions down of each question there are some buttons ,each button is an answer.Here comes the logic problem.I want when a button is clicked (the user is answering a question) to define a variable.So i will be able to use it later on the script with some IF in order to end up to the conclusion. Here is how i tried to do it but it doesnt seem to work local seab = vgui.Create ( "DButton" ); seab:SetSize ( 50 , 20) seab:SetPos (30 , 55) seab:SetText( "Near Sea" ) seab:SetParent ( window ) seab.DoClick = function () answer1="nearsea" end I would really appreciate any help. Thanks
I don't see what your problem is, answer1 should now be equal to "nearsea".
[lua] -- For these kinds of things, a table is better than a gazillion variables local answers = {} local seab = vgui.Create ( "DButton", window ); seab:SetSize ( 50 , 20) seab:SetPos (30 , 55) seab:SetText( "Near Sea" ) seab.DoClick = function () answers[1] ="nearsea" -- Set the first answer, or the first element in the table, to "nearsea" end -- ... more code ... -- You use this whenever you're checking answers. if answers[1] == "nearsea" then -- THe answer was nearsea elseif answers[1] == "atsea" then -- The answer was atsea end[/lua] Yeah.
Yea but the think is that i want it to be equal to "nearsea" only if the button is clicked.
[QUOTE=spjohny;20704548]Yea but the think is that i want it to be equal to "nearsea" only if the button is clicked.[/QUOTE] It will be.
Button.DoClick is an "event" in the sense that it only runs when the button is clicked. The answer is not set to nearsea by default, only when the button is clicked.
[QUOTE=Entoros;20704587]Button.DoClick is an "event" in the sense that it only runs when the button is clicked. The answer is not set to nearsea by default, only when the button is clicked.[/QUOTE] How to test that the variavle is defined?
[lua]if ( variable != nil ) then end[/lua]
[QUOTE=Overv;20704655][lua]local window = vgui.Create( "DFrame" ); window:SetSize( 340,500 ); window:Center(); window:SetTitle( "ideaD" ); window:MakePopup(); local q1 = vgui.Create( "DLabel" ) q1:SetText( "Where would you prefer to be?" ) q1:SetParent( window ) q1:SetPos( 30, 35 ) q1:SetTextColor( Color(188, 247, 53, 255) ) q1:SizeToContents( ) local answers = {} local seab = vgui.Create ( "DButton", window ); seab:SetSize ( 50 , 20) seab:SetPos (30 , 55) seab:SetText( "Near Sea" ) seab:SetParent( window ) seab.DoClick = function () answers[1] ="nearsea" end local answers = {} local landb = vgui.Create ( "DButton" ); landb:SetSize ( 55 , 20) landb:SetPos (95 , 55) landb:SetText( "Near Land" ) landb:SetParent ( window ) landb.DoClick = function () answers[1]="nearland" end if answers[1] == "nearsea" then Msg("Cool") elseif answers[1] == "nearland" then Msg ("Too bad") end [/lua][/QUOTE] So when i run this it should write a messege on console Cool if button NearSea is clicked and Too bad if button nearland is clicked right? [editline]08:50PM[/editline] Anyone got an answer?
I told him this in already further up in this thread.
So is there any other way of doing this I want?
You don't need to do it another way, the way you are doing it works. You must be doing something incredibly wrong somewhere else for this to not work.
So the code it above you see any mistake?
All of it apart from the Msg's at the bottom. You have been told twice before why they won't work.
So Msg's are the problem.I just want to test that the variable is defined when you click the button,how can I do that?Because you know Im new and I want to understand lua not just learn it.
The variables are being defined, you don't need to test that. But if you really want to just stick the Msg's in the buttons DoClick function.
Ok i get it.One last think.This way after each question the answer goes on the memory.On the end of the script i can call this variables to use them with some IF and reach a conclusion right?And if so how do I connect 2 IF's so that If 2 answers are true in the same time a command will executed? Thanks
You should make another button that says submit, in the DoClick function of it you should do your answer checking. Your second question: [lua]if answer[1] == "whatever" and answer[2] == "somethingelse" then -- Code here end[/lua]
Ok thanks a lot I go make it and I hope I wont bother you again with my newbie questions :)
Sorry, you need to Log In to post a reply to this thread.