Coder Social home page Coder Social logo

Comments (2)

Codinablack avatar Codinablack commented on August 22, 2024

Change

local Game = class("Game")

to

Game = class("Game")

and you can use it in multiple files, ofc middleclass will still need to be loaded

from middleclass.

kikito avatar kikito commented on August 22, 2024

I have several strategies for dealing with this.

First strategy is: not having big classes in the first place. A big class means that it is probably doing too much stuff. I try to use composition when possible (so instead of having a big object separated in two files, I would have two smaller classes separated into two files, and one of the classes would instantiate and use the other).

The second strategy is using mixins. In middleclass, a mixin is a table with functions inside. Here are two mixins:

-- game/main_menu.lua
local MainMenu = {
  show_main_menu = function()
    ...
  end,
  ...
}

return MainMenu
-- game/options_menu.lua
local OptionsMenu = {
  show_options_menu = function()
    ...
  end,
  ...
}

return OptionsMenu

And here's how you would use it in a "Game" class:

-- game.lua
local class = requie "middleclass"
local MainMenu = require "game.main_menu"
local OptionsMenu = require "game.options_menu"

local Game = class.new("MainMenu"):include(MainMenu, OptionsMenu)

...
local game = Game:new()
game:show_main_menu() -- show main menu
game:show_options_menu() -- show options menu

You could do the same with individual methods, as in your example (you could make game_main_menu.lua return a function, and then set it in the Game class by doing Game.main_menu = game_main_menu) but I think using mixins is less noisy and more powerful - you can group several functions in a single file this way.

A third option would be using stateful.lua. It is a mixin that "augments" classes with the possibility of having "states". Each state can have method called the same way. When you invoke a method on a stateful class, it invokes the method of the state marked as "current". I think it aligns well with a Game class:

-- game/main_menu.lua
local MainMenu = {
  draw = function()
    ...
  end,
  ...
}

return MainMenu
-- game/options_menu.lua
local OptionsMenu = {
  draw = function()
    ...
  end,
  ...
}

return OptionsMenu
-- game.lua
local class = require "middleclass"
local Stateful = require "stateful"
local MainMenu = require "game.main_menu"
local OptionsMenu = require "game.options_menu"

local Game = class.new("MainMenu"):include(Stateful)

Game:addState('MainMenu', MainMenu)
Game:addState('OptionsMenu', OptionsMenu)

...

local game = Game:new()

game:gotoState('MainMenu')
game:draw() -- draws the MainMenu

game:gotoState('OptionsMenu')
game:draw() -- draws the OptionsMenu

...
local game = Game:new()
game:show_main_menu()

That's all. Sorry for this taking so long for me to answer, I just noticed this issue.

from middleclass.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.