Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Python method disappearing after being bound to a class

I've been working on making a multiplayer game and have been diving into network code for the first time. An issue I have ran into that I can't seem to figure out is that a method that is dynamically added to an instanced class object seems to disappear and isn't there anymore when I try to call it later.

def Network_create(self, data): # format: "type" "id" "data"
    print(data)
    if not data['type'] in draw_table.keys():
        return
    thing = GameObject(data['id'])
    thing.__dict__ = data['data']

    f = draw_table[data['type']]
    thing.draw_func = types.MethodType(f, thing)
    print(dir(thing))
    game_objects.append(thing)

(I'm using PodSixNet) The above snippet receives messages from the network and recreates classes that exist on the server using the data recieved. The caveat is that every object recieved from the server has a custom "draw_func" defined for it based on the original class it was based on from the server. The code then takes a function from a draw_table and binds it to the created game object as a method and appends it to a list of game objects.

The issue comes when I try to later call the draw_func method later in the program as part of the client's main loop.

def render_updates():
    screen.clearscreen()
    for object in game_objects:
        print(dir(object))
        object.draw_func()

    screen.update()

I get the following error: AttributeError: 'GameObject' object has no attribute 'draw_func'

The issue seems to be that the method is disappearing somewhere along the line, and I have no idea why. Here is the output for dir(object) during object creation, and being processed in render_updates()

Before: ...'color', 'draw_func', 'heading', 'id', 'register', 'safe_delete', 'tracked_vars', 'update', 'x', 'y']

After: ....'color', 'heading', 'id', 'register', 'safe_delete', 'tracked_vars', 'update', 'x', 'y']

I'm genuinely stumped by this and am wondering if anyone has any idea what is going on for my method to just disappear like that.

Comments