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

Quick question...

Started by kennyist, 13 December 2012 - 02:59 AM
kennyist #1
Posted 13 December 2012 - 03:59 AM
I have looked in the wiki ETC and havnt found any help.

Im trying to add an ID from a rednet message to an end of a string, How would i do this? I've tried:


green = green
red = red

function signal(id, msg, x, y)
tempg = green,id
tempr = red,id


function signal(id, msg, x, y)
tempg = "green",id
tempr = "red",id


green = green
red = red

function signal(id, msg, x, y)
tempg = green..id
tempr = red..id



function signal(id, msg, x, y)
tempg = "green"..id
tempr = "red"..id
PixelToast #2
Posted 13 December 2012 - 04:10 AM
you concat it .-.

you were doing it correctly in the 4th example, you just forgot an end:


function signal(id, msg, x, y)
tempg = "green"..id
tempr = "red"..id
end

i would suggest you look at / play around with other peoples code to learn things like this .-.
you might also want to read teh manual you can ignore the c implementations
kennyist #3
Posted 13 December 2012 - 04:12 AM
you concat it .-.

you were doing it correctly in the 4th example, you just forgot an end:


function signal(id, msg, x, y)
tempg = "green"..id
tempr = "red"..id
end

This does not work, there is an end, this is the full code:


rednet.open("left")
red3 = paintutils.loadImage("red")
green3 = paintutils.loadImage("green")
green = green
red = red

term.clear()
paintutils.drawLine(2, 10, 50, 10, 128)

function drawLines()
  term.clear()
  term.setCursorPos(1,1)
  paintutils.drawLine(2, 10, 50, 10, 128)
end

function signal(id, msg, x, y)
tempg = "green"..id
tempr = "red"..id
  if msg == "green" then
    paintutils.drawImage(tempg, x, y)
  else
    paintutils.drawImage(tempr, x, y)
  end
end

while true do
  event, id, msg, dist = os.pullEvent("rednet_message")
    if id == 3 then
	 x = 25
	 y = 9
	 signal(id, msg, x, y)
   else
	 drawLines()
  end
end

I get error: paintutils:94: attempt to get length of nil
Edited on 13 December 2012 - 03:13 AM
PixelToast #4
Posted 13 December 2012 - 04:20 AM
.-. verry verry messy code

anyway, the cause of your error is that you are trying to draw a string on line 20 and 22
get the variable of a string using:
_G[string]
ex:
local str="print"
_G[str](stuff)
or in this case:
paintutils.drawImage(_G[tempg], x, y)
and
paintutils.drawImage(_G[tempr], x, y)
kennyist #5
Posted 13 December 2012 - 04:30 AM
.-. verry verry messy code

anyway, the cause of your error is that you are trying to draw a string on line 20 and 22
get the variable of a string using:
_G[string]
ex:
local str="print"
_G[str](stuff)
or in this case:
paintutils.drawImage(_G[tempg], x, y)
and
paintutils.drawImage(_G[tempr], x, y)

If you ment it like this, I still get the same error (except :92: instead of :94:)


-- images
red3 = paintutils.loadImage("red")
green3 = paintutils.loadImage("green")
--

function drawLines()
  term.clear()
  term.setCursorPos(1,1)
  paintutils.drawLine(2, 10, 50, 10, 128)
end

function signal(id, msg, x, y)
tempg = "green"..id
tempr = "red"..id
if msg == "green" then
  paintutils.drawImage(_G[tempg], x, y)
else
  paintutils.drawImage(_G[tempr], x, y)
end
end

rednet.open("left")
drawLines()

while true do
  event, id, msg, dist = os.pullEvent("rednet_message")
  if id == 3 then
x = 25
y = 9
signal(id, msg, x, y)
  else
	drawLines()
  end
end
PixelToast #6
Posted 13 December 2012 - 05:07 AM
the only thing i can think of is that the images red and green dont exist
or the sender id isnt 3
kennyist #7
Posted 13 December 2012 - 06:09 AM
Just added a write command and it is showing "green3"

function signal(id, msg, x, y)
tempg = "green"..id
tempr = "red"..id
write(tempg)



And the images where showing before i tried to attach an ID instead of just adding green3 into the draw command.
huettner94 #8
Posted 13 December 2012 - 08:15 AM
If you just connect a String and a Number the program won't recognize that as an object with that name, it will be just a normal String.
You probably want to do something like this:

rednet.open("left")
red = {}
green = {}
red[3] = paintutils.loadImage("red")
green[3] = paintutils.loadImage("green")
term.clear()
paintutils.drawLine(2, 10, 50, 10, 128)

function drawLines()
  term.clear()
  term.setCursorPos(1,1)
  paintutils.drawLine(2, 10, 50, 10, 128)
end

function signal(id, msg, x, y)
  if msg == "green" then
	paintutils.drawImage(green[id], x, y)
  else
	paintutils.drawImage(red[id], x, y)
  end
end

while true do
  event, id, msg, dist = os.pullEvent("rednet_message")
	if id == 3 then
		 x = 25
		 y = 9
		 signal(id, msg, x, y)
   else
		 drawLines()
  end
end