In the style of Higginbottom. Formerly staticv0id@reddit

  • 0 Posts
  • 36 Comments
Joined 1 year ago
cake
Cake day: June 22nd, 2023

help-circle



  • For most games, the real consequence of failure in a game is being forced to repeat what just happened. And getting caught in a Groundhog Day-esque situation that repeats once every few minutes suuuuucks.

    It’s even worse when a failure causes your character to lose stuff. That’s even more time wasted, in that the time and effort taken to get the thing is gone.

    Paint the rainbow on my proud carebear chest if you must. I just want a place to escape to for a little while, a place that doesn’t frustrate the shit out of me.








  • My favorite way to implement this is with decorators. I used this to make a dispatch table for reading objects from a MySQL database.

    (Yes I know I should be using UserDict below. You should too and don’t subclass dict like I did.)

    class FuncRegistry(dict):
        """Creates a registry of hashable objects to function mappings."""
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
        def register_for(self, key: Hashable) -> Callable:
            """Decorator to register functions in the registry.
    
            Parameters
    
            key: Hashable
                The key which should point to this function
    
            Returns: Callable
                Returns a decorator that registers the function to the key"""
            def decorator(fn: Callable) -> Callable:
                self[key] = fn
                return fn
            return decorator
    
    qreg = FuncRegistry()
    
    @qreg.register_for('foobr')
    def handle_foobr(arg1, arg2):
        # do something here then
        return
    
    qreg['foobr']('ooo its an arg', 'oh look another arg')
    

    edit: formatting