• Content count

  • Joined

  • Last visited

Community Reputation

107 Neutral

About christophe-f8

  • Rank
  1. Help with delegates

    Right, that was what I meant to say, but your wording is definitely better than mine.
  2. Help with delegates

      This was it. Thanks for the quick reply.   For those who might struggle with this:   Basically in SetOnClick, I was passing and storing the pointer of the asIScriptFunction in my application. Then, when I wanted to trigger the event I was just preparing the context of the asIScriptFunction and executing it.   Until recently all my events where asIScriptFunction pointers to global functions of the script, and it seems those are never released until the end of the script execution (I guess because they might be called by the script at any time). But when I started to store asIScriptFunction pointers to class methods (delegates), I run into the problem of having the asIScriptFunction released between the time it was stored and the time I executed it (unless I declared the delegate in a global or at least class scope, so it remains in memory).   Anyway, I just added AddRef in the SetOnClick method to make sure the asIScriptFunction that is stored won't be released, and everything works ok now. (I can do TestButton.OnClick = MyEvent(this.TestButtonClick); directly without having to store anything).
  3. Help with delegates

    Oh dear...a minute after posting I found the solution. I declared the delegate as a private member of the script class so it doesn't get freed by the GC (I guess). Anyway it works, but is there any way to not have to declare all those delegates as private members ?   Just for reference if someone happens to have the same problem, here is the update script. Notice the private delegate of type MyEvent: class MyScriptClass { private MyEvent @ClickCallback; MyScriptClass(...) { Button @TestButton = Button(...); @ClickCallback = MyEvent(this.TestButtonClick); TestButton.OnClick = @ClickCallback; Button @TestButton2 = Button(...); TestButton2.OnClick = @TestButtonClick2; } void TestButtonClick(...) { // fails } } void TestButtonClick2(...) { // works ok } void main() { MyScriptClass @Test = MyScriptClass(...); }
  4. Help with delegates

    Hi,   I registered a simple button class which has an event being triggered when it is clicked. To do so, I registered a funcdef (MyEvent) and a method within the button class (SetOnClick) which stores in my application the asIScriptFunction given in parameter. This works just fine.   Now, I want to create a script class (NOT registered by the application, just a simple script class), which has buttons as private members. And I want to execute one of the methods of the class when the button is clicked. I created the method with the same signature as the MyEvent and in the constructor of the class I instantiated a delegate (MyEvent(this.MyMethod)) that I pass as a parameter to the SetOnClick method of the button. When I break in my app I can see asIScriptFunction->GetFuncType() is asFUNC_DELEGATE, but most other methods return NULL (GetObjectName, GetObjectType ... GetDeclaration returns "void ::_unnamed_function(...)).   I think the problem lays here, but just to explain further: if I just store the asIScriptFunction, when I want to use it (i.e. when the button is clicked and I want to call the function) all calls on the asIScriptFunction crash (unhandled exception, priviledge instruction ... pretty random), including the calls that were working when it was stored.   If it help here is my very simple script (pseudo code): class MyScriptClass { MyScriptClass(...) { Button @TestButton = Button(...); TestButton.OnClick = MyEvent(this.TestButtonClick); Button @TestButton2 = Button(...); TestButton2.OnClick = @TestButtonClick2; } void TestButtonClick(...) { // fails } } void TestButtonClick2(...) { // works ok } void main() { MyScriptClass @Test = MyScriptClass(...); } (MyEvent is declared by the application and both TestButtonClick and TestButtonClick2 use the MyEvent signature)