Jump to content




[Lua] Same code not working twice o_O


  • You cannot reply to this topic
4 replies to this topic

#1 D3add3d

  • Members
  • 7 posts
  • LocationSD card

Posted 21 April 2013 - 09:12 AM

Spoiler
It writes:
-----------------------------------------------------
______________Shop v1
-----------------------------------------------------
//replace _ with [space]

and nothing else... When I was testing the sensor I had this:
Spoiler
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?

#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 April 2013 - 09: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 :)

#3 D3add3d

  • Members
  • 7 posts
  • LocationSD card

Posted 21 April 2013 - 09: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

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 21 April 2013 - 10: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.

#5 D3add3d

  • Members
  • 7 posts
  • LocationSD card

Posted 03 May 2013 - 01:27 PM

View PostremiX, on 21 April 2013 - 10:04 AM, said:

--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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users