Learning LUA.

Dear,

Today I decided to try to learn lua. I am familiair with PHP and I know the most basic code languages HTML and CSS. I’ ve been using this tutorial, and lua doesn’t seem to be too hard. Has anyone got a link for me on all the things that GLua adds to the traditional lua?

Kind regards,
Brem.

Dear Brem,

Please see references in this post:
http://forum.facepunch.com/showthread.php?t=1337945

Kind regards,
Luke.

Dear,

It says the following;

It’s a high-level scripting language yet they recommend to learn it if you’ve got zero programming skills? How comes?

Regards,
Brem

Dear Brem,

We don’t really worry about being too formal here, so don’t worry about using ‘Dear’, and ‘Kind regards’

Basically the language is similar to reading english , it’s very easy to read and interpret,

I don’t really know exactly why it says that but thats not to worry, I doubt you will have trouble learning, you can always add me on steam if you need help!

Kind-er regards,

Luke

Salutations Brem,

The term “high level” doesn’t refer to the requisite level of skill required to use the programming language. Rather, it refers more to how close a language is to being human readable. In this case, that statement is meant to assure you that the program should be relatively easy to understand for beginners.

Sincerely,
Lord Sasha

Okay, I’ll stop acting formal then :P. Since these are my very first few posts, I tought you guys wouldn’t be waiting for posts as “hey yo plz learn me LU4 i is want lern”.

Anyways, is this subforum only about Glua or Lua-learning in general aswell?

-edit-

I just now wrote my first small script, it may seem like nothing to you guys but I’m actually proud;



distance = 120
time = 2
velocity = distance / time
speedprint = print

speedprint("Car 1 is traveling at a speed of",velocity,"km per hour")

--The car keeps going faster by 5 km per hour, untill he reaches the maximum speed of 100

while velocity <= 100 do
	print("Car 1 drives",velocity,"km per hour")
	velocity = velocity + 5
end

-- 4 cars are driving at the speeds of 10,25,50,100
cars = {10, 25, 50, 100}

if cars <= 100 do
print("there are",#cars,"driving 100 km/ph or less")
end



the last functions doesn’t work, how do I make it so that it states how many cars are driving with a speed of 100 km/h or less?

Regards,
Brem

It’s quite hard to tell really, i would have thought that just learning lua, with no ties to garry’s mod would go into the programming section.

I was editting my old post but that took so long, maenwhile your answer showed up. Anyways, could you help me with the above question?

By the way, if you are going to use this code for gLua for an addon or something, put the ‘local’ prefix before each variable you are creating if you don’t want other code in the game to use it (if it’s ‘global’, which means that it can’t just be used in this script and another script checks or changes your variable thinking it’s one of it’s own, it might cause errors).

Also, if you only want something to repeat once, try using ‘then’ instead of ‘do’ (although I’m not even sure what ‘do’ would do in this case because I’ve never tried it.



local distance = 120
local time = 2
local velocity = distance / time


print("Car 1 is traveling at a speed of " .. velocity .. "km/h")


--The car keeps going faster by 5 km per hour, untill he reaches the maximum speed of 100


while velocity <= 100 do
    print("Car 1 is driving " .. velocity .. "km/h")
    velocity = velocity + 5
end


-- 4 cars are driving at the speeds of 10,25,50,100, 150
local cars = {10, 25, 50, 100, 150}


for k, v in pairs(cars) do
    if v > 100 then
        table.remove(cars, k)
    end


end


print("There are " .. #cars .. " cars driving 100 km/h or less")



Output:



Car 1 is traveling at a speed of 60.0km/h
Car 1 is driving 60.0km/h
Car 1 is driving 65.0km/h
Car 1 is driving 70.0km/h
Car 1 is driving 75.0km/h
Car 1 is driving 80.0km/h
Car 1 is driving 85.0km/h
Car 1 is driving 90.0km/h
Car 1 is driving 95.0km/h
Car 1 is driving 100.0km/h
There are 4 cars driving 100 km/h or less


Apologies for indentation, it was written in notepad.

Additional exercises
Using only ipairs, find the average height of the cats.

I’m at the end of my knowledge, how do I do this?

[lua]
cats = {10, 11, 12, 8, 100}

for i,v in ipairs(cats) do
print((v+v+v+v+V)/i)
end
[/lua]

that’s what I had but it didn’t work.



local sum = 0
for i,v in ipairs(cats) do
    sum=sum+v
end
print(sum/#cats)


Prolly a dumb question but why doesn’t this work;

[lua]

ja = 1

if ja == 1 then
print(“Jesse ja”)
end
[/lua]

Probably the dumbest answer ever but it should.

It gives no output in lua console…

Send the link of the lua console

I use the luai application, console gives no output.

it does work
(http://www.lua.org/cgi-bin/demo)

Just saying, if you’re gonna do some gmod lua, you’d probably want to take a look at this

pftt