Giter VIP home page Giter VIP logo

Comments (3)

CrazyInfin8 avatar CrazyInfin8 commented on May 23, 2024

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.

from wren.

DCubix avatar DCubix commented on May 23, 2024

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() {

    }
}

from wren.

CrazyInfin8 avatar CrazyInfin8 commented on May 23, 2024

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

  • You could 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.
  • Alternatively you could use 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.

from wren.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.