Jump to content

LUA for Beginners (as easy as possible)


ViRuZGamiing

Recommended Posts

Hello community,

I'll be explaining the basics of LUA as I understand it.

CONTENT

Foreword

My background

Part 1 - Let's get started!

Part 2 - Variables (Extra: concat)

Foreword

First of all I wanna make some things clear, I'm not the best scripter in the world let be in LUA.

Second, I can write stuff here that some programmers will disagree that's why I am saying 'as I understand it'.

I'm going to try and make this understandable for the beginner starting from scratch.

My Background

My name is Billy, I'm from Belgium and I've been interested in LUA since I started MTA in 2011, I've tried many times to understand LUA and thanks to IIYAMA I've continued trying and never gave up, he taught me a lot but I never saw the entire picture, Why? Because I was to focussed on trying to make something I want instead of learning the language first.

I've seen C# in 1 school year and it learned me to understand and learn programming languages on my own.

Part 1 - Let's get started!

The first things we need to understand are datatypes in my opinion. In LUA there isn't that much to explain about datatypes since we don't need to declare them with our variable. BUT we need to make sure that variable data is of the same datatype. So we can't combine numbers and text we'd need to convert these numbers to plain text to fit in. (More about this later).

Part 2 - Variables

There are 2 types of variables, local and global.

Example:

local localVariable -- Here is 'localVariable' the name 
globalVariable -- Here is 'globalVariable' the name, ALSO we don't write global 

Now, what's the difference?

globalVariable = "Hello" 
  
function funcName () 
   local localVariable = "World" -- the name 'localVariable can only be used inside this function 
end 
  
outputChatBox(globalVariable) 
outputChatBox(localVariable) 
-- Result only globalVariable will be outputted, if we put the output inside the function local would work as well 

This would work,

globalVariable = "Hello" 
  
function funcName () 
   local localVariable = "World" -- the name 'localVariable can only be used inside this function 
   outputChatBox(localVariable) 
end 
  
outputChatBox(globalVariable) 

Try avoiding the use of global variables as much as possible!

Part 2 Extra - Concat

We can use both variables as plain text but to merge them we have to 'concat' them.

What is concat? Text example:

"Hello " "World" becomes "Hello World"

LUA Example:

local localVariable = "Hello" 
outputChatBox(localVariable.." World!") 

.. is concat you can see it as a + (Do not see it as a + for calculations but for adding text to eachother)

________________________________________________________________________________

If this is the end of the tutorial? That's what you guys decide, it took me some time for this and I am definitely not the best teacher but if you guys think I should continue I definitely will. I have a lot more to feature to finish the entire basics I hope this will be accepted as a good attempt to teaching.

Kind regards

Billy

  • Like 1
Link to comment

http://www.lua.org/about.html

What's in a name?

"Lua" (pronounced LOO-ah) means "Moon" in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun. More specifically, "Lua" is a name, the name of the Earth's moon and the name of the language. Like most names, it should be written in lower case with an initial capital, that is, "Lua". Please do not write it as "LUA", which is both ugly and confusing, because then it becomes an acronym with different meanings for different people. So, please, write "Lua" right!

The more you know.

Also, you should be using "global" scope variables more than "local" scope variables, there is a limit to how many "local" variables you can define per scope, this is because Lua searches in the local scope for variables first and then the global scope for variables when you use one in a statement.

Only use "local" for quick access variables and to hide variables from other scopes.

The performance for doing so will speak for itself.

Link to comment

Also, you should be using "global" scope variables more than "local" scope variables, there is a limit to how many "local" variables you can define per scope, this is because Lua searches in the local scope for variables first and then the global scope for variables when you use one in a statement.

Only use "local" for quick access variables and to hide variables from other scopes.

The performance for doing so will speak for itself.

I heard, the limit for local variables is about 200.

You can use the following approach to use even more local variables, if I am right.

local localv = {} 
  
localv.myVariable = 1 
localv.anotherVar = 123 
localy.thirdVariable = "Hello MTA!" 
--and so on... 

Link to comment

256, yeah you can do that as well as a work around, but that will create two searches for the variable you need.

(one in the global search and another in the table search)

It's just best to do as I previously stated, if you care about performance at all.

If you have the best computer imaginable 4+ core 4.0+ ghz you don't need to worry about it much.

Link to comment
256, yeah you can do that as well as a work around, but that will create two searches for the variable you need.

(one in the global search and another in the table search)

It's just best to do as I previously stated, if you care about performance at all.

If you have the best computer imaginable 4+ core 4.0+ ghz you don't need to worry about it much.

Ah, thanks for clarification. :)

On topic:

Don't take this wrong but this is a tutorial but not a beginner one.

Suggestions:

Explain what datatypes are and how many are there.

Give example codes of declaring and using datatypes.

Comment and explain things in each line of example code.

I do appreciate your intention though.

Link to comment
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...