Skip to content

Use namespaces for Scenes #8

@MatheusRich

Description

@MatheusRich

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions