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

[Lua] Same code not working twice o_O

Started by D3add3d, 21 April 2013 - 08:12 AM
D3add3d #1
Posted 21 April 2013 - 10:12 AM
Spoiler

os.loadAPI("ocs/apis/sensor") --load OpenCCSensors sensor API

left = sensor.wrap("left") --wrap the sensor
m = peripheral.wrap("top") --wrap the monitor

term.redirect(m) --redirect output to monitor

local headerText = "Shop v1" --store header text in variable

function fetchTargets() --define new function "fetchTargets()"
	local targets = sensor.call("left","getTargets") --fill table "targets" with getTargets from sensor
end --close the function

function render() --define new function "render()"
	if targets then --if the table "targets" is not nil then...
		for name, basicDetails in pairs(targets) do --for lenght of name and basicDetails in table targets do
			print("Entity: "..name.." "..textutils.serialize(basicDetails.position)) --print the name of entity, print serialized position of the entity
		end --end for loop
	end --end if
end --end function

function header()   --define new function "header()"
	x,y = term.getSize()	--gets size of terminal(in this case monitor on top)

	for i = 1,x do  --for 1 to value of x do
		term.write("-")	--write "-"
	end --end loop

	term.write("\n")	--new line
	term.write("\n")	--new line
	for i = 1,x do  --for 1 to value of x do
		term.write("-")
	end

	term.setCursorPos(x/2-#headerText/2, 2)	--sets the cursor to x/2 - lenght of headerText/2 char. of 2nd line
	term.write(headerText) --writes the text to the screen

	term.setCursorPos(1,4) --sets the cursor to 1st char. of 4th line
end

function redraw()   --define new function "redraw()"
	term.clear()	--clears the terminal
	term.setCursorPos(1,1)  --sets the cursor to 1st char. of 1st line
  
	fetchTargets()  --calls the fetchTargets() function
  
	header()	--calls the header() function
  
	render()	--calls the render() function
end

while 1 == 1 do --infinite loop (1 is always equal to 1)
	redraw()	--calls the redraw() function
	sleep(1)  --sleeps for 1 second
end		   --closes the loop

term.restore() --this will be usefull if I add some variable to end the loop (or use break)
It writes:
—————————————————–
______________Shop v1
—————————————————–
//replace _ with [space]

and nothing else… When I was testing the sensor I had this:
Spoiler

os.loadAPI("ocs/apis/sensor")
m = peripheral.wrap("top")
left = sensor.wrap("left")
term.redirect(m)
while true do
term.clear()
term.setCursorPos(1,1)
local targets = sensor.call("left","getTargets")
if targets then
for name, basicDetails in pairs(targets) do
print("Entity: "..name.." "..textutils.serialize(basicDetails.position))
print("-----")
end
end
sleep(1)
end
and it showed something like:
Entity: D3add3d {X=1,Y=1,Z=1}
—–
Entity: NeoRazorX {X=2,Y=1,Z=1}
—–

So… my question is why is it not showing the entity thing?
remiX #2
Posted 21 April 2013 - 10:18 AM
It's because you're making targets local within the fetchTargets function.

function fetchTargets() --define new function "fetchTargets()"
        local targets = sensor.call("left","getTargets") --fill table "targets" with getTargets from sensor
end --close the function

All you need to do is define a local targets table at the top:
local targets = {}
and change the function to
function fetchTargets() --define new function "fetchTargets()"
        targets = sensor.call("left","getTargets") --fill table "targets" with getTargets from sensor
end --close the function

Then it should work :)/>
D3add3d #3
Posted 21 April 2013 - 10:24 AM
Oh… thanks :)/>
I did not know it makes it local to function… I thought It was doing it local to file.
Again thanks for your help :)/>
//sorry for my bad english
remiX #4
Posted 21 April 2013 - 11:04 AM
It makes it local to the block.

Like so:

local var = "sup"
moo = true

if moo then
	local ifvalue = "what?"
	print(ifvalue) -- "what?"
	for i = 1, 2 do
		local test = "yeah!"
		print(test) -- "yeah!"
	end
	print(tostring(test)) -- nil!
end

print(tostring(ifvalue) -- nil!

moo there is a global variable. Will be able to be used within other programs, but restarting the pc will reset it.
D3add3d #5
Posted 03 May 2013 - 02:27 PM
–quote–

Hello, it is me again… I have

number = 1
in setnumber script
and
print(number)
in printnumber script… but the problem is it prints empty line… so I am not sure if the global variables are working :-/

Can you help me please?