www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - I need runtime reflection

reply Gheorghe Gabriel <knoppy273 live.com> writes:
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
Sep 27
next sibling parent Jordan Wilson <wilsonjord gmail.com> writes:
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
Sep 27
prev sibling parent reply JN <666total wp.pl> writes:
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.
Sep 29
parent reply Gheorghe Gabriel <knoppy273 live.com> writes:
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
Sep 29
parent reply bitwise <bitwise.pvt gmail.com> writes:
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.
Sep 29
parent reply Gheorghe Gabriel <knoppy273 live.com> writes:
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.
Sep 29
next sibling parent bitwise <bitwise.pvt gmail.com> writes:
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.
Sep 29
prev sibling parent reply bitwise <bitwise.pvt gmail.com> writes:
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
Sep 30
parent reply Gheorghe Gabriel <knoppy273 live.com> writes:
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
Oct 01
parent bitwise <bitwise.pvt gmail.com> writes:
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.
Oct 01