This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
clu1's profile picture

Trying to make a "walkie Talkie"

Started by clu1, 08 October 2013 - 10:50 AM
clu1 #1
Posted 08 October 2013 - 12:50 PM
Trying to make a "walkie Talkie"

So I'm making a little program for me and my friends to use as a sort of secondary chat system… I have the basics set out, but I want it to keep the text until it receives the next input.


for k,v in pairs(rs.getSides()) do
		if (peripheral.getType(v) == "terminal_glasses_bridge") or (peripheral.getType(v) == "glassesbridge") then
				p = peripheral.wrap(v)
				break
		elseif (peripheral.getType(v) == "sensor") and (not worldSensor) then
				worldSensor = sensor.wrap(v)
				if not (worldSensor.getSensorName() == "worldCard") then
						worldSensor = false
				end
		end
end

while true do
  p.clear()
  --reads $$words and text
  local msg = os.pullEvent("chat_command")
	local wide = getStringLength(msg)

  --sets the message usable
  local say = msg
  p.addGradientBox(0, 0, wide+2, 10, 0xaaaaaa, 1, 0xffffff, .5, 1)

  --displays text
  p.addText(1, 2, say, 0x000000)

  os.sleep(5)

end

As of now, it takes 5 seconds for the message to be cleared, then you can put a new one in, but as stated above, that's not what I want. Anything helps, especially the function that waits for a chat command to do things.

Thanks, C1


P.S.I snagged the peripheral finding system form "Plastic Beta[WIP], along with anything else I didn't understand.
Bomb Bloke #2
Posted 08 October 2013 - 06:53 PM
'local msg = os.pullEvent("chat_command")' should already wait for a chat command, yes? If you put that above the clear line and remove the sleep line altogether, the program should draw a message and leave it there until the next message arrives.
clu1 #3
Posted 08 October 2013 - 08:04 PM
I tried that, but the p.clear() just erases teh text before it's displayed… I think I need to shuffle the code around a little, see what I can do…

Thanks for the reply though :)/>

I also want to figure out what method is used to get the length/width of a string to make it prettier :3
Bomb Bloke #4
Posted 08 October 2013 - 08:16 PM
'local wide = #msg' will do the trick there.

Though really you don't need to store that in a variable at all - since you're only using it once, there's no point in "saving it for later". Eg, this'd work fine:

p.addGradientBox(0, 0, #msg+2, 10, 0xaaaaaa, 1, 0xffffff, .5, 1)

Likewise, there's no point in copying "msg" into "say" unless you specifically want to be able to manipulate one copy of the message separately to the other.
Bubba #5
Posted 09 October 2013 - 10:04 AM
'local wide = #msg' will do the trick there.

No it won't. That gets the number of characters in the message, but the number of characters is not equal to the width of the string in OpenPeripheral glasses. However, Mikeemoo provides a handy function to help you out with that:

bridge.getStringWidth([input string])

There is a full list of openperipheral methods located here. However, I would advise you to be wary because the documentation has not been fully updated in some cases. In this one however, p.getStringWidth(msg) will work fine.