Target event Handler in the same class?

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

Moderators: Lapo, Bax

Post Reply
User avatar
Carl Lydon
Posts: 298
Joined: 12 Nov 2007, 16:15
Location: NYC

Target event Handler in the same class?

Post by Carl Lydon »

I realize it is not recommended, but is there a syntax for targeting an event handler to a function in the same class?

instead of:

Code: Select all

addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler.class);
something like:

Code: Select all

addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler.function);
or

Code: Select all

addEventHandler(SFSEventType.USER_LOGIN, LoginEventHandler); 
?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Target event Handler in the same class?

Post by rjgtav »

Hi.

Hmm, I don't think you can do that, as there are specific classes that you need to override for handling both server events and client requests, that's why you need to handle each different event in a separate class.
But you can handle multiple client requests on the same BaseClientRequestHandler, creating what we call of MultiHandler, which you can read more about here (search for @MultiHandler).

Oh, and please post the SFS2X server-side related questions here on the SFS2X Questions forum.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
User avatar
Carl Lydon
Posts: 298
Joined: 12 Nov 2007, 16:15
Location: NYC

Re: Target event Handler in the same class?

Post by Carl Lydon »

Thanks so much for the reply, I really appreciate it. I'm new to Java but not new to SF, I need help. :D

Ok, so the "recommended" way is actually the only way possible? It's not possible to do your entire extension in one class/package as is possible in SF Pro/ javascript? Ok.

I'm new to Java, but it seems similar to C#. One reason I wanted to do everything in one class is because I can't figure out how to communicate efficiently between classes without instantiating new instances all over the place. For example, to do a custom login you have to make a LoginEventHandler class. My main extension sets the event, and I know there is already an instance of the class because it traces out on init. When I ask the SF message board how to send messages to that class, they say I have to instantiate a new class and save that reference in a variable. I don't want to do this because clearly I have an instance already made on startup of the extension. I don't want tons of extraneous instances all over the place, plus it would be nice to let the classes have persistent global variables without instances conflicting with each other? If I had everything in one class this would not be an issue.

In Java + SF, If I can't keep all my events in one class, is there a way to call a function in an already-made-class without instantiating another instances of that class?

For example, in my main class called "TidesMainExtension" I have a function called "traceOut" that simply traces things...
Is it possible, from another class, to do something like:

Code: Select all

Class c = TidesMainExtension.class;
c.traceOut("testing...");
The above code doesn't work, but is there something like that, that would work?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Target event Handler in the same class?

Post by Lapo »

Carl, if you are more familiar with the SFS PRO way of coding check the BaseSFSExtension class instead of SFSExtension.
We talk about it here:
http://docs2x.smartfoxserver.com/Advanc ... extensions
(See "A peek at the Extension API" section)
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply