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

Local Variables are toast.

Started by Pyuu, 17 January 2016 - 12:31 AM
Pyuu #1
Posted 17 January 2016 - 01:31 AM
I've been in CC for a while now, but I've never really figured out where local variables can be accessed from.
Such as:

local A = 1

for i=1, 20 do
if A == 1 then -- Is A nil or 1?
blah()
end
end

function b () if A == 1 then
-- is A nil or 1?
end
end

function c ()
local B = 2
end
c()
if B == 2 then
-- Is B nil or 2?
end

It's quite a confusing mess honestly of where local variables can be called.

I've seen a few people do this:


local A = 1
function B ()
local A
if A == 1 then
-- A is 1?
end
end

It's confusing. Anyone got any good information about this?
Lemmmy #2
Posted 17 January 2016 - 01:38 AM
This may be a good read for you.

If you redefine a variable inside a function, it will of course keep its new definition within the scope of the function. For your final case, A will be nil, not 1.
Dragon53535 #3
Posted 17 January 2016 - 01:39 AM
Tutorial
Lemmmy #4
Posted 17 January 2016 - 01:40 AM

I didn't know there was a scope tutorial here, that's definitely a good read too! I definitely recommend that Mayushii reads the PIL too, of course.
Pyuu #5
Posted 17 January 2016 - 01:40 AM
It's sad that the tutorial doesn't have more replies :(/>
Lemmmy #6
Posted 17 January 2016 - 01:45 AM
Yeah. Is there a master board for tutorials?

Edit: like an index of them.
Edited on 17 January 2016 - 12:45 AM
Dragon53535 #7
Posted 17 January 2016 - 01:48 AM
Here In the spoiler
Lemmmy #8
Posted 17 January 2016 - 01:53 AM
We should have a better, more nicely formatted, easily findable and stickied post for it.
Pyuu #9
Posted 17 January 2016 - 01:53 AM
Lol there is a master board, but I haven't seen local variables in Ask a pro.
Lemmmy #10
Posted 17 January 2016 - 01:55 AM
I also just realised, the post you linked doesn't actually have anything regarding Lua programming in general.
Dragon53535 #11
Posted 17 January 2016 - 02:00 AM
Yeah, it's more of a computercraft focused list.
Lemmmy #12
Posted 17 January 2016 - 02:03 AM
Yeah, it's more of a computercraft focused list.

I'll bring it to Forum Discussion.