Questions regarding Smartfox

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

Moderators: Lapo, Bax

MMOI_James
Posts: 4
Joined: 03 Oct 2020, 17:55

Questions regarding Smartfox

Postby MMOI_James » 03 Oct 2020, 18:13

Hi, so I am fairly new to SmartFox Server. I've been messing around with it off and on through out the years but never really settled on it. I am now attempting to learn it and Live Stream the development of a little prototype MMO. I was trying to figure out the best way to break everything up. I have experience with the HeroEngine and in Hero they have different types of "Areas" some are your normal zones/Game Areas but then they also break up all of the game features into areas. I got to think about that and looking into "Rooms" in Smartfox. I decided to attempt to break up game play features into different Rooms and then use the MMORooms for the actual zones.

So so far I have a MainWorld MMORoom and a Inventory Room which is just a regular basic room. When I join the MainWorld MMO Room I also join the Inventory Room at which point the Inventory Extension creates my Inventory. All of that is working fine. I am trying to figure out the best way to be able to access the Extensions from other extensions. So like the Inventory Extension would have to be accessed from the Combat Extension for example once a Mob is destroyed.

Just to test it out I decided to try and get a copy of the Inventory Extension from the Extension attached to the MainWorld room so I have a ServerReady event which contains the following:

Code: Select all

public class ServerReadyEvent extends BaseServerEventHandler
    {
        @Override
        public void handleServerEvent(ISFSEvent isfse) throws SFSException {
            Room InventoryRoom = getParentZone().getRoomByName("Inventory");
            ISFSExtension ext = SmartFoxServer.getInstance().getExtensionManager().getRoomExtension(InventoryRoom);
            InventoryExt = (InventoryExtension)ext;
           
            trace("Setting Extension up!");
        }
    }


When that event is called I get an exception:

java.lang.ClassCastException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.ClassCastException
Message: MMOExtensions.InventoryExtension cannot be cast to MMOExtensions.InventoryExtension
Description: Error during event handling: java.lang.ClassCastException: MMOExtensions.InventoryExtension cannot be cast to MMOExtensions.InventoryExtension, Listener: { Ext: MMOExtension, Type: JAVA, Lev: ROOM, { Zone: BasicExamples }, [ MMORoom: MainWorld, Id: 1, Group: default, AOI: (25.0, 25.0, 25.0) ] }
+--- --- ---+
Stack Trace:
+--- --- ---+
MMOExtensions.WorldExtension$ServerReadyEvent.handleServerEvent(WorldExtension.java:42)
com.smartfoxserver.v2.extensions.SFSExtension.handleServerEvent(SFSExtension.java:259)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:769)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchGlobalEvent(SFSExtensionManager.java:670)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:638)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.handleServerEvent(SFSExtensionManager.java:885)
com.smartfoxserver.v2.core.SFSEventManager$SFSEventRunner.run(SFSEventManager.java:66)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
java.lang.Thread.run(Thread.java:748)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



I really don't get this. Granted I am not the best at Java but in C# that would have worked just fine. Yes in C# you would want some checks in there to make sure it is actually the type you are expecting it to be but in this case I know what extension is attached to the Inventory room and it should be the InventoryExtension.
User avatar
Lapo
Site Admin
Posts: 21723
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Questions regarding Smartfox

Postby Lapo » 05 Oct 2020, 07:58

Hi,
the problem is the at this line:
InventoryExt = (InventoryExtension)ext;

and the reason is that the Zone Extension and Room Extension are two entirely different classes , because they are loaded in different classloaders.

If you're not familiar with this concept it is explained here:
http://docs2x.smartfoxserver.com/Extens ... assLoading

A quick way around the problem is to deploy your extension under extensions/__lib__/ , though this will disable the ability to reload Extensions on the fly. I'd recommend reading the link above to make sure you understand how this works.

If anything is unclear let us know
Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
MMOI_James
Posts: 4
Joined: 03 Oct 2020, 17:55

Re: Questions regarding Smartfox

Postby MMOI_James » 05 Oct 2020, 17:06

Thanks for the reply. I've restructured how I do my extensions. I have Singleton Systems that are located in the __Lib__ folder and then the extensions are purely for communicating with the Client. The Systems are able to be used from any where that they are needed. I have another issue now though. I am playing around with MMOItems trying to get interactive Objects working. I have created a Portal Class that extends MMOItem. The Portal has a position and active variables. When the player interacts with the portal it's supposed to send a ExtensionRequest to the World Extension. In the extension handler it toggles the Active variable and on the client it's supposed to toggle a portal particle effect. However I am not getting the MMOItem Variable Updates.

Here's the Interaction Handler:

Code: Select all


public class ItemInteractHandler implements IClientRequestHandler{
        @Override
        public void handleClientRequest(User user, ISFSObject params){
            int Id = params.getInt("ItemId");
            MMOItem item = worldItems.get(Id);
            Portal p = (Portal)item;
           
            p.Active = !p.Active;
           
            trace("Interacted with a Portal!");
            trace(p.Position);
        }
    }



Then in a scheduled task I run the following to update the MMOItemVariable:

Code: Select all


public class UpdateInteractiveObjects implements Runnable{

        @Override
        public void run() {
            worldItems.forEach((k, v) ->{
                Portal portal = (Portal)v;
                portal.setVariable(new MMOItemVariable("Active", portal.Active));
            });
        }
       
    }



Then on the client I've registered for the MMOItem Variable Updates
sfs.AddEventListener(SFSEvent.MMOITEM_VARIABLES_UPDATE, OnMMOItemUpdate);

The OnMMOItemUpdate method is never called no matter what I do and I really don't understand it.
MMOI_James
Posts: 4
Joined: 03 Oct 2020, 17:55

Re: Questions regarding Smartfox

Postby MMOI_James » 05 Oct 2020, 18:07

Ok I've got it working sending extension responses instead of using the built in MMOItemVariables. At some point I'd like to come back and revisit this and see if I can get it working but for now it's working ok just sending out extension responses.

Image

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Aldenea, DavinVicy, DerikFent, JerodSa, Kevengar and 31 guests