|
| | #1 (permalink) |
| <3 ![]() ![]() Join Date: Feb 2008 Location: London, ON (Canada, eh?)
Posts: 765
![]() ![]() | Hey, as I said hundreds of thousands of times, I'm a lua noob. I usually learn slow when coding by starting with the basics. And yet, I still suck at the basics. Since you guys here have made the most popular lua addon for garry's mod, I thought this would be a good place to post. (Except for FacePunch, but everyone hates eachother there. I find this forum more friendly. I love you guys). --cut the crap-- So, I'm making an In-game calculator. Which I find necessary for beginners to learn. And I need some Derma help. It's basically a Pop-up window with calculator functions. But, *gasp*, there's a problem. Code: function OpenCalc( )
local Calculator = vgui.Create( "DFrame" ) -- Creates the frame itself
Calculator:SetPos( 50,50 ) -- Position on the players screen
Calculator:SetSize( 400, 400 ) -- Size of the frame
Calculator:SetTitle( "Calculator" ) -- Title of the frame
Calculator:SetVisible( true )
Calculator:SetDraggable( true ) -- Draggable by mouse?
Calculator:ShowCloseButton( true ) -- Show the close button?
Calculator:MakePopup() -- Show the frame
function Calculator( Panel )
local Op = vgui.Create( "DNumSlider", Calculator )
Op:SetSize( 150, 50 )
Op:SetText( "Operator")
Op:SetMin( 1 )
Op:SetMax( 6 )
Op:SetDecimals( 0 )
Op:SetVisible( true )
local In1 = vgui.Create( "DTextEntry", Calculator )
In1:SetPos( 20,25 )
In1:SetTall( 20 )
In1:SetWide( 450 )
In1:SetText( "Primary")
In1:SetEnterAllowed( true )
In1:SetVisible( false )
local In2 = vgui.Create( "DTextEntry", Calculator )
In2:SetPos( 20,50 )
In2:SetTall( 20 )
In2:SetWide( 10 )
In2:SetText( "Secondary" )
In2:SetEnterAllowed( true )
In2:SetVisible( true )
local Out = vgui.Create( "DTextEntry", Calculator )
Out:SetPos( 20,75 )
Out:SetTall( 20 )
Out:SetWide( 32 )
Out:SetEnterAllowed( false )
Out:SetVisible( true )
end
end
concommand.Add( "OpenCalc", OpenCalc, Calculator, Op, In1, In2, Out );
But when I play ingame, type OpenCalc, (I have its bind set to "L") I get this; The frame, but no... anything. Just an empty frame. Am I missing some code? Many thanks for helpers.
__________________ Signature: WIREMOD WILL NOT WORK ON A PIRATED GMOD! ![]() ![]() MY COLOUR IS BLUE. Post your expressions here: www.wiredexpressions.tk It's my favourite country song. And I hate it. IEF015's got his own website :3 |
| | |
| Sponsored Links | |
| | #2 (permalink) |
| UWSVN Contributor ![]() ![]() ![]() Join Date: Dec 2007 Location: Hamburg, Germany
Posts: 230
![]() | Well, i would say that you missed something. You got a pretty nice "Calculator" function, which isn't called >.> Your concommand.Add thingy only says, that the OpenCalc function will be called. Not your Calculator function. second is, that you refer to a variable called "Calculator" all the time in your "Calculator" function. Tip: I wouldn't use the name of any function you declared as variable-name. But the argument you give to this function is called "Panel", which is not used anywhere. I didn't use VGUI until now, so i could really correct the code, but this should help you to correct your code yourself.
__________________ cmao = coding my ass off Free Fall Extension: 20081213 0002 Wire Socket Radio Rev2: 20081021 0932 (And it WORKS! (Mostly)) Please take a look into the Free Fall Extension and leave at least a small comment please, because it makes me kinda feel bad, if i put work into something, that nobody cares about. Understandable, isn't it? ![]() My IQ is 139 ![]() |
| | |
| | #3 (permalink) |
| <3 ![]() ![]() Join Date: Feb 2008 Location: London, ON (Canada, eh?)
Posts: 765
![]() ![]() | I'll try it once I get home. Thank you.
__________________ Signature: WIREMOD WILL NOT WORK ON A PIRATED GMOD! ![]() ![]() MY COLOUR IS BLUE. Post your expressions here: www.wiredexpressions.tk It's my favourite country song. And I hate it. IEF015's got his own website :3 |
| | |
| | #4 (permalink) |
| <3 ![]() ![]() Join Date: Feb 2008 Location: London, ON (Canada, eh?)
Posts: 765
![]() ![]() | Code: function OpenCalc( )
local Calculator = vgui.Create( "DFrame" ) -- Creates the frame itself
Calculator:SetPos( 50,50 ) -- Position on the players screen
Calculator:SetSize( 400, 400 ) -- Size of the frame
Calculator:SetTitle( "Calculator" ) -- Title of the frame
Calculator:SetVisible( true )
Calculator:SetDraggable( true ) -- Draggable by mouse?
Calculator:ShowCloseButton( true ) -- Show the close button?
Calculator:MakePopup() -- Show the frame
function Calculator( Panel )
local Op = vgui.Create( "DNumSlider" )
Op:SetSize( 150, 50 )
Op:SetText( "Operator")
Op:SetMin( 1 )
Op:SetMax( 6 )
Op:SetDecimals( 0 )
Op:SetVisible( true )
local In1 = vgui.Create( "DTextEntry" )
In1:SetPos( 20,25 )
In1:SetTall( 20 )
In1:SetWide( 450 )
In1:SetText( "Primary")
In1:SetEnterAllowed( true )
In1:SetVisible( false )
local In2 = vgui.Create( "DTextEntry" )
In2:SetPos( 20,50 )
In2:SetTall( 20 )
In2:SetWide( 10 )
In2:SetText( "Secondary" )
In2:SetEnterAllowed( true )
In2:SetVisible( true )
local Out = vgui.Create( "DTextEntry" )
Out:SetPos( 20,75 )
Out:SetTall( 20 )
Out:SetWide( 32 )
Out:SetEnterAllowed( false )
Out:SetVisible( true )
end
end
concommand.Add( "OpenCalc", OpenCalc, Calculator );
Still can't get it to work.
__________________ Signature: WIREMOD WILL NOT WORK ON A PIRATED GMOD! ![]() ![]() MY COLOUR IS BLUE. Post your expressions here: www.wiredexpressions.tk It's my favourite country song. And I hate it. IEF015's got his own website :3 |
| | |
| | #5 (permalink) | |
| Newbie ![]() Join Date: Feb 2008 Location: 30*N 10*W
Posts: 28
![]() | it would probably help to tell us what error message comes up in the console=)
__________________ Hitman217: Quote:
| |
| | |
| | #6 (permalink) |
| <3 ![]() ![]() Join Date: Feb 2008 Location: London, ON (Canada, eh?)
Posts: 765
![]() ![]() | Searched mighty hard for an error. Nothing to do with it. Didn't see any errors.
__________________ Signature: WIREMOD WILL NOT WORK ON A PIRATED GMOD! ![]() ![]() MY COLOUR IS BLUE. Post your expressions here: www.wiredexpressions.tk It's my favourite country song. And I hate it. IEF015's got his own website :3 Last edited by IEF015; 06-03-2008 at 04:35 PM.. |
| | |
| | #7 (permalink) |
| UWSVN Contributor ![]() ![]() ![]() Join Date: Dec 2007 Location: Hamburg, Germany
Posts: 230
![]() | DUDE, you still haven't called the "Calculator" function, which means, you won't have buttons. If you thought, that the concommand.Add would call one function after another, you're kind wrong. Read This for exact usage of the concommand function. And if you're handling with an Object called "Calculator" you can't really call a function with the same name. Lua-Engine would try to call the Object, what will result in an Error. So Corrected Code looks like this: Code: function OpenCalc( )
function FillCalculator( Panel )
local Op = vgui.Create( "DNumSlider", Panel )
Op:SetSize( 150, 50 )
Op:SetText( "Operator")
Op:SetMin( 1 )
Op:SetMax( 6 )
Op:SetDecimals( 0 )
Op:SetVisible( true )
local In1 = vgui.Create( "DTextEntry", Panel )
In1:SetPos( 20,25 )
In1:SetTall( 20 )
In1:SetWide( 450 )
In1:SetText( "Primary")
In1:SetEnterAllowed( true )
In1:SetVisible( false )
local In2 = vgui.Create( "DTextEntry", Panel )
In2:SetPos( 20,50 )
In2:SetTall( 20 )
In2:SetWide( 10 )
In2:SetText( "Secondary" )
In2:SetEnterAllowed( true )
In2:SetVisible( true )
local Out = vgui.Create( "DTextEntry", Panel )
Out:SetPos( 20,75 )
Out:SetTall( 20 )
Out:SetWide( 32 )
Out:SetEnterAllowed( false )
Out:SetVisible( true )
end
local Calculator = vgui.Create( "DFrame" ) -- Creates the frame itself
FillCalculator(Calculator)
Calculator:SetPos( 50,50 ) -- Position on the players screen
Calculator:SetSize( 400, 400 ) -- Size of the frame
Calculator:SetTitle( "Calculator" ) -- Title of the frame
Calculator:SetVisible( true )
Calculator:SetDraggable( true ) -- Draggable by mouse?
Calculator:ShowCloseButton( true ) -- Show the close button?
Calculator:MakePopup() -- Show the frame
end
concommand.Add( "OpenCalc", OpenCalc );
__________________ cmao = coding my ass off Free Fall Extension: 20081213 0002 Wire Socket Radio Rev2: 20081021 0932 (And it WORKS! (Mostly)) Please take a look into the Free Fall Extension and leave at least a small comment please, because it makes me kinda feel bad, if i put work into something, that nobody cares about. Understandable, isn't it? ![]() My IQ is 139 ![]() |
| | |
| | #8 (permalink) |
| <3 ![]() ![]() Join Date: Feb 2008 Location: London, ON (Canada, eh?)
Posts: 765
![]() ![]() | Wow I feel dumb now. *bangs head on desk* Thank you. ![]() ---Edit: I got these examples from Gmod.org wiki. They're really bad codes and doesn't explain alot. That's why I could never get this to work. I'm still learning
__________________ Signature: WIREMOD WILL NOT WORK ON A PIRATED GMOD! ![]() ![]() MY COLOUR IS BLUE. Post your expressions here: www.wiredexpressions.tk It's my favourite country song. And I hate it. IEF015's got his own website :3 Last edited by IEF015; 06-04-2008 at 08:41 AM.. |
| | |
| | #9 (permalink) | ||
| Flux's Assistant ![]() ![]() | You have to understand scope and process of execution. Unline other program languages, lua is PARSED, not executed. This means that it is all loaded (compiled) into memory, and executed by something else. The only time that a lua script acts as if it were executed (meaning ran from top to bottom) is when it is autoran, executed by another script, or via lua_run. Since this is not the case with your script, and requires a player to type in a command to activate functions within the script, you have to treat it as such. Therefore you need to encase the entire code which you want run, into a function scope. Encasing all of it in brackets {} is how it is done in lua. Also, I HIGHLY recommend you get used to using the "local" scope as much as possible. This prevents cluttering of the global environment, which causes conflicts and slowdowns. The best analogy I can give you of global and local scope is this: If you have two companies working on one program, each will probably keep copies of their own software and libraries to develop the program (keeping local, to their own companies). If they all ran off of the same hard drive, you will find that it will clash often, and run very slowly, as everyone is trying to install, save, run, etc, the same applications (aka globally). Imagine if someone tried to use windows notepad, and then someone else installed their own program, also called notepad. That is an example of globals clashing, and would produce unpredictable results. Typically you only need to use globals if you are trying to make something available to other scripts in which your script doesn't call, but is running in the same environment (Garry's Mod), like Resource Distribution's Resource Table. I am also not sure how it is done with lua, or if it is possible, but another usefulness for globals, is if you NEED a variable to go outside of function scope. However, this is very very bad programming practice, and should only be used if it is unavoidable.
__________________ Quote:
Quote:
Flux: wat Flux: whats wrong with the attachments Flux: WHY CANT YOU PEOPLE JUST POST A THREAD OR PM ME WHEN SOMETHING GOES WRONG Flux: I MIGHT CURSE YOU OUT BUT AT LEAST I KNOW ABOUT IT Anticept: Posting Anticept: Flux, PAY ATTENTION TO YOUR OWN DAMN FORUM. http://www.wiremod.com/5142-attachments-help.html Flux: ERAUHFSDHFUS Last edited by Anticept; 06-04-2008 at 09:26 AM.. | ||
| | |
| | #10 (permalink) |
| Lifetime Supporter ![]() Join Date: Aug 2007
Posts: 210
![]() | When you make a Frame (or DFrame whatever) it's generally useful to register that with VGUI and create child controls in the Init hook. Also concommand.Add only takes three parameters: name, function and auto-complete function. The last parameter is optional and doesn't matter in this case because your command doesn't need any options from the command line. To register a custom panel with VGUI you can use either the vgui.Register or vgui.RegisterTable functions and then to create your panel you would use vgui.Create or vgui.CreateFromTable respectively. This also yields the benefit of your panel being able to handle events without any hacky code! Personally I tend to use vgui.RegisterTable for stuff that I want internal to whatever I'm making. You might find the following Wiki entries useful: Writing panels in vgui library compatible form is fairly easy: Code: local PANEL = {} // This can be anything, but PANEL is a convention
function PANEL:Init ()
// Do stuff like create contols
end
function PANEL:Paint ()
// Custom drawing stuff
end
// ...any other hooks...
// Register with vgui
vgui.Register ("MyFrame", PANEL, "Frame")
// OR
local MyFrameTable = vgui.RegisterTable (PANEL, "Frame")
Code: function OpenMyFrame ()
// Assuming you used vgui.Register:
local pnl = vgui.Create ("MyFrame")
// If you used vgui.RegisterTable:
local pnl = vgui.CreateFromTable (MyFrameTable)
// Show the frame
pnl:MakePopup () // you could use pnl:SetVisible by itself
end
concommand.Add ("openmyframe", OpenMyFrame)
Your code could look something like this: Code: local PANEL = {}
// Position your frame and create your controls here
function PANEL:Init ()
// Set up the frame
self:SetPos (50, 50)
self:SetSize (400, 400)
self:SetTitle ("Calculator")
// Create child controls
self.Op = vgui.Create ("DNumSlider", self)
self.Op:SetSize (150, 50)
self.Op:SetText ("Operator")
self.Op:SetMin (1)
self.Op:SetMax (6)
self.Op:SetDecimals (0)
self.Op:SetVisible (true)
self.In1 = vgui.Create ("DTextEntry", self)
self.In1:SetPos (20, 25)
self.In1:SetTall (20)
self.In1:SetWide (450)
self.In1:SetText ("Primary")
self.In1:SetEnterAllowed (true)
self.In1:SetVisible (false)
self.In2 = vgui.Create ("DTextEntry", self)
self.In2:SetPos (20, 50)
self.In2:SetTall (20)
self.In2:SetWide (10)
self.In2:SetText ("Secondary")
self.In2:SetEnterAllowed (true)
self.In2:SetVisible (true)
self.Out = vgui.Create ("DTextEntry", Calculator)
self.Out:SetPos (20, 75)
self.Out:SetTall (20)
self.Out:SetWide (32)
self.Out:SetEnterAllowed (false)
self.Out:SetVisible (true)
end
// Register you panel with VGUI:
local IEF015Calculator = vgui.RegisterTable (PANEL, "DFrame")
// Console command
function OpenCalc ()
local Calculator = vgui.CreateFromTable (IEF015Calculator)
Calculator:MakePopup ()
end
concommand.Add ("OpenCalc", OpenCalc)
Hopefully this will be of some use to you, and anyone else starting out with VGUI. |
| | |
| Sponsored Links | |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|