Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


christophe-f8

Member Since 27 Nov 2013
Offline Last Active Jan 17 2014 03:36 AM

Topics I've Started

Help with delegates

27 November 2013 - 04:47 AM

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)


PARTNERS