224 posts
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?
256 posts
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.
1080 posts
Location
In the Matrix
Posted 17 January 2016 - 01:39 AM
256 posts
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.
224 posts
Posted 17 January 2016 - 01:40 AM
It's sad that the tutorial doesn't have more replies :(/>
256 posts
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
1080 posts
Location
In the Matrix
Posted 17 January 2016 - 01:48 AM
256 posts
Posted 17 January 2016 - 01:53 AM
We should have a better, more nicely formatted, easily findable and stickied post for it.
224 posts
Posted 17 January 2016 - 01:53 AM
Lol there is a master board, but I haven't seen local variables in Ask a pro.
256 posts
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.
1080 posts
Location
In the Matrix
Posted 17 January 2016 - 02:00 AM
Yeah, it's more of a computercraft focused list.
256 posts
Posted 17 January 2016 - 02:03 AM
Yeah, it's more of a computercraft focused list.
I'll bring it to Forum Discussion.