Jump to content




What does _G['item']=item do?


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

#1 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 21 March 2016 - 11:00 PM

I'm curious.

#2 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 22 March 2016 - 02:18 AM

Unless you have a variable in local scope called item, it does absolutely nothing.

Edit: To elaborate, it sets item to the value it was already at.

Edited by Dragon53535, 22 March 2016 - 02:18 AM.


#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 22 March 2016 - 03:19 AM

_G.item = whatever    -- Sets an item in _G to whatever. Can then be accessed from anywhere.
item = whatever       -- Sets a global to whatever. Can then be accessed from anywhere in the current environment.
local item = whatever -- Sets a local to whatever. Can then be accessed within the current code block, or within nested blocks.

When you check the value of "item", if a local variable with that name exists, that gets used. If not, then if a variable with that name is a global of the current environment, then that gets used. If not, then if _G.item exists, that gets used. Failing all that, you finally get nil.

Usually in Lua, _G is your environment. But in terms of ComputerCraft, each shell instance gets its own environment (but can still access _G), and notably any APIs you load can't access any globals in those custom environment tables (as APIs aren't tied to a specific shell instance). So if you're running multiple shells on a system (eg via multishell tabs) and want to share vars, or want your APIs to be able to access certain variables, you'd manually dump them into _G.

http://lua-users.org...i/ScopeTutorial

http://www.lua.org/pil/14.html





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users