besen icon indicating copy to clipboard operation
besen copied to clipboard

Creating besen objects from application side + passing to functions

Open JernejL opened this issue 7 years ago • 4 comments

type..

TActorInterface = class(TBESENNativeObject)

class is registered properly so it can be prototyped from:

BesenInst.RegisterNativeObject('Actor', TActorInterface);

So i try to create a object on my side, and pass it as parameter to javascript

    JsPlayer: TBESENValue;

	AResult: TBESENValue;
    ta: Tactor;
    JsCreatedPlayer: TActorInterface;

begin

 				TBESEN(BesenInst).GarbageCollector.Protect(OnCreatePlayer);

 			    try
 			       AResult.ValueType := bvtBOOLEAN;

                   ta:= Tactor.GetFromIndex(PlayerID);

                   JsCreatedPlayer:= TActorInterface.Create(BesenInst, TBESEN(BesenInst).ObjectPrototype, false);
                   JsCreatedPlayer.SetHandle(ta.GetHandle());

 				   TBESEN(BesenInst).GarbageCollector.Add(JsCreatedPlayer);
                   TBESEN(BesenInst).GarbageCollector.Protect(TBESENObject(JsCreatedPlayer));

                   a[0]:= @JsPlayer;
                   JsPlayer := BESENObjectValue(TBESENObject(JsCreatedPlayer));
                   // todo: add parameter player ID

 			       OnCreatePlayer.Call(BESENObjectValue(OnCreatePlayer), @a, 2, AResult);

 			    except
 			       on e: EBESENError do
 			       begin
 			         addtochat(Format('%s ( Line %d ): %s', [e.Name, TBESEN(BesenInst).LineNumber, e.Message]));
 			       end;

 			       on e: exception do
 			       begin
 			         addtochat(Format('%s ( Line %d ): %s', ['Exception', TBESEN(Instance).LineNumber, e.Message]));
 			       end;
 			    end;


If i print the object it's: [object TActorInterface$Constructor] {}

Looks like some sort of empty object, it doesn't function properly in javascript (cannot change properties, etc.. ).

I've based my code on what i saw in https://github.com/Coldzer0/Cmulator/ project, i'm guessing i did something very wrong.

I'd just like to create an instance of native object, and pass it to function, so that properties and all can be correctly used as if it was created within javascript.

If you have a better method of contacting i'll take it, as i'm abusing tickets for help / support :( i'm in #besen channel but you are usually not reachable there.

JernejL avatar Nov 19 '18 08:11 JernejL

Can you upload a complete test project with your issue?

BeRo1985 avatar Nov 19 '18 12:11 BeRo1985

I will try to make an isolated example.

As for object creation logic, does that seem correct way to do it at all?

JernejL avatar Nov 19 '18 13:11 JernejL

Ok, here is a whole self-contained lazarus project that does what i experimented with, binary and all code included:

https://gtamp.com/tdc/besenexperiment.7z

I'm sure i'm doing a lot of things wrong or things could be done better, so i will take any kind of feedback on how to integrate besen better / properly.

I'm still learning on how besen works, but maybe out of this i can build a few more detailed examples and some nice documentation.

JernejL avatar Nov 19 '18 14:11 JernejL

Writing again, just as notification.. i made the example and linked it here.

JernejL avatar Dec 01 '18 13:12 JernejL