Singleton + Reflection + ?????

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Rotty956
Posts: 74
Joined: 02 Jan 2011, 15:58

Singleton + Reflection + ?????

Postby Rotty956 » 15 Feb 2011, 14:04

Hello Everyone
I'm using my main Extension like a singleton. In my main Extension I store some data. I then would like to use reflection on the Extension to call some of its functions dynamically.

Is there a way that I can use the reflection without needing to make a new instance of it? Otherwise I loose my data which is the most important part. Can you do reflection on a current instantiated object?
To truly make the Extension a singleton shouldn't I be making all the methods static?
Is this a horribly bad idea using my extension as a singleton? I think I saw somewhere in the doc suggesting it.

Any input would be great thanks!
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Re: Singleton + Reflection + ?????

Postby tchen » 15 Feb 2011, 15:06

Rotty956 wrote:Is this a horribly bad idea using my extension as a singleton?


Yes ;) You'll actually end up with multiple independent singletons if you have Room extensions since they each have their own class loaders.

You'll need to traverse the getZone getParentExtension methods and casts to get your way to the main extension. Helper classes make this a bit more bearable. Once you're there, have at it with reflection.

Alternatively, you could try putting your world data into a separate singleton class and throwing it into __lib__ folder. That should survive the singleton/class-loader issue. You won't get the niceties of an extension reset if you go this route though.

Look under "Advanced Extension Development" in the docs for more detailed explanations.
Rotty956
Posts: 74
Joined: 02 Jan 2011, 15:58

Postby Rotty956 » 15 Feb 2011, 16:27

The way I wanted to do it is each room would have its own singleton per game.

Ultimately what I want is to call functions inside the extension from a string. I have a function that the handler will call something like useAbility that will call one of the ability functions in the extension. From my research I need to use reflection to do this. However all the examples I see of using reflection get a new instance of the class that they want to invoke the method on. This I can't do because making a new instance will clear out all the data I'm trying to get at.

I'm currently calling handler that calls a main function on the extension that is suppose to dynamically call ability functions.

Thanks for your response = P
Rotty956
Posts: 74
Joined: 02 Jan 2011, 15:58

Postby Rotty956 » 16 Feb 2011, 03:53

I figured it out. From all the examples I was looking at they kept making a new instance or object of the thing they want to do reflection on. I finally got it to work using this bit of code.

Code: Select all

         method.invoke(this, id, targetId);



I thought I had tried this but I guess I must of had an error else where.

Thanks again for your help tchen.

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 139 guests