Jump to content


exploder's Content

There have been 28 items by exploder (Search limited from 10-February 22)


By content type

See this member's


Sort by                Order  

#155699 Event queue

Posted by exploder on 09 December 2013 - 04:25 PM in Ask a Pro

Got my turtle to do what I wanted, thanks everyone for posing and helping.



#155436 Event queue

Posted by exploder on 07 December 2013 - 08:50 AM in Ask a Pro

Hello there.

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 :P.



#139444 Peripheral Api Issue - Turtle Upgrade Id 32767 Fails Silently?

Posted by exploder on 11 August 2013 - 12:31 PM in Bugs

View Postimmibis, on 11 August 2013 - 07:38 AM, said:

Why would that be it? The maximum is 32767, so 32767 is still a valid value.

The minimum value is -32768, btw.

I though about that too but as I said in last post is that i'm not 100% sure.



#139078 Turtle Bug (1.6.2)

Posted by exploder on 09 August 2013 - 03:17 PM in Bugs

View PostXDjackieXD, on 09 August 2013 - 02:27 PM, said:

Maybe the directory of the save file is read-only? Looks like it...

I highly doubt it because nobody would set world directory read-only and then ask why it's not saving. Plus there is no reason to do so.



#138971 Peripheral Api Issue - Turtle Upgrade Id 32767 Fails Silently?

Posted by exploder on 09 August 2013 - 04:08 AM in Bugs

Well I guess the problem could be because 32767 is the max size of short data type in Java? If you look at this side then you'll see that short min and max values are exactly -32767 and 32767 (assuming that CC uses short data type for IDs). But I might not be 100% sure.

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

When you exit out of the server (local or online) the chunks get unloaded and all programs inside there are stopped. But this never causes files or resources to get deleted. Maybe you should install a fresh copy of Minecraft and CC because I'm currently writing a program that mines and saves it's positions so to test it I have to exit and reconnect to server several times and resources or programs have never disappeared so I guess it's only you.

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

View PostMysticT, on 05 August 2013 - 11:17 AM, said:

Use a timer instead of sleep and handle the "timer" event in the main loop. Something like this:
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

View PostGopherAtl, on 05 August 2013 - 10:13 AM, said:

coroutines aren't functions.

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?

View Postsvdragster, on 05 August 2013 - 10:18 AM, said:

You have to make a loop in the displayTime function too I guess

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

Hello. I have a problem that I can't solve. I'm trying to make a clock that would refresh every second and print time on screen but when I make coroutine that I put inside my main loop that has an os.pullEvent() then the function won't run every second but only once and that's it. Could you please tell what is the best way to resolve this problem?

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

World War III



#90595 Word War

Posted by exploder on 24 February 2013 - 12:59 AM in Forum Games

Person who designed the beam :D.



#90450 bios:338: [string "startup"]:27 'end' expected

Posted by exploder on 23 February 2013 - 09:52 AM in Ask a Pro

Lyquid already told you what to do, but in case you don't understand here is the fixed code, just copy%paste it.

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

View PostChunLing, on 23 February 2013 - 09:01 AM, said:

If you use GPS, then you don't need precise position saving. Then it's just a matter of tracking your program's general progress.

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

View PostChunLing, on 22 February 2013 - 10:03 AM, said:

Yeah...you perform a bunch of functions in between saves. That means that, depending on which function was last performed, your position saves end up inaccurate in different ways.

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.



#90095 Turtle not saving properly

Posted by exploder on 22 February 2013 - 09:59 AM in Ask a Pro

Anyone?



#89988 Turtle not saving properly

Posted by exploder on 22 February 2013 - 03:25 AM in Ask a Pro

I have written a program for mining turtle that saves turtles current position and if chunks get unloaded then loaded again (like if you disconnect/connect) it will continue doing it's work.

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

Shortage of water.



#81111 Customizable Door Lock

Posted by exploder on 28 January 2013 - 08:20 AM in Programs

Hello CC community,

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.
Generally the program is not that difficult, but it allows user to customize everything without touching the code even once.

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:
Spoiler


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

View PostKaoS, on 18 January 2013 - 10:45 AM, said:

I also find it useful to note that file.close() returns nil, allowing us to summarise the above into
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

View PostremiX, on 18 January 2013 - 06:19 AM, said:

Why insert into a table, what you could do is return what's in the file
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

Hi everyone. I want to make function that can get data from files, but I can't figure it out how to make one work.


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

View PostremiX, on 30 December 2012 - 05:20 AM, said:

Finally found it!

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, "*")

View PostOrwell, on 30 December 2012 - 05:43 AM, said:

I wrote something quickly myself, no moving with left and right arrow like the original read because I'm fealing lazy :P (could hardly bother anyone with 10 characters anyway). I think this might be quite the same as the previous snippets, but I focused on making it as understandable as possible.

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

View PostremiX, on 30 December 2012 - 04:38 AM, said:

All you do is make your own function with os.pullEvent and keep adding the inputted char to a line and if the line is over 10, it doesn't add any more.

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

View Postgrabie2, on 30 December 2012 - 04:24 AM, said:

you have to write custom read function that will not allow you to write if string length is more than specified value

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 :D but thanks anyways.


View PostGopherAtl, on 30 December 2012 - 04:26 AM, said:

read() doesn't support a max length, best you can do is check the length after, ex

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.



#66063 How to restrict the length of a word

Posted by exploder on 30 December 2012 - 04:12 AM in Ask a Pro

Hello.

I'm making a login screen where user needs to input username and password and I can't figure out, how to restrict the length that user is allowed to type?