Jump to content




Local Variables are toast.


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

#1 Pyuu

  • Members
  • 203 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?

#2 Lemmmy

  • Members
  • 218 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.

#3 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 17 January 2016 - 01:39 AM

Tutorial

#4 Lemmmy

  • Members
  • 218 posts

Posted 17 January 2016 - 01:40 AM

View PostDragon53535, on 17 January 2016 - 01:39 AM, said:


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.

#5 Pyuu

  • Members
  • 203 posts

Posted 17 January 2016 - 01:40 AM

It's sad that the tutorial doesn't have more replies :(

#6 Lemmmy

  • Members
  • 218 posts

Posted 17 January 2016 - 01:45 AM

Yeah. Is there a master board for tutorials?

Edit: like an index of them.

Edited by Lemmmy, 17 January 2016 - 01:45 AM.


#7 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 17 January 2016 - 01:48 AM

Here In the spoiler

#8 Lemmmy

  • Members
  • 218 posts

Posted 17 January 2016 - 01:53 AM

We should have a better, more nicely formatted, easily findable and stickied post for it.

#9 Pyuu

  • Members
  • 203 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.

#10 Lemmmy

  • Members
  • 218 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.

#11 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 17 January 2016 - 02:00 AM

Yeah, it's more of a computercraft focused list.

#12 Lemmmy

  • Members
  • 218 posts

Posted 17 January 2016 - 02:03 AM

View PostDragon53535, on 17 January 2016 - 02:00 AM, said:

Yeah, it's more of a computercraft focused list.

I'll bring it to Forum Discussion.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users