Codex NG
Recurrent Flamer
- Joined
- Jul 24, 2015
- Messages
- 2,994
- Solutions
- 12
- Reaction score
- 1,657
Before we start building anything in any language,
It is important to have a clear understanding of the language itself
Comments, Naming Convention, Structure:It is important to have a clear understanding of the language itself
Comments in every programming language allow the programmer and the non-programmers to have a general idea of what is going on in the code, especially when that code becomes excessively large and or complex.
There are 2 types of comments in lua
Single Line Comment:
-- This is a single line comment, everything til the end of the line will be commented out & ignored by the lua interpreter
Multi-Line Comment:
--[[ This is a multi-line comment, everything that is contained within the --[[ ]] is ignored by the lua interpreter ]]
Technically speaking there is an unwritten 3rd type of comment in every language, which is called the naming convention although naming convention falls under a consistent style of program (e.g. nameOfPlayer, nam_of_player etc..) it can also be used as a comment style of programming.
Lets look at an example:
This piece of code, will run and execute as expected.
Although its functional, it isn't very readable which is a problem because we have no idea what is going on in this script.
Code:
local s = 18000 local x = { p = 0.50, f = 0.10}
function onEquip(cid, item, slot)
local b = (isPremium(cid)) and x['p'] or x['f'] local r = g(cid, true) + b
if getPlayerStorageValue(cid, s) == -1 then setPlayerStorageValue(cid, s, 1) doPlayerSetExperiencer(cid, r) doPlayerSendTextMessage(cid, 22, "You have gained "..(b * 100).."% b exp!")
if not isPremium(cid) then doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive "..(x['p'] * 100).."% instead of "..(x['f'] * 100).."% as a free account while using this ring.") end
doTransformItem(item.uid, 7697, 1) end return true end
function onDeEquip(cid, item, slot) local b = (isPremium(cid)) and x['p'] or x['f'] local r = ((g(cid, true) - b) <= 1) and 1 or g(cid, true) - b
if getPlayerStorageValue(cid, s) == 1 then doPlayerSetExperiencer(cid, r) setPlayerStorageValue(cid, s, -1) doTransformItem(item.uid, 7708, 1) doPlayerSendTextMessage(cid, 22, "The bounus xp has ended.") end return true end
function g(cid, s) if s then return getExperienceStage(getPlayerLevel(cid), getVocationInfo(getPlayerVocation(cid)).experienceMultiplier)end return getConfigInfo('rateExperience') end
Code:
local s = 18000 local x = { p = 0.50, f = 0.10}
It is not very clear and it would take a bit time to go through the code to see what these things are used for.
So lets rename these letters so that they describe their purpose
Code:
local xpStorage = 18000 local xpToAdd = { premacc = 0.50, freeacc = 0.10}
Structure is also an important part of readability it allows us to see the level of execution, find bugs and make our less then savy code seem a bit sexy When we talk about structure we are referring to proper spacing & indentation of the code.
So with the above in mind I will show the original code using comments, a naming convention & structure.
Code:
local xpStorage = 18000 -- change this to a storage value not used
local xpToAdd = {
premacc = 0.50, -- premium account bonus xp
freeacc = 0.10 -- free account bonus xp
}
function onEquip(cid, item, slot)
local bonus = (isPremium(cid)) and xpToAdd['premacc'] or xpToAdd['freeacc']
local rate = getPlayerExpRate(cid, true) + bonus
if getPlayerStorageValue(cid, xpStorage) == -1 then
setPlayerStorageValue(cid, xpStorage, 1)
doPlayerSetExperienceRate(cid, rate)
doPlayerSendTextMessage(cid, 22, "You have gained "..(bonus * 100).."% bonus exp!")
if not isPremium(cid) then
doPlayerSendTextMessage(cid,21,"If you were a premium player you would receive "..(xpToAdd['premacc'] * 100).."% instead of "..(xpToAdd['freeacc'] * 100).."% as a free account while using this ring.")
end
doTransformItem(item.uid, 7697, 1)
end
return true
end
function onDeEquip(cid, item, slot)
local bonus = (isPremium(cid)) and xpToAdd['premacc'] or xpToAdd['freeacc']
local rate = ((getPlayerExpRate(cid, true) - bonus) <= 1) and 1 or getPlayerExpRate(cid, true) - bonus
if getPlayerStorageValue(cid, xpStorage) == 1 then
doPlayerSetExperienceRate(cid, rate)
setPlayerStorageValue(cid, xpStorage, -1)
doTransformItem(item.uid, 7708, 1)
doPlayerSendTextMessage(cid, 22, "Bonus xp has ended.")
end
return true
end
function getPlayerExpRate(cid, stages)
if stages then
return getExperienceStage(getPlayerLevel(cid), getVocationInfo(getPlayerVocation(cid)).experienceMultiplier)
end
return getConfigInfo('rateExperience')
end
Declaration:
Variable:
A variable is simply a container used to hold a value, think of a variable as a box where you can only hold 1 thing in that box.
A variable must start with a letter or underscore they can be 1 character to the entire alphabet in length, variables can contain numbers, letters or underscores but absolutely no spaces, variables are case sensative meaning wow, WoW & wOw are all different variables.
The way we declare a variable is by typing the variable's name followed by an equals sign ( = ) and the value you want to store in the variable, the equal sign is known as the assignment operator.
Code:
local myVariable = 5 -- a variable with a default value of 5
If we wanted to access the variables value and assign it to a new variable or just print it to the console we could write
Code:
local myNewVariable = myVariable -- store the value of myVariable in myNewVariable now both variables hold the value of 5
or
print(myVariable) -- this will print 5 in the console window
A table is similar to a variable but instead of just holding 1 value it can hold multiple values & even other tables.
Code:
local myEmptyTable = {} -- this is an empty table
Code:
local myTable = {
name = 'Codex'
}
Example:
Code:
local myName = myTable.name -- assign myTable.name to myName
Code:
myEmptyTable.name = myName
Tables can also be assigned a property based on its index value, think of a table as a bookshelf and its index value as one of its shelves.
Lets create a new table
Code:
local myNewEmptyTable = {}
myNewEmptyTable[1] = 10
This also could have been done during its declaration
Code:
local myNewEmptyTable = {
10 -- index 1
}
Code:
local table_ = {
[0] = 23, -- index 0
27, -- index 1
89 -- index 2
}
local table_ = {
[0] = 23, -- index 0
[5] = 27, -- index 5
[100] = 89 -- index 100
}
Tables can also be assigned tables as properties
It is important to note that if you have more than 1 property in a table that you place a comma ( , ) prior to the last property listed.
Code:
local myNewEmptyTable = {
10, -- index 1
innerEmptyTable = {} -- index 2
}
Add a property
Code:
myNewEmptyTable.innerEmptyTable.someProperty = 34
Code:
local x = myNewEmptyTable.innerEmptyTable.someProperty
Last edited: