Question about SFSExtension

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

Moderators: Lapo, Bax

monk
Posts: 11
Joined: 25 Mar 2020, 07:39

Question about SFSExtension

Postby monk » 28 Mar 2020, 22:16

Hello again,

In my code I have my main extension class (EngineExtension) inherited from SFSExtension. Inside EngineExtension I have multiple managers. AccountManager, MapManager, etc. I pass EngineExtension into those mangers through the constructor.

Now I assume this is by design, but I don't understand why I can't access certain methods of EngineExtension from within the manager classes. Methods such as addEventHandler and addRequestHandler.

So to get around this, I've created public functions in EngineExtension that can be called from the managers to access those add handler methods.

My question is, will this cause problems and why was it designed like this in the first place? I know about multihandlers, but I like to have my managers implement their own handlers.

Hope that made sense. Thanks.

Code: Select all

public class EngineExtension extends SFSExtension {

    public AccountManager accountManager;
    public RegionManager regionManager;

    @Override
    public void init() {
        accountManager = new AccountManager(this);
        regionManager = new RegionManager(this);
    }
   
    public void addEvtHandler(SFSEventType type, Class<?> listener) {
        addEventHandler(type, listener);
    }

    public void addReqHandler(String id, IClientRequestHandler handler) {
        addRequestHandler(id, handler);
    }
   
}

public class AccountManager {

    private EngineExtension engine;

    public AccountManager(EngineExtension e) {
        engine = e;

        // engine.addEventHandler(SFSEventType.USER_JOIN_ZONE, JoinZoneHandler.class) <- method isn't found
        engine.addEvtHandler(SFSEventType.USER_JOIN_ZONE, JoinZoneHandler.class);
    }
   
}
monk
Posts: 11
Joined: 25 Mar 2020, 07:39

Re: Question about SFSExtension

Postby monk » 29 Mar 2020, 06:20

Nevermind, I think I understand it a bit better now. :)
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Question about SFSExtension

Postby Lapo » 30 Mar 2020, 09:06

Hi,
those methods you have mentioned ( such as addRequestHandler ) are marked as protected in SFSExtension, which is the class you're extending.
The keyword makes the method accessible only by the child class (that which extends).

As a design suggestion, it would be best if you could add and remove all your listeners within your main Extension class, rather than delegating the task to several other subclasses.
If I understand it correctly you want certain managers to deal with specific events or requests, which is perfectly fine. To do so you could wrap the delegation inside the request handler.

Here's an example:

Code: Select all

@Override
public void init()
{
   addRequestHandler("myCustomRequest", new BaseClientRequestHandler()
   {
      @Override
      public void handleClientRequest(User sender, ISFSObject params)
      {
         mapManager.handleMyEvent(sender, params);
      }
   });
}

Inside the init() method of your Extension you can define a new req. handler and pass the delegating method directly.

Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 76 guests