Old 06-01-2008   #1 (permalink)
<3

 
IEF015's Avatar
 

Join Date: Feb 2008
Location: London, ON (Canada, eh?)
Posts: 765
IEF015 has a spectacular aura aboutIEF015 has a spectacular aura about
Default Help me with the basix!

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 );
Is what I have so far. I think I can get the code for the text and stuff.

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
IEF015 is online now  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Old 06-02-2008   #2 (permalink)
UWSVN Contributor


 
Free Fall's Avatar
 

Join Date: Dec 2007
Location: Hamburg, Germany
Posts: 230
Free Fall is on a distinguished road
Default Re: Help me with the basix!

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

Free Fall is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-02-2008   #3 (permalink)
<3

 
IEF015's Avatar
 

Join Date: Feb 2008
Location: London, ON (Canada, eh?)
Posts: 765
IEF015 has a spectacular aura aboutIEF015 has a spectacular aura about
Default Re: Help me with the basix!

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
IEF015 is online now  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-02-2008   #4 (permalink)
<3

 
IEF015's Avatar
 

Join Date: Feb 2008
Location: London, ON (Canada, eh?)
Posts: 765
IEF015 has a spectacular aura aboutIEF015 has a spectacular aura about
Default Re: Help me with the basix!

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 );
Am I getting closer? 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
IEF015 is online now  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-03-2008   #5 (permalink)
Newbie
 
Magnight's Avatar
 

Join Date: Feb 2008
Location: 30*N 10*W
Posts: 28
Magnight is on a distinguished road
Default Re: Help me with the basix!

it would probably help to tell us what error message comes up in the console=)
__________________
Hitman217:
Quote:
Originally Posted by Hitman271 View Post
Ok guys, here's my fix for the latest wireos...

Code:
mov eax,GO LEARN CPU FOR FUCKS SAKE. NO ONE ANSWERS YOUR QUESTIONS HERE
AzraelUK:
Quote:
Originally Posted by AzraelUK View Post
If you don't have anything to say that's helpful, useful, or even vaguely witty, then why reply?
Magnight is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-03-2008   #6 (permalink)
<3

 
IEF015's Avatar
 

Join Date: Feb 2008
Location: London, ON (Canada, eh?)
Posts: 765
IEF015 has a spectacular aura aboutIEF015 has a spectacular aura about
Default Re: Help me with the basix!

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..
IEF015 is online now  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-04-2008   #7 (permalink)
UWSVN Contributor


 
Free Fall's Avatar
 

Join Date: Dec 2007
Location: Hamburg, Germany
Posts: 230
Free Fall is on a distinguished road
Default Re: Help me with the basix!

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 );
PS: You should work on the Size-values. right now they aren't any good
Attached Images
File Type: jpg OpenCalc.jpg (10.7 KB, 38 views)
__________________
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

Free Fall is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-04-2008   #8 (permalink)
<3

 
IEF015's Avatar
 

Join Date: Feb 2008
Location: London, ON (Canada, eh?)
Posts: 765
IEF015 has a spectacular aura aboutIEF015 has a spectacular aura about
Default Re: Help me with the basix!

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..
IEF015 is online now  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-04-2008   #9 (permalink)
Flux's Assistant

 
Anticept's Avatar
 

Join Date: Feb 2008
Location: Ohio, USA
Posts: 406
Anticept will become famous soon enough
Send a message via AIM to Anticept Send a message via MSN to Anticept Send a message via Yahoo to Anticept Send a message via Skype™ to Anticept
Default Re: Help me with the basix!

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:
Originally Posted by tad2020
TAD2ô2ô says: on the logical scale on 1 to 10
TAD2ô2ô says: it was a basket of napalm flavored kittens
Quote:
Originally Posted by tad2020
<@TAD2020> and flux has a kryptonite penis! my asbestos pants is no match to that monster!
Anticept: Flux, fix the attachments :P
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..
Anticept is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 06-23-2008   #10 (permalink)
Lifetime Supporter
 
DuFace's Avatar
 

Join Date: Aug 2007
Posts: 210
DuFace is on a distinguished road
Default Hate to bump an old thread but...

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")
As is creating them:
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)
For Derma help I suggest starting at the source (haha, forgive me for the awful pun) over here. I've been working on some custom VGUI lately and this has helped immensely.


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)
Derma deletes your frame when it's closed, so the above code wont cause any memory problems. If Frame was used instead of DFrame you would need to include code that would only create it once and then use SetVisible to show it on all subsequent console command executions.

Hopefully this will be of some use to you, and anyone else starting out with VGUI.
DuFace is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -7. The time now is 12:09 AM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.

Page top