user2301881
user2301881

Reputation: 350

Why attempt to index global (a nil value) Lua?

Actually I'm working from a tutorial and there is some error in that tutorial.

There is the code:

class "Game"( Graphics )

Game.menuScreen  = nil
Game.gameScreen  = nil
Game.achScreen   = nil
Game.screen      = nil

-- main
function Game:main()
    -- Create the screens and store their links within the class
    Game.menuScreen  = MainMenu.new()
    Game.gameScreen  = GameScreen.new()
    Game.achScreen   = AchScreen.new()

    -- Display menuScreen
    Game.showScreen( 'menuScreen' )
end

-- showScreen
function Game.showScreen( name )
    -- If a screen is being displayed - remove it from the stage
    if Game.screen then
        Stage.detach( Game.screen )
        Game.screen = nil
    end

    -- Retrieve a screen link by name
    local screen = Game[name]
    if not screen then
        return nil
    end

    -- If the screen is found - add it to the stage
    Stage.attach( screen )
    -- Save the displayed screen
    Game.screen = screen

    return screen
end

It write to me [string "Game.script"]:11: attempt to index global 'MainMenu' (a nil value) I use Dreemchest Composer and this one is the tutirul: http://dreemchest.com/doc/en/Game%20menu%20and%20screen.html

Actually I remowed the LEVEL select screen from this code, cos I don't want to implement a level selection to my first game in this.

I have a script named MainMenu and it's class's MainMenu, Superclass is soMainMenu.

Upvotes: 2

Views: 7396

Answers (1)

user2301881
user2301881

Reputation: 350

I have got it, thank you!

I had to create Stage Objects for all menu points to get it work. It's easy but the tutorial may didn't wrote it or I just didn't understand it.

Upvotes: 0

Related Questions