Codex NG
Recurrent Flamer
- Joined
- Jul 24, 2015
- Messages
- 2,994
- Solutions
- 12
- Reaction score
- 1,657
I got a pm from someone recently, they had shown me a script they are trying to write, I gave them a brief opinion and they made the changes then they showed me the code and I have not responded.
It's not just enough when creating new concepts or systems for your server to just copy and paste code, you need to understand what you are looking at, this is why i tell people to learn the language of the enviorment they are working in.
When I 1st started learning about code years ago, I would use things in the real world to help me identify things I was reading about, because they were abstract and confusing.
Its not just enough to know the language you are working in, you also have to apply it, this is how you learn programming logic this is how you learn to code.
When you write a program/script you are testing the possible outcomes whether they are for the benefit of your idea or the exclusion of things you don't want.
Also when you write a program/script since your idea is basic so should your code be.
1st thing is 1st, we setup a basic enviorment to work in.
Lets use a movements script for this example, I will set up the basic functionality of the script, this way I know it works as intended.
Now I analyze the core script to determine how it works.
I say to myself I know creature can be anything, it can be a player, monster or npc.
item can be a tile or movable item I will be stepping on, how do I know this?
Because of the name of the interface "on step in" gives me this hint.
The position is the place i am attempting to step in.
And finally fromPosition is where I am coming from.
The return true at the end of the script tells the server that everything worked as intended and to end the script.
Now that we have a general idea on how the script works we can start working on the script itself.
For this example we will just allow the player to pass if they meet a level requirement.
Since creature can be anything we don't need to re-create it, all we need to do is check if creature is a player.
We simply do this by using the metamethod isPlayer.
Since our script is going to be designed for the actions of a player and not anything else we put a not in front of creature:isPlayer()
Lets say a cyclops walks on the item, is a cyclops a player? No of course it isn't.. so this condition creature:isPlayer() will return false, the not will take the false value and make it true which will execute the block of code inside of the if statement.
If creature is a player then the condition will return true and the not will turn that true value into false skipping over this if statement.
Now that we have set some basic prevention in place, the rest of our code will be focused on the player.
Next thing we need to do is get the level of the player and store the results, we will do this using a local variable labeled level.
Now we need to define a number to compare the player level to, so lets do that now.
Does it matter where I define minimumLevel? for this example it does not, however I like to get into the habbit of setting up my definitions outside of the interface, it helps to keep the code clean and easy to read/manage.
minimumLevel although is defined as local it's scope is actually global to the script (for the most part), for more on scope please refer to a lua tutorial.
Now that we have a variable to get the player's level and the minimumLevel needed, we create another statement to compare the two values.
This aspect of the script will check to see if level is greater than or equal (>=) to minimumLevel, if it is then execute the code inside of the if statement.
Send them to the position.
Else the player does not meet the minimumLevel then send them back to the position they came from.. e.g. fromPosition
This script could be simplified, however this was not the purpose of this example, it was to show that when writing a script you need to understand the parameters of the interface, who/what the target is, preventive measures and most of all the language.
It's not just enough when creating new concepts or systems for your server to just copy and paste code, you need to understand what you are looking at, this is why i tell people to learn the language of the enviorment they are working in.
When I 1st started learning about code years ago, I would use things in the real world to help me identify things I was reading about, because they were abstract and confusing.
Its not just enough to know the language you are working in, you also have to apply it, this is how you learn programming logic this is how you learn to code.
When you write a program/script you are testing the possible outcomes whether they are for the benefit of your idea or the exclusion of things you don't want.
Also when you write a program/script since your idea is basic so should your code be.
1st thing is 1st, we setup a basic enviorment to work in.
Lets use a movements script for this example, I will set up the basic functionality of the script, this way I know it works as intended.
Code:
function onStepIn(creature, item, position, fromPosition)
return true
end
Now I analyze the core script to determine how it works.
I say to myself I know creature can be anything, it can be a player, monster or npc.
item can be a tile or movable item I will be stepping on, how do I know this?
Because of the name of the interface "on step in" gives me this hint.
The position is the place i am attempting to step in.
And finally fromPosition is where I am coming from.
The return true at the end of the script tells the server that everything worked as intended and to end the script.
Now that we have a general idea on how the script works we can start working on the script itself.
For this example we will just allow the player to pass if they meet a level requirement.
Since creature can be anything we don't need to re-create it, all we need to do is check if creature is a player.
We simply do this by using the metamethod isPlayer.
Code:
function onStepIn(creature, item, position, fromPosition)
if creature:isPlayer() then
end
return true
end
Since our script is going to be designed for the actions of a player and not anything else we put a not in front of creature:isPlayer()
Code:
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
end
return true
end
Lets say a cyclops walks on the item, is a cyclops a player? No of course it isn't.. so this condition creature:isPlayer() will return false, the not will take the false value and make it true which will execute the block of code inside of the if statement.
Code:
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
-- execute this code inside the if statement if creature is not a player
return true -- end the script
end
return true
end
If creature is a player then the condition will return true and the not will turn that true value into false skipping over this if statement.
Now that we have set some basic prevention in place, the rest of our code will be focused on the player.
Next thing we need to do is get the level of the player and store the results, we will do this using a local variable labeled level.
Code:
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
-- execute this code inside the if statement if creature is not a player
return true -- end the script
end
local level = creature:getLevel()
return true
end
Now we need to define a number to compare the player level to, so lets do that now.
Code:
local minimumLevel = 10
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
return true
end
local level = creature:getLevel()
return true
end
Does it matter where I define minimumLevel? for this example it does not, however I like to get into the habbit of setting up my definitions outside of the interface, it helps to keep the code clean and easy to read/manage.
minimumLevel although is defined as local it's scope is actually global to the script (for the most part), for more on scope please refer to a lua tutorial.
Now that we have a variable to get the player's level and the minimumLevel needed, we create another statement to compare the two values.
Code:
local minimumLevel = 10
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
return true
end
local level = creature:getLevel()
if level >= minimumLevel then
end
return true
end
This aspect of the script will check to see if level is greater than or equal (>=) to minimumLevel, if it is then execute the code inside of the if statement.
Send them to the position.
Code:
local minimumLevel = 10
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
return true
end
local level = creature:getLevel()
if level >= minimumLevel then
creature:teleportTo(position)
end
return true
end
Else the player does not meet the minimumLevel then send them back to the position they came from.. e.g. fromPosition
Code:
local minimumLevel = 10
function onStepIn(creature, item, position, fromPosition)
if not creature:isPlayer() then
return true
end
local level = creature:getLevel()
if level >= minimumLevel then
creature:teleportTo(position)
else
creature:teleportTo(fromPosition)
end
return true
end
This script could be simplified, however this was not the purpose of this example, it was to show that when writing a script you need to understand the parameters of the interface, who/what the target is, preventive measures and most of all the language.