Thread overview
I need runtime reflection
Sep 27, 2017
Gheorghe Gabriel
Sep 27, 2017
Jordan Wilson
Sep 29, 2017
JN
Sep 29, 2017
Gheorghe Gabriel
Sep 29, 2017
bitwise
Sep 29, 2017
Gheorghe Gabriel
Sep 29, 2017
bitwise
Sep 30, 2017
bitwise
Oct 01, 2017
Gheorghe Gabriel
Oct 01, 2017
bitwise
September 27, 2017
Hi,

I have a 3D scene editor.
I need my scripts to be dynamically loaded in the scene.
In c# or java I can use reflections to do that.
How can I do that with D?
I know that std.traits only works in compile time.
Please, help me

Gabriel
September 27, 2017
On Wednesday, 27 September 2017 at 20:03:27 UTC, Gheorghe Gabriel wrote:
> Hi,
>
> I have a 3D scene editor.
> I need my scripts to be dynamically loaded in the scene.
> In c# or java I can use reflections to do that.
> How can I do that with D?
> I know that std.traits only works in compile time.
> Please, help me
>
> Gabriel

I've used a reflection library called witchcraft which might be helpful to you.
For script execution, I like LuaD, might be worth checking out also.

Jordan

September 29, 2017
On Wednesday, 27 September 2017 at 20:03:27 UTC, Gheorghe Gabriel wrote:
> Hi,
>
> I have a 3D scene editor.
> I need my scripts to be dynamically loaded in the scene.
> In c# or java I can use reflections to do that.
> How can I do that with D?
> I know that std.traits only works in compile time.
> Please, help me
>
> Gabriel

Your best bet is to either use a scripting language like Lua (Unity is written in C++ and uses C#), or compile your scripts to a dll and load them from there.
September 29, 2017
On Friday, 29 September 2017 at 09:34:26 UTC, JN wrote:
> On Wednesday, 27 September 2017 at 20:03:27 UTC, Gheorghe Gabriel wrote:
>> Hi,
>>
>> I have a 3D scene editor.
>> I need my scripts to be dynamically loaded in the scene.
>> In c# or java I can use reflections to do that.
>> How can I do that with D?
>> I know that std.traits only works in compile time.
>> Please, help me
>>
>> Gabriel
>
> Your best bet is to either use a scripting language like Lua (Unity is written in C++ and uses C#), or compile your scripts to a dll and load them from there.

Thank you for your answer,

This is a script example:

----------------------------
module game.player;

import engine;

class Player : Actor {

    this(string name) {
        super(name)
    }

    ~this() {}

    void jump() { ... }

    @Editable("Slider")("min = 0; max = 5;")
    ubyte lives = 5;
}
----------------------------

If i compile this script to a .dll then pleyer object can be dragged and dropped into the scene?
Do yout think that after the final LDC Android build, the game will work properly on Android? (because of .dll scripts)

Gabriel
September 29, 2017
On Friday, 29 September 2017 at 11:05:00 UTC, Gheorghe Gabriel wrote:
> [...]
> If i compile this script to a .dll

DLL support for D is currently very spotty. Before investing too much time, I would suggest confirming that DLLs are even properly supported for your target platform at all.


September 29, 2017
On Friday, 29 September 2017 at 16:24:32 UTC, bitwise wrote:
> On Friday, 29 September 2017 at 11:05:00 UTC, Gheorghe Gabriel wrote:
>> [...]
>> If i compile this script to a .dll
>
> DLL support for D is currently very spotty. Before investing too much time, I would suggest confirming that DLLs are even properly supported for your target platform at all.

So, could you upload a copy of your own reflection module? I want to try it and then I will send you my feedback.
September 29, 2017
On Friday, 29 September 2017 at 16:40:38 UTC, Gheorghe Gabriel wrote:
> On Friday, 29 September 2017 at 16:24:32 UTC, bitwise wrote:
>> On Friday, 29 September 2017 at 11:05:00 UTC, Gheorghe Gabriel wrote:
>>> [...]
>>> If i compile this script to a .dll
>>
>> DLL support for D is currently very spotty. Before investing too much time, I would suggest confirming that DLLs are even properly supported for your target platform at all.
>
> So, could you upload a copy of your own reflection module? I want to try it and then I will send you my feedback.

I'm reworking how properties/fields are set, and how functions are called. I'm not sure how long it will take. I'll throw a copy up when it's done.

Simple reflection is not that hard though:

`
ClassReflection[string] _reflections;

abstract class ClassReflection
{
    Object create() const;
    string name() const;
}

class ClassReflectionImpl(T) : ClassReflection
{
    import std.traits : fullyQualifiedName;
    static this() {
        _reflections[fullyQualifiedName!T] = new typeof(this);
    }

    override Object create() const { return new T; }
    override string name() const { return T.stringof; }
}

template registerClass(T) {
    alias registerClass = ClassReflectionImpl!T;
}

export extern(C) ClassReflection reflectionOf(string name) {
    ClassReflection* pRefl = name in _reflections;
    return pRefl ? *pRefl : null;
}

class Player {
    void speak() { writeln("hello world"); }
}

alias registration = registerClass!Player;

int main(string[] argv)
{
    ClassReflection playerRefl = reflectionOf("main.Player");
    ClassReflection missingRefl = reflectionOf("main.MissingClass");
    assert(playerRefl !is null);
    assert(missingRefl is null);

    Player player = cast(Player)playerRefl.create();
    assert(player !is null);
    player.speak();

    return 0;
}
`

You can use __traits and std.traits to build whatever you need into ClassReflection.


September 30, 2017
On Friday, 29 September 2017 at 16:40:38 UTC, Gheorghe Gabriel wrote:
> [...]

Still work to do, but usable.

https://github.com/nicolasjinchereau/d-reflection

October 01, 2017
On Saturday, 30 September 2017 at 19:06:20 UTC, bitwise wrote:
> On Friday, 29 September 2017 at 16:40:38 UTC, Gheorghe Gabriel wrote:
>> [...]
>
> Still work to do, but usable.
>
> https://github.com/nicolasjinchereau/d-reflection

I understand, thank you! :)

I have created another scripting model (compiled, not interpreted) for the graphics engine, which is better than the runtime reflection (it has only one disadvantage and more advantages over it). I cannot wait until the engine is finished, so that I will be able to show it to the public.

Gabriel
October 01, 2017
On Sunday, 1 October 2017 at 15:53:57 UTC, Gheorghe Gabriel wrote:
> On Saturday, 30 September 2017 at 19:06:20 UTC, bitwise wrote:
>> On Friday, 29 September 2017 at 16:40:38 UTC, Gheorghe Gabriel wrote:
>>> [...]
>>
>> Still work to do, but usable.
>>
>> https://github.com/nicolasjinchereau/d-reflection
>
> I understand, thank you! :)
>
> I have created another scripting model (compiled, not interpreted) for the graphics engine, which is better than the runtime reflection (it has only one disadvantage and more advantages over it). I cannot wait until the engine is finished, so that I will be able to show it to the public.
>
> Gabriel

I'm curious to see what you've come up with.

There are certain problems that can't really be overcome with a compile time approach, like generically passing things across DLL boundaries, or modifying/referencing things at design-time in an editor. You could generate some generic wrapper/interface at compile time, but that would basically be the same as run-time reflection at that point.