• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
christophe-f8

Help with delegates

5 posts in this topic

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)

Edited by christophe-f8
0

Share this post


Link to post
Share on other sites

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(...);
}
0

Share this post


Link to post
Share on other sites

How have you implemented and registered the Button::OnClick  property?

 

The Button should make sure the asIScriptFunction is not released prematurely by calling AddRef on it (or not calling Release, depending on how you've registered the method).

0

Share this post


Link to post
Share on other sites

The Button should make sure the asIScriptFunction is not released prematurely by calling AddRef on it 

 

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).

Edited by christophe-f8
0

Share this post


Link to post
Share on other sites

Just to clarify the behaviour of AngelScript here.

 

When a function handle is taken for a class method, a delegate object is created to hold the handle to the class instance and the handle to the class method. This delegate object is not owned by anyone so it will be destroyed if no-one takes ownership by calling AddRef on the asIScriptFunction pointer (storing the handle in a global variable is implicitly calling AddRef).

 

When a function handle is taken for a global function, the handle points to the actual function which is owned by the module, and will thus only be released when the module is discarded. If the module had been discarded while you still held a pointer to the script function (without calling AddRef) you would have seen the same error as you did for the delegate.

 

Regards,

Andreas

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0