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
call zone extension's variable in request handler
-
- Posts: 11
- Joined: 11 Apr 2017, 17:13
Re: call zone extension's variable in request handler
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
This is in your RequestHandler class:
Hope it helps
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
-
- Posts: 11
- Joined: 11 Apr 2017, 17:13
Re: call zone extension's variable in request handler
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 MyExtensionCode: 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
Re: call zone extension's variable in request handler
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
-
- Posts: 11
- Joined: 11 Apr 2017, 17:13
Re: call zone extension's variable in request handler
This is my ZoneExtension
then I called getParenExtension() in GameRegisterHandler.class which was extends from BaseClientRequestHandler
then it returned null value at zoneExtension
https://imgur.com/a/UMPPD
What did I do wrong?
Thanks!
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!
-
- Posts: 11
- Joined: 11 Apr 2017, 17:13
Re: call zone extension's variable in request handler
Oh, sorry
If I put code in handleClientRequest method, it will return the exist value.
If I put code in handleClientRequest method, it will return the exist value.
Who is online
Users browsing this forum: Stevenor and 78 guests