Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9076157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:02:55+00:00 2026-06-16T19:02:55+00:00

I’m writing a modding API for my game and I want to allow users

  • 0

I’m writing a modding API for my game and I want to allow users to tell the game engine to run their mod’s function immediately before or after one of the API functions, in other words to “extend” the functions.

Before now modders had to rewrite the function to add functionality to it which meant digging in the game’s sourcecode, which I think sucks for them.

Right now I’m thinking of something like this:

def moddersFunction():
    pass

myAPI.extendFunction('functionName', 'before-or-after', moddersFunction, extraArgs=[])

Now what will be comparably clean way to implement this?
In other words, what would the internals of myAPI.extendFunction look like if you were to write it?

I’m thinking of having a dictionary which myAPI.extendFunction will add functions to and the functions in my API will check and run the mod’s function if it has been set (“registered”).
But this means adding the code which checks the dictionary in every single function in my modding API which I want to allow to be extended (even if it’s just a single function call which does the check and function calling itself, it seems to much itself).

I’m wondering if there’s some neat Python trick which will allow such “extending” of functions without “butchering” (maybe I’m exaggerating) the existing sourcecode of my game’s API.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:02:56+00:00Added an answer on June 16, 2026 at 7:02 pm

    I would do something along the following lines:

    class hookable(object):
      def __init__(self, fn):
        self.pre = []
        self.post = []
        self.fn = fn
      def add_pre(self, hook):
        self.pre.append(hook)
      def add_post(self, hook):
        self.post.append(hook)
      def __call__(self, *args, **kwargs):
        for hook in self.pre:
          hook(*args, **kwargs)
        ret = self.fn(*args, **kwargs)
        for hook in self.post:
          hook(*args, **kwargs)
        return ret
    

    Any “extendable” function can now be decorated like so:

    @hookable
    def square(x):
      return x**2
    

    Now you can use square.add_pre() and square.add_post() to register functions that would automatically get called before and after each call to square():

    print square(2)
    
    def pre(x): print 'pre', x
    def post(x): print 'post', x
    
    square.add_pre(pre)
    print square(2)
    print square(3)
    
    square.add_post(post)
    print square(4)
    print square(5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I am writing an app for my school newspaper, which is run completely online
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.