Posted 16 May 2013 - 06:53 PM
Table of contents: [anchor='Top']
Top
Table declaration
smartTimer explanation
smartPullEvent explanation
Example
Here is how it all began:
First, I began work on a mob farm controller that was limited to a certain amount of time, and needed to check for monitor_touch, char,timer, and player (MiscPeripherals). Here is (the timer only version of) what I came up with:
Creating the timer:
Catching the timer:
As you see in 'Catching the timer', it relies on tables. Here's an example:
When you put the code together, you get this:
You may be thinking "I don't understand this!"
Let me explain and give an example:
This declares the look-up table and the table to store id's [anchor='Table']
After deciphering, the program will return '5' (String form, no quotes)
local tTimers = {}
The timer id's and what the id's represent will be stored here
Now, lets look at that smartTimer function. Its quite simple if you know tables [anchor='Timer']
at position sID (Variable representing an Integer, no quotes).
The table would look like:
Now, lets look at what happens during the code to use this timer event [anchor='Event']
ID is equal to '1' (example)
tVal[ID] is equal to '2^5' (The timer passed with '2^5')
tMain[tVal[ID]] is equal to '5' (Integer form, no quotes, timer passed with '2^5')
then, the function returns:
"timer", "5" (Both strings)
Let's see an example with this. [anchor='Example']
Creating a program with these contents
I hope you have learned something and found this tutorial helpful.
Please post any and all comments, concerns, considerations, and questions.
Top
Table declaration
smartTimer explanation
smartPullEvent explanation
Example
Here is how it all began:
First, I began work on a mob farm controller that was limited to a certain amount of time, and needed to check for monitor_touch, char,timer, and player (MiscPeripherals). Here is (the timer only version of) what I came up with:
Creating the timer:
function smartTimer(sTime, sRep)
sID = os.startTimer(sTime)
table.insert(tTimers, sID, sRep)
end
Catching the timer:
function smartPullEvent(tMain, tVal)
local toRet = nil
local ID
event, ID = os.pullEvent("timer")
toRet = tMain[tVal[ID]]
return event, toRet
end
As you see in 'Catching the timer', it relies on tables. Here's an example:
tDecipher = {
[2^1] = "1",
[2^2] = "2",
[2^3] = "3",
[2^4] = "4",
[2^5] = "0",
[2^6] = "6",
[2^7] = "7",
[2^8] = "8",
[2^9] = "9",
}
local tTimers = {}
Remember to declare the tables before you use them!When you put the code together, you get this:
tDecipher = {
[2^1] = "1",
[2^2] = "2",
[2^3] = "3",
[2^4] = "4",
[2^5] = "0",
[2^6] = "6",
[2^7] = "7",
[2^8] = "8",
[2^9] = "9",
}
local tTimers = {}
function smartTimer(sTime, sRep)
sID = os.startTimer(sTime)
table.insert(tTimers, sID, sRep)
end
function smartPullEvent(tMain, tVal)
local toRet = nil
local ID
event, ID = os.pullEvent("timer")
toRet = tMain[tVal[ID]]
return event, toRet
end
You may be thinking "I don't understand this!"
Let me explain and give an example:
This declares the look-up table and the table to store id's [anchor='Table']
tDecipher = {
[2^1] = "1",
[2^2] = "2",
[2^3] = "3",
[2^4] = "4",
[2^5] = "0",
[2^6] = "6",
[2^7] = "7",
[2^8] = "8",
[2^9] = "9",
}
Lets say the timer with an ID of 5 is linked in the table with '2^5' (Integer form, no quotes)After deciphering, the program will return '5' (String form, no quotes)
local tTimers = {}
The timer id's and what the id's represent will be stored here
Now, lets look at that smartTimer function. Its quite simple if you know tables [anchor='Timer']
function smartTimer(sTime, sRep)
sID = os.startTimer(sTime)
-- # Gets the ID of the timer when its created, then...
table.insert(tTimers, sID, sRep)
end
Stores Variable sRep (Variable form, no quotes) in table 'tTimers' (Table, no quotes)at position sID (Variable representing an Integer, no quotes).
The table would look like:
tTimers = {[sID] = Srep}
or, passing 2^5, thistTimers = {[1] = 2^5}
Now, lets look at what happens during the code to use this timer event [anchor='Event']
function smartPullEvent(tMain, tVal)
local toRet = nil
local ID
-- # Declares two variables usable in only this function
event, ID = os.pullEvent("timer")
-- # Waits for the "timer" event to be pulled
toRet = tMain[tVal[ID]]
return event, toRet
-- # explained below
end
Now, lets look at
toRet = tMain[tVal[ID]]
return event, toRet
First,ID is equal to '1' (example)
tVal[ID] is equal to '2^5' (The timer passed with '2^5')
tMain[tVal[ID]] is equal to '5' (Integer form, no quotes, timer passed with '2^5')
then, the function returns:
"timer", "5" (Both strings)
Let's see an example with this. [anchor='Example']
Creating a program with these contents
tDecipher = {
[2^1] = "1",
[2^2] = "2",
[2^3] = "3",
[2^4] = "4",
[2^5] = "0",
[2^6] = "6",
[2^7] = "7",
[2^8] = "8",
[2^9] = "9",
}
local tTimers = {}
function smartTimer(sTime, sRep)
sID = os.startTimer(sTime)
table.insert(tTimers, sID, sRep)
end
function smartPullEvent(tMain, tVal)
local toRet = nil
local ID
event, ID = os.pullEvent("timer")
toRet = tMain[tVal[ID]]
return event, toRet
end
smartTimer(10, 2^8)
e, string1 = smartPullEvent(tDecipher, tTimers)
print(string1)
will make the program print '8' (String form, no quotes)I hope you have learned something and found this tutorial helpful.
Please post any and all comments, concerns, considerations, and questions.
Edited on 16 May 2013 - 04:59 PM