• Print

Author Topic: Learning Lua... (Just a random post from a lua super noob.)  (Read 27855 times)

0 Members and 1 Guest are viewing this topic.

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Learning Lua... (Just a random post from a lua super noob.)
« on: January 24, 2014, 08:53:36 am »
Code: [Select]
x = "Kenny"
if x == "Brandon" then
print("Hi, " .. x)
end
if x ~= "Brandon" then
print ("You are not Brandon, " .. x)
end

Code: [Select]
You are not Brandon, KennyI swear I started crying when this actually worked!
God I feel like I accomplished something this week:)
Lua Student

"The more you understand, the crazier you get."

Offline Decicus

  • Hero Member
  • *****
  • Posts: 552
  • Karma: 81
    • Alex Thomassen
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #1 on: January 24, 2014, 09:02:47 am »
While I understand you're a "Lua super noob", this would work as well, in the same way.
Code: [Select]
x = "Kenny"
if x == "Brandon" then
print("Hi, " .. x)
else
print ("You are not Brandon, " .. x)
end
It does the same exact thing as your code, but basically it does "Is 'X' Brandon? No. I'll print the 'else' code".

Other than that, you're doing good. I remember doing something that was similar in my earlier Lua days.
Contact information:
E-mail: alex@thomassen.xyz.
You can also send a PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #2 on: January 24, 2014, 10:24:22 am »
I used to do things like this in BASIC back in.... 1998. You're doing it right. Learn the basics. Learn how coding works. You'll pick up more advanced stuff in time.

Too many people just try to jump right in and start editing. Then they don't know what they're doing, only how to modify certain things.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #3 on: January 24, 2014, 03:35:29 pm »
I echo Decicus and MrP. We see so many people come through these forums asking us to teach them to do xyz task, by which they normally mean "do it for me". Good to see you're not getting frustrated by what may seem like small accomplishments.
Experiencing God's grace one day at a time.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #4 on: January 24, 2014, 09:01:48 pm »
Learning one language will make learning other languages easier.

Lua is a good place to start.
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #5 on: January 24, 2014, 09:52:42 pm »
Learning one language will make learning other languages easier.

Lua is a good place to start.
Agreed.
I started Lua when I was 12...
...then took up Java at 13, and blew threw it.
The only thing that irks me about C-based languages is that they're so complicated... *cough* PHP */cough*
Code: PHP
  1. echo("PHP Y U SO HARD? D:");
  2. die(":'(");
  3.  
The semicolons at the end of functions... just... why? :(
bw81@ulysses-forums ~ % whoami
Homepage

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #6 on: January 24, 2014, 10:24:00 pm »
Syn, said in another topic you wanted challenge.
Here you go.
Small one. Table included.
See if you can tell what it does/outputs before posting it into a lua interpreter (gmod included)
Code: [Select]
c = 0
x = { 0, 100, 20, 99 }
for n = x[1], x[3] do
   print ( n )
   c = n
end
print ("I just counted to ".. c )
« Last Edit: January 24, 2014, 10:25:37 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #7 on: January 24, 2014, 10:44:45 pm »
can i look at the reference manual?  :'(
Lua Student

"The more you understand, the crazier you get."

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #8 on: January 24, 2014, 10:48:14 pm »
Here's some more tables.

Can you figure out how this handles medals?

Code: [Select]
MEDAL[1] = { "Pasta Lover", "Obtain some yummy pasta.", 5 }
MEDAL[2] = { "Panty Thief", "Steal some underwear.", 10 }
MEDAL[3] = { "Pig Molester", "Bother a harmless pig.", 5 }
MEDAL[4] = { "Crisis", "Drop into crisis mode for the first time.", 5 }
MEDAL[5] = { "Error", "...", 20 }


local function Medalsvn
          print( "Medal: " .. MEDAL[3][1] )
          print( "Hint: " .. MEDAL[3][2] )
          print( MEDAL[3][3] )
          player.medalvalue = player.medalvalue + MEDAL[3][3]
end
Out of the Garry's Mod business.

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #9 on: January 24, 2014, 10:59:55 pm »
eyes hurt. hate tables. pig molester?
Lua Student

"The more you understand, the crazier you get."

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #10 on: January 24, 2014, 11:04:39 pm »
Want me to explain what's going on in mine?
Out of the Garry's Mod business.

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #11 on: January 24, 2014, 11:10:09 pm »
yes please neku:) I really don't like tables now
Lua Student

"The more you understand, the crazier you get."

Offline syn.

  • Jr. Member
  • **
  • Posts: 56
  • Karma: 6
  • Lua Padawan
    • SynGaming
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #12 on: January 24, 2014, 11:20:50 pm »
20! Jam it's 20! you freaking added 100 and 99 into the table for no reason... starts at 0 because c = 0 and then goes up by one all the way to 20 because you put x[3] and 20 is your 3rd variable (or whatever it's called) and n = c and blahblahblah. I want more jam pls:D

edit* : just checked on SciTE and I'm pretty sure I'm right.
Lua Student

"The more you understand, the crazier you get."

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #13 on: January 24, 2014, 11:30:50 pm »
Think of tables as variables inside of a variable.

I went a tad further and made tables within the table.

To call a table's value, you would do
Code: [Select]
table[x]

Say I wanted to call the point value of Pig Molester.
I would do
Code: [Select]
MEDAL[3][3] -- Another [] because it's inside another table.
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Learning Lua... (Just a random post from a lua super noob.)
« Reply #14 on: January 24, 2014, 11:33:02 pm »
20! Jam it's 20! you freaking added 100 and 99 into the table for no reason... starts at 0 because c = 0 and then goes up by one all the way to 20 because you put x[3] and 20 is your 3rd variable (or whatever it's called) and n = c and blahblahblah. I want more jam pls:D

edit* : just checked on SciTE and I'm pretty sure I'm right.
I think I might have one for you...
Simple, but weird if you don't know it all...
Code: [Select]
tab = {
"Billy",
"Bob",
"Joe"
}

math.randomseed( tonumber( tostring( os.time()):reverse():sub( 1, 6 ) ) )

print( "Winner: " .. tab[ math.random( 3 ) ] )
Yep, more tables. C:
I could've just posted my raffle name picker code, but its function namings and heavy commenting are too easily understandable... :p
EDIT: *crosses fingers and hopes I didn't screw anything up while typing on a lag guy as heck iPod tough 4G with autocorrect on*
EDIT 2: Fixed a few blatant syntax errors (probably added a few too) and added spacing.
EDIT 3: Fixed a blatant syntax error that I should've noticed but didn't due to my 20/100 uncorrected vision and my usage of a mobile device to make this post (I have switched to a computer since then :P).
EDIT 4: Tabs! :D
« Last Edit: January 25, 2014, 06:32:16 am by Princess Twilight Sparkle »
bw81@ulysses-forums ~ % whoami
Homepage

  • Print