Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use namespaces for Scenes #8

Open
MatheusRich opened this issue Jan 19, 2024 · 1 comment
Open

Use namespaces for Scenes #8

MatheusRich opened this issue Jan 19, 2024 · 1 comment

Comments

@MatheusRich
Copy link
Contributor

MatheusRich commented Jan 19, 2024

Hey! Thanks for creating Scale! It's been helpful on my first DR game.

One thing that didn't please me was how all the game Scenes had to live inside the same scope. I understand how handy is being able to use Scene.send(:"tick_#{args.state.scene}", args), but the way the template is set encourages people to put all the scene behavior as methods under Scene. That can cause methods from one scene leaking (or worse, overriding) other scenes! For instance:

# scenes/gameplay.rb
module Scene
  def self.tick_gameplay(args)
    update(args)
  end

  def update(args)
  end
end


# scenes/pause.rb

module Scene
  def self.tick_pause(args)
    update(args)
  end

  # this will override the update method in the gameplay scene
  def update(args)
  end
end

I propose a gentle change (so we can still use send as before), but that namespaces the specifics of each scene. Each scene is still defined in a separate file, with a corresponding tick_#{scene} method. But that method will use an inner module to define its behavior:

# scenes/gameplay.rb
module Scene
  def self.tick_gameplay(args)
    GameplayScene.tick(args)
  end

  module GameplayScene
    class << self
      def tick(args)
        # do stuff
      end

      private

      # you can define private helper methods that don't pollute the Scene namespace
    end
  end
end


# scenes/pause.rb

module Scene
  def self.tick_pause(args)
    PauseScene.tick(args)
  end

  module PauseScene
    class << self
      def tick(args)
        # do pause stuff
      end

      private

      # pause helper methods
    end
  end
end

This won't vastly change the template, but it promotes better defaults, IMO. Thoughts?

@brettchalupa
Copy link
Member

Makes a ton of sense, I think that’d be great! Let me know if I can do anything to help support the change—contribution certainly welcome. Glad Scale has been helpful. 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants