2

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.

5
  • 1
    No nil in C or C++. Also no function. Commented Jan 3, 2014 at 15:02
  • 2
    I hate to break it to you, but this is not C or C++. It's Lua. Commented Jan 3, 2014 at 15:03
  • That's why i don't understood this code.. :-) I just read somewhere it's a c++ software to make games... But anyway do you know why it says to me MainMenu is nil? Commented Jan 3, 2014 at 15:14
  • Probably, you should add a line like this one: class "MainMenu"( soMainMenu ) to create MainMenu prior to using it. Undefined variables in Lua are nil. Commented Jan 3, 2014 at 15:43
  • 2
    The problem is that MainMenu is not declared anywhere in your file so its value is defaulting to NULL. Unfortunately, I think the solution is going to be specific to Dreemchest - from that tutorial it seems like you were supposed to declare things in separate files and that the related globals would be automatically initialized for you... Commented Jan 3, 2014 at 15:45

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you elaborate a little bit more on how and what you fixed. Then this would be a perfect answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.