Closed DCubix closed 1 year ago
Currently your script has on_create
defined as static on_create(object)
. This means that script->receiver
must be the Test
class itself and the signature passed to wrenMakeCallHandle
(which returns the value used for script->on_create
) should be "on_create(_)"
(single underscore for single parameter).
Your engine ensures 3 slots, extra slots should just be ignored when calling wrenCall
so slot 0 is the receiver and slot 1 is the value for object (slot 2 is extra so it would just get ignored).
If you want to pass just the foreign object to the function parameter, you could put the object class in slot 2 and create the foreign function in slot 1 instead using wrenSetSlotNewForeign
(might be able to get away with putting the object class in slot 1 then replacing it with the foreign object to the same slot but you should test that first)
if you want both the object class as well as the foreign object passed to on_create
, you'll need to create a method with signature: "on_create(_,_)"
(2 underscores for 2 parameters, also you'd use this signature for wrenMakeCallHandle
instead). This allows the value in slot 2 to be used as the second parameter.
I'm not completely certain I fully understand the situation so feel free to clarify if I misunderstood the issue.
If you want to pass just the foreign object to the function parameter, you could put the object class in slot 2 and create the foreign function in slot 1 instead using wrenSetSlotNewForeign (might be able to get away with putting the object class in slot 1 then replacing it with the foreign object to the same slot but you should test that first)
@CrazyInfin8 oooh that worked just fine! Thank you!
What if I want to remove the static
from the functions and do something like Lua's metatables for the foreign object where I can implement new functionality in runtime?
So instead of doing like I did with the static methods, I have something like this:
class Test {
construct new() {}
on_create() {
System.print(this.name) // this = the foreign object
System.print("Hello from on_create")
}
on_update(dt) {
}
on_destroy() {
}
}
to remove static
from the functions, you'd need an instance to call them first. You can create an instance using one of the following methods
wrenInterpret
some script that creates that instance using Test.new()
, store the resulting value in a top-level variable, then use wrenGetVariable
to get the instance value to use in C.Test
as the receiver (slot 0), new()
as the signature for wrenMakeCallHandle
, wrenCall
that handle, and (assuming no errors) slot 0 should contain a brand new instance of the class Test
(return values of wren functions are placed in slot 0).After you've done one of these, you can proceed to use that instance as the new receiver (instead of using the class itself) when calling on_create
, on_update
, on_destroy
.
I currently have this script:
But I have no idea how can I call the methods from the C API and pass a foreign object to them.
I call them like this from the engine:
However, it's passing 3 parameters to it, so "object" is the class... So the call becomes
on_create(object_class, object)
.Is there a workaround for this? And also, wouldn't it make more sense for
wrenSetSlotNewForeign
to pop that class type from the stack?