call zone extension's variable in request handler

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

Moderators: Lapo, Bax

Ctrl_Shift_N
Posts: 7
Joined: 11 Apr 2017, 17:13

call zone extension's variable in request handler

Postby Ctrl_Shift_N » 04 Mar 2018, 01:50

Hello,
I have got some variables which were declared in zone extension, and I want to call them in ClientRequestHandler or ServerEventHandler without using static.
How can I do it?
Thank you and sorry about my poor English
User avatar
Lapo
Site Admin
Posts: 20113
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: call zone extension's variable in request handler

Postby Lapo » 05 Mar 2018, 08:32

Hi,
you mean that you want to access some fields of your Extension class from another class? Such as a RequestHandler?

If this is the case you just need to create getters for those fields and the access them from the other class.

Example:
This is in your main Extension class, called MyExtension

Code: Select all

public int getMaxPlayers()
{
   return value;
}


This is in your RequestHandler class:

Code: Select all

int maxPlayers = ((MyExtension) getParentExtension()).getMaxPlayers();


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
Ctrl_Shift_N
Posts: 7
Joined: 11 Apr 2017, 17:13

Re: call zone extension's variable in request handler

Postby Ctrl_Shift_N » 06 Mar 2018, 10:05

Lapo wrote:Hi,
you mean that you want to access some fields of your Extension class from another class? Such as a RequestHandler?

If this is the case you just need to create getters for those fields and the access them from the other class.

Example:
This is in your main Extension class, called MyExtension

Code: Select all

public int getMaxPlayers()
{
   return value;
}


This is in your RequestHandler class:

Code: Select all

int maxPlayers = ((MyExtension) getParentExtension()).getMaxPlayers();


Hope it helps

Will I need setParentExtension() in somewhere?
I tried to call getParentExtension() before, but it returned null value
thanks for your help
User avatar
Lapo
Site Admin
Posts: 20113
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: call zone extension's variable in request handler

Postby Lapo » 06 Mar 2018, 15:41

Ctrl_Shift_N wrote:
Lapo wrote:Hi,
Will I need setParentExtension() in somewhere?

No. That's already done for you.

I tried to call getParentExtension() before, but it returned null value

I don't think that's possible.
If it happens please show me the code and the stack trace of the exception.

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Ctrl_Shift_N
Posts: 7
Joined: 11 Apr 2017, 17:13

Re: call zone extension's variable in request handler

Postby Ctrl_Shift_N » 07 Mar 2018, 14:27

This is my ZoneExtension

Code: Select all

public class ZoneExtension extends SFSExtension {

    private Database database;
    private Cache cache;
    private UserManager userManager;

    @Override
    public void init() {

        generateDbAndCache();

        addEventHandler(SFSEventType.USER_LOGIN, LoginHandler.class);
        addRequestHandler("game.register", GameRegisterHandler.class);
    }

    public void initManager() {
        userManager = new UserManager(database, cache);
    }

    public void generateDbAndCache() {
        database = Database.getInstace();
        cache = Cache.getInstace();
    }

    public Database getDatabase() {
        return database;
    }

    public Cache getCache() {
        return cache;
    }

    public UserManager getUserManager() {
        return userManager;
    }
}


then I called getParenExtension() in GameRegisterHandler.class which was extends from BaseClientRequestHandler

Code: Select all

private Database database;
    private Cache cache;
    private ZoneExtension zoneExtension;

    ISFSObject object = new SFSObject();

    public GameRegisterHandler() {

        zoneExtension = (ZoneExtension) getParentExtension();

        System.out.println(">> zoneExtension: " + zoneExtension);

        database = Database.getInstace();
        cache = Cache.getInstace();
    }

then it returned null value at zoneExtension
https://imgur.com/a/UMPPD
What did I do wrong?
Thanks!
Ctrl_Shift_N
Posts: 7
Joined: 11 Apr 2017, 17:13

Re: call zone extension's variable in request handler

Postby Ctrl_Shift_N » 07 Mar 2018, 14:35

Oh, sorry
If I put code in handleClientRequest method, it will return the exist value. :D

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Rob and 13 guests