- ComputerCraft | Programmable Computers for Minecraft
- → exploder's Content
exploder's Content
There have been 28 items by exploder (Search limited from 10-February 22)
#155436 Event queue
Posted by
exploder
on 07 December 2013 - 08:50 AM
in
Ask a Pro
I would like to ask a question: is there a way to queue event so I can use it after some loops and functions because I have a turtle that is mining and I need him to listen for rednet messages at the same time, so for example if the message is "test123" the turtle stops mining.
Computer code:
rednet.broadcast("test123")
Turtle code:
-- If I put os.pullEvent("rednet_message") here, it just stays at the spot and waits for the message.
for i=1,60 do
turtle.digDown()
turtle.down()
-- I need to check here if the message sent to turtle was test123 if it is, then stop.
end
I hope you will understand what I want to accomplish here because English is not my native language
#138971 Peripheral Api Issue - Turtle Upgrade Id 32767 Fails Silently?
Posted by
exploder
on 09 August 2013 - 04:08 AM
in
Bugs
Site: http://www.tutorials...c_datatypes.htm
#138970 Turtle Bug (1.6.2)
Posted by
exploder
on 09 August 2013 - 03:58 AM
in
Bugs
BTW, next time post your bug reports here: http://www.computerc.../forum/19-bugs/
#138282 How To Make Coroutines Run Together With Events?
Posted by
exploder
on 05 August 2013 - 11:39 AM
in
Ask a Pro
MysticT, on 05 August 2013 - 11:17 AM, said:
local clockTimer = os.startTimer(1) -- start a timer for 1 second -- Main loop while true do local evt, p1, p2, p3, p4, p5 = os.pullEvent() if evt == "timer" then if p1 == clockTimer then displayTime() -- redraw the clock clockTimer = os.startTimer(1) -- restart the timer end elseif evt == "some other event" then -- handle the event end end
When I tried that last time it didn't work (it wasn't reading mouse x and y positions, etc) but now it worked without errors. Thanks.
#138279 How To Make Coroutines Run Together With Events?
Posted by
exploder
on 05 August 2013 - 10:50 AM
in
Ask a Pro
GopherAtl, on 05 August 2013 - 10:13 AM, said:
displayTime displays the time, sleeps, and exits. Once it exits, it's coroutine is dead, and it will not run anymore, no matter how many times you try to resume() it.
Coroutines, just like programs, need to contain a loop if they're going to keep running indefinitely.
Then what would be the best way to make that function run every second without interrupting events?
svdragster, on 05 August 2013 - 10:18 AM, said:
Well, if I put loop inside there, makes no diffirence.
#138259 How To Make Coroutines Run Together With Events?
Posted by
exploder
on 05 August 2013 - 09:17 AM
in
Ask a Pro
Pastebin link: http://pastebin.com/YN6pE8gW -- The main loop is at line 122.
Thanks.
#90793 Word War
Posted by
exploder
on 24 February 2013 - 12:01 PM
in
Forum Games
#90595 Word War
Posted by
exploder
on 24 February 2013 - 12:59 AM
in
Forum Games
#90450 bios:338: [string "startup"]:27 'end' expected
Posted by
exploder
on 23 February 2013 - 09:52 AM
in
Ask a Pro
local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
-- [[ Variables ]] --
local scode = "1525"
local code = "8126"
local out = "back"
local time = 5
-- [[ Main Part ]] --
term.clear()
term.setCursorPos(1,1)
write("Please input code> ")
local input = read("*")
if input == code then
term.clear()
term.setCursorPos(1,1)
print("Correct code!")
rs.setOutput(out, true)
sleep(time)
rs.setOutput(out, false)
os.reboot()
elseif input == scode then
os.pullEvent = pullEvent
print("You can terminate the program now")
else
term.clear()
term.setCursorPos(1,1)
print("Incorrect code!")
sleep(2)
os.reboot()
end
And next time please format your code correctly, and use pastebin or code (<>) option in the editor.
#90446 Turtle not saving properly
Posted by
exploder
on 23 February 2013 - 09:41 AM
in
Ask a Pro
ChunLing, on 23 February 2013 - 09:01 AM, said:
Well, GPS is not the way to go, because I don't want to place 4 computers with wireless modems everywhere I place my turtle, so I guess I will just have to live with this saving system until they implement required things..
#90318 Turtle not saving properly
Posted by
exploder
on 23 February 2013 - 01:36 AM
in
Ask a Pro
ChunLing, on 22 February 2013 - 10:03 AM, said:
To get the best saving, you need to save right before and right after every move. And even then you'll still have some problems that can't really be solved until full programming persistence is implemented in CC (they really are working on that).
That's sad that I can't get super precise saving system then... OK, thanks.
#89988 Turtle not saving properly
Posted by
exploder
on 22 February 2013 - 03:25 AM
in
Ask a Pro
Here is when the bug occurs that if chunks get unloaded then sometimes it just stops and acts like it's finished, and sometimes it just keeps digging till it reaches bedrock and stops there (there was fuel inside).
So, I can't find the part, that's messing the whole thing up.
Pastebin link: http://pastebin.com/EeXAbRsN
Edit: Sometimes after relog it starts going forward on wrong side aswell.
#89978 Word War
Posted by
exploder
on 22 February 2013 - 03:11 AM
in
Forum Games
#81111 Customizable Door Lock
Posted by
exploder
on 28 January 2013 - 08:20 AM
in
Programs
I have made simple, but very customizable door lock, where you can:
- Set username/password which is auto-saved in computer.
- Set door side, where redstone signal will be sent to.
- Set "Debug" code, which allows user to access terminal.
Pastebin link:
http://pastebin.com/CVQWZpPJ
Disadvantages:
- Only Advanced Computers are currently supported, but support for colorless computers are coming.
- There is no auto-updater, but I plan on making one.
Installation:
See if HTTP API is enabled first, if not then enable it first.
If you don't know how to enable HTTP API then look at this CC WIKI page.
http://computercraft...itle=HTTP_(API)
After you have enabled HTTP API type in computer:
pastebin get CVQWZpPJ startup
and reboot your computer.
Screens:
Please leave me feedback, describing what are my code weaknesses, so I can improve it and learn few things as well.
#76321 [Lua] Load function from file
Posted by
exploder
on 18 January 2013 - 10:58 AM
in
Ask a Pro
KaoS, on 18 January 2013 - 10:45 AM, said:
function load(fileName)
file = fs.open("security/door/"..fileName,"r")
return (file.readLine() or "none"),file.close()
end
password = load("password")
I know it looks random and not even very useful but I find that many coders use file handles often and this helps keep your coded short
That might come in handy sometimes, thanks.
#76305 [Lua] Load function from file
Posted by
exploder
on 18 January 2013 - 10:40 AM
in
Ask a Pro
remiX, on 18 January 2013 - 06:19 AM, said:
function load(fileName)
file = fs.open("security/door/"..fileName,"r")
local line = file.readLine()
file.close()
return line or "none" -- will return none if the file is empty
end
--Call it like
password = load("password")
But I do see what you're trying to do... but the above should suffice?Works great, thank you.
#76183 [Lua] Load function from file
Posted by
exploder
on 18 January 2013 - 06:13 AM
in
Ask a Pro
So if I would like to load my password from file then I would write
load("password",password,pass)
print(pass)
But the computer will only print out empty space, so how can I fix this?
Code:
function load(name,tablename,data,)
file = fs.open("security/door/"..name,"r")
tablename = {} -- Table should be named password.
local line = file.readLine()
table.insert(tablename,line)
file.close()
data = tablename[1] -- Data should be named pass.
end
Thank you.
#66529 How to restrict the length of a word
Posted by
exploder
on 31 December 2012 - 03:10 AM
in
Ask a Pro
remiX, on 30 December 2012 - 05:20 AM, said:
I didn't have time to test it after taking it out of an old file of mine, but try it:
x, y = term.getSize() function limitRead(nLimit, replaceChar) term.setCursorBlink(true) local cX, cY = term.getCursorPos() local rString = "" if replaceChar == "" then replaceChar = nil end while true do local xPos, yPos = term.getCursorPos() local event, p1 = os.pullEvent() if event == "char" then -- Character event if #rString + 1 <= nLimit then rString = rString .. p1 if not replaceChar then if not nLimit then write(p1) else if #rString >= nLimit then term.setCursorPos(cX, cY) write(string.sub(rString, #rString - nLimit + 1)) elseif #rString < nLimit then write(p1) end end else if not nLimit then write(replaceChar) else if #rString >= nLimit then term.setCursorPos(cX, cY) write(string.rep(replaceChar, nLimit)) elseif #rString < nLimit then write(replaceChar) end end end end elseif event == "key" and p1 == keys.backspace then -- Backspace rString = string.sub(rString, 1, #rString-1) term.setCursorPos(cX, cY) term.clearLine() if replaceChar then for i = 1, #rString do write(replaceChar) end else write(rString) end elseif event == "key" and p1 == keys.enter then -- Enter break end end term.setCursorBlink(false) print() -- Skip to the next line after clicking enter. return rString end -- And you call it like this input = limitRead(10) -- If you want to replace it with a char, like read() then password = limitRead(10, "*")
Orwell, on 30 December 2012 - 05:43 AM, said:
local function readN(len, replaceChar)
len = len or 10
local input=""
local key = 0
term.setCursorBlink(true)
repeat
local e,p1 = os.pullEvent()
if e=="char" then
if #input < len then
input = input .. p1
term.write(replaceChar or p1)
end
elseif e=="key" and p1==keys.backspace and #input > 0 then
input = input:sub(1,#input-1)
local x,y = term.getCursorPos()
term.setCursorPos(x-1,y)
term.write(" ")
term.setCursorPos(x-1,y)
end
until p1==keys.enter
term.setCursorBlink(false)
return input
end
Edit: it's not the same as the previous ones except for remiX's. The main difference is that he writes the entire string on each iteration, while I handle each position seperately. Also, for unlimited length, you could just use math.huge or term.getSize() instead of adding seperate conditions. For replaceChar a simple 'or' is sufficient.
Both of them are working perfect, thanks everyone.
Edit: remiX you have a little problem in your code because if I press backspace no matter where it will erase everything that is behind the input.
Orwell's code doesn't have this bug.
#66076 How to restrict the length of a word
Posted by
exploder
on 30 December 2012 - 04:44 AM
in
Ask a Pro
remiX, on 30 December 2012 - 04:38 AM, said:
I have one somewhere on my pc, i'll try find it now, else I'll make a quick one
If you mean that it doesn't allow more than 10 (or more,less) chars to be imputed and doesn't allow any more than it would be perfect if you could post it.
Thanks.
#66073 How to restrict the length of a word
Posted by
exploder
on 30 December 2012 - 04:39 AM
in
Ask a Pro
grabie2, on 30 December 2012 - 04:24 AM, said:
here you have original read function
function read( _sReplaceChar, _tHistory )
term.setCursorBlink( true )
local sLine = ""
local nHistoryPos = nil
local nPos = 0
if _sReplaceChar then
_sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
end
local w, h = term.getSize()
local sx, sy = term.getCursorPos()
local function redraw( _sCustomReplaceChar )
local nScroll = 0
if sx + nPos >= w then
nScroll = (sx + nPos) - w
end
term.setCursorPos( sx, sy )
local sReplace = _sCustomReplaceChar or _sReplaceChar
if sReplace then
term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
else
term.write( string.sub( sLine, nScroll + 1 ) )
end
term.setCursorPos( sx + nPos - nScroll, sy )
end
while true do
local sEvent, param = os.pullEvent()
if sEvent == "char" then
sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
nPos = nPos + 1
redraw()
elseif sEvent == "key" then
if param == keys.enter then
-- Enter
break
elseif param == keys.left then
-- Left
if nPos > 0 then
nPos = nPos - 1
redraw()
end
elseif param == keys.right then
-- Right
if nPos < string.len(sLine) then
nPos = nPos + 1
redraw()
end
elseif param == keys.up or param == keys.down then
-- Up or down
if _tHistory then
redraw(" ");
if param == keys.up then
-- Up
if nHistoryPos == nil then
if #_tHistory > 0 then
nHistoryPos = #_tHistory
end
elseif nHistoryPos > 1 then
nHistoryPos = nHistoryPos - 1
end
else
-- Down
if nHistoryPos == #_tHistory then
nHistoryPos = nil
elseif nHistoryPos ~= nil then
nHistoryPos = nHistoryPos + 1
end
end
if nHistoryPos then
sLine = _tHistory[nHistoryPos]
nPos = string.len( sLine )
else
sLine = ""
nPos = 0
end
redraw()
end
elseif param == keys.backspace then
-- Backspace
if nPos > 0 then
redraw(" ");
sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
nPos = nPos - 1
redraw()
end
elseif param == keys.home then
-- Home
nPos = 0
redraw()
elseif param == keys.delete then
if nPos < string.len(sLine) then
redraw(" ");
sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
redraw()
end
elseif param == keys["end"] then
-- End
nPos = string.len(sLine)
redraw()
end
end
end
term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()
return sLine
end
This is a bit too complicated for me
GopherAtl, on 30 December 2012 - 04:26 AM, said:
function readLen(prompt, minLen,maxLen,isPassword)
while true do
write(prompt)
--//read the line; if isPassword is true, pass "*" as the char to display, otherwise nil so it displays what's typed
local sInput=read(isPassword and "*" or nil)
--//if the length is less than min or greater than max...
if #sInput<minLen or #sInput>maxLen then
--//...complain
print("Must be between "..minLen.." and "..maxLen.." characters long!")
else --//not too long or short, return it
return sInput
end
end
end
--//just call it instead of read, passing the prompt, min and max lengths, and optionally "true" if you're reading a password
local username=readLen("Enter username : ",1,16)
--//declare a var for the password before the loop
local password
--//quick loop
while true do
password=readLen("Enter password : ",4,16,true)
--//local declared inside loop, will go away when the loop ends
local pw2=readLen("Confirm password : ",4,16,true)
--//see if they match
if pw2==password then
break --//pws match, end the while loop
end
print("Passwords don't match! Try again")
end
:edit: or, you could modify the original read function grabie posted, if checking after and forcing re-entry bothers you.
This will do just fine and isn't too complicated, thanks.
- ComputerCraft | Programmable Computers for Minecraft
- → exploder's Content


