wren icon indicating copy to clipboard operation
wren copied to clipboard

Retrieving data encoded in Wren classes from C/++

Open redruin1 opened this issue 6 months ago • 3 comments

I'm working with embedding Wren and I would like to be able to specify Wren classes which act as "prototypes" for instances of game objects. For example, I'd like to do something akin to:

class Weapon {
    shoot() { } // stub implementation

    // Register the class on the host
    static foreign register(weapon_class)
}

class Revolver is Weapon {
    static id { "revolver" } // all instances of revolver should have the same ID, hence static

    // instance-specific method
    shoot() {
        Game.spawn("bullet")
    }
}
Weapon.register(Revolver)

And then Weapon.register() would somehow inspect the Revolver class in order to pull attributes from it (such as id). Is an approach like this possible? I'm struggling with figuring out how to call static getters for class Revolver, and problematically the only way I know how requires calling wrenCall() during the foreign function, which from what I understand is prohibited.

redruin1 avatar May 21 '25 10:05 redruin1

Looks like to be an API design issue. There is a mix of class instance vs object instance mix, with some unknown underlying native system. It is most likely you are trying to solve a problem the wrong way...

mhermier avatar May 21 '25 12:05 mhermier

Right now, I think I want to use Wren for describing the data and functionality of certain game objects, which would primarily be dispatched and managed by the host application. Using classes is not necessarily required; for example, I could do the following, which seems to do what I need:

Weapon.register({
    "id": "revolver",
    "shoot": Fn.new { ... }
})

But writing a class seems to me a better mechanism for actually defining this data alongside the methods which describe its behavior. In addition, defining a class like this would allow me to use it to create actual instances of Revolver on the Wren side of things.

redruin1 avatar May 22 '25 06:05 redruin1

Ifeom what I understand,the issue is not at registration, but at the wrenCall site. What triggers the call and do you expect to trigger? As of right now Weapon class can be anything, and could register any passed object. And from the little I see, maybe you want to do Weapon.register("id", object) instead, to allow multiple aliases.

mhermier avatar May 22 '25 11:05 mhermier