Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
attempt to index global 'world' error — Gideros Forum

attempt to index global 'world' error

DikkesnoekDikkesnoek Member
edited August 2012 in General questions
Hello,

I think that I still don't understand Lua and Classes completely.
In the main.lua I specify a world globally like:
world = b2.World.new(0, 9.8, true)
I have a class called Block (block.lua). In this class I will handle
touches and mouse events. Also in this class I need to handle
physics. When I release the mouse button for example, the block
needs to drop to the floor due the gravity. When I define at the
beginning of block.lua:
Block = Core.class(Sprite)
 
local dummyBody = world:createBody({})
in the Block class I will get the error:

attempt to index global 'world'

I wan't to use one world so I thought that this was the right
approach. Any idea's?

Thanks,

Marc

Tagged:

Comments

  • zvardinzvardin Member
    edited August 2012
    In your Block class if you need access to the world, I would create a parameter to pass your game/world object in to keep a reference, so something like:
    function Block:init(world)
        self.world = world
    end
  • Thanks, I will try this. Is there no way to access global variables
    from the main file in your class file(s) and vice versa? Do you
    always need to pass these values via function parameters?

    Regards,

    Marc
  • Try including main.lua in the code dependency of block.lua (right click on the block.lua file in the project panel to access code dependencies). That might do it using world as a global.
  • exactly as @petec said. For now there is a pretty random order (probably order of creation) how lua files are read and processed. So probably Block class file is executed before main file, so you are trying to use variable which is not yet defined.
    Usually this is not a problem, because you can define class, but use it only later.
    So for example if you'd defined world usage inside call constructor, like:
    Block = Core.class(Sprite)
    function Block:init()
        local dummyBody = world:createBody({})
    end
    Then it would be valid, and you could use your class afterwards (for example in the end of main.lua file) like:
    local block = Block.new()
  • DikkesnoekDikkesnoek Member
    edited August 2012
    Thanks, I tried both. The first option seems to work, the second one
    will give errors in other classes I'll use.

    What I don't get is that:
    function Block:init(world)
    	-- Get the world.
    	self.world = world
    	-- Create empty Box2D body for touches and mouse.
    	dummyBody = self.world:createBody({})
     
    	-- etc --
    end
    is ok and,
    function Block:init(world)
    	-- Create empty Box2D body for touches and mouse.
    	dummyBody = world:createBody({})
     
    	-- etc --
    end
    is not ok.

    I know that it's Lua and not C, but....

    Marc
  • As @ar2rsawseen said, I think it's to do with the order in which the lua files are being executed, but I see what you are getting at. Why does 'world' get picked up OK when you use self.world=world in the first example but not in the second example? I'm afraid I have no idea. I just know that I've had to use code dependencies occasionally to get things working.
  • Actually I have no idea either. But there must be some logical explanations. There must be one!!! :)
  • @Dikkesnoek: did you try @petec's suggestion above?
    Try including main.lua in the code dependency of block.lua (right click on the block.lua file in the project panel to access code dependencies). That might do it using world as a global.
    That should work. You must set the checkbox next to main.lua in the code dependencies list for every class that accesses world.

  • Hi,

    Yes I tried that. It gave me errors in other classes. These classes are not
    directly dependent of main.lua.

    Marc
Sign In or Register to comment.