SetMMOItemVariables

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

Moderators: Lapo, Bax

DavidL
Posts: 3
Joined: 11 Oct 2020, 17:44

SetMMOItemVariables

Postby DavidL » 12 Oct 2020, 13:21

Good day SFS community!

First of all, I absolutely love your API :)
Saves so much headaches! Its amazing!

I've been working on a game for the past few weeks, and the development has been really smooth and easy so far.

But, right now I'm facing a problem and I have no idea what is wrong.

See my following code:

Code: Select all

// Set vars
LinkedList<IMMOItemVariable> itemVars = new LinkedList<IMMOItemVariable>();
itemVars.add(new MMOItemVariable("amount", 5));
itemVars.add(new MMOItemVariable("player", DBHandler.onlinePlayers.get(user.getName()).name));
itemVars.add(new MMOItemVariable("itemid", newItem.id));
// Create new mmoitem
MMOItem item = new MMOItem(itemVars);
ZoneExtension.getExt().trace("Works sofar3");
mmoApi = SmartFoxServer.getInstance().getAPIManager().getMMOApi();
mmoApi.setMMOItemVariables(item, itemVars, true);
ZoneExtension.getExt().trace("Works sofar4");


So, for some reason, "Works sofar4" never gets called. The application continues working and does not show any error at all. Somehow the "mmoApi.setMMOItemVariables(item, itemVars, true);" part gets stuck and I can't find anything about it in the logs or the console.

I know that the constructor of MMOItem already accepts the vars, but I need to update the values later so the .setMMOItemVariables function is a must for me. Setting the vars by .setVariable does not inform the clients AFAIK. Atleast, I've tried it and it doesn't for me.

Since I'm getting no feedback at all I have no idea whats wrong, I've been checking the docs about .setMMOItemVariables multiple times and I have no clue, perhaps I'm overlooking something?

Any help will be appreciated! :)
Thanks in advance!
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SetMMOItemVariables

Postby Lapo » 12 Oct 2020, 14:39

Hi,
seems strange that you get no errors, in any ways I think you should take a look at the MMOItem example in the docs, here:
http://docs2x.smartfoxserver.com/Advanc ... /mmo-rooms

Code: Select all

private void createMMOItem()
{
    // Reference to the MMOApi object
    SFSMMOApi mmoApi = SmartFoxServer.getInstance().getAPIManager().getMMOApi();
     
    // Reference to the game's MMORoom
    Room targetRoom = getParentZone().getRoomByName("My MMO Room");
     
    // Prepare a list of variables for the MMOItem
    List<IMMOItemVariables> variables = new LinkedList<IMMOItemVariable>();
    variables.add( new MMOItemVariable("type", "bonus") );
    variables.add( new MMOItemVariable("points", 250) );
    variables.add( new MMOItemVariable("active", true) );
     
    // Create the MMOItem
    MMOItem mmoItem = new MMOItem(variables);
     
    // Deploy the MMOItem in the MMORoom's map
    mmoApi.setMMOItemPosition(mmoItem, new Vec3D(50, 40, 0), targetRoom);
}

You should notice that the MMOItem is first positioned in the 2D/3D space. This is important because if you attempt to update its variables before you place it in the "map" the system will have no way of knowing which players should be receiving the update.

Once that's fixed it should work without problems.
Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
DavidL
Posts: 3
Joined: 11 Oct 2020, 17:44

Re: SetMMOItemVariables

Postby DavidL » 12 Oct 2020, 14:54

Lapo wrote:Hi,
seems strange that you get no errors, in any ways I think you should take a look at the MMOItem example in the docs, here:
http://docs2x.smartfoxserver.com/Advanc ... /mmo-rooms

Code: Select all

private void createMMOItem()
{
    // Reference to the MMOApi object
    SFSMMOApi mmoApi = SmartFoxServer.getInstance().getAPIManager().getMMOApi();
     
    // Reference to the game's MMORoom
    Room targetRoom = getParentZone().getRoomByName("My MMO Room");
     
    // Prepare a list of variables for the MMOItem
    List<IMMOItemVariables> variables = new LinkedList<IMMOItemVariable>();
    variables.add( new MMOItemVariable("type", "bonus") );
    variables.add( new MMOItemVariable("points", 250) );
    variables.add( new MMOItemVariable("active", true) );
     
    // Create the MMOItem
    MMOItem mmoItem = new MMOItem(variables);
     
    // Deploy the MMOItem in the MMORoom's map
    mmoApi.setMMOItemPosition(mmoItem, new Vec3D(50, 40, 0), targetRoom);
}

You should notice that the MMOItem is first positioned in the 2D/3D space. This is important because if you attempt to update its variables before you place it in the "map" the system will have no way of knowing which players should be receiving the update.

Once that's fixed it should work without problems.
Hope it helps

Yep, Thanks a lot man! This fixed it.

It makes sense, can't believe I didn't realize this. I have no idea why it doesn't throw an exception though, maybe that is an interesting case for the developers of SFS. I'm on version 2.14 so perhaps this does not occur in the latest versions.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SetMMOItemVariables

Postby Lapo » 12 Oct 2020, 15:17

Yeah, that's kind of strange, but it could be a bug as well. We'll try to recreate the use case and see what happens, if no warning is raised we'll make sure to add one.

Thanks for the feedback
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SetMMOItemVariables

Postby Lapo » 12 Oct 2020, 15:40

UPDATE: we tested with the latest SFS2X a similar scenario.
1- create a number of MMOItemVars
2- create the MMOItem
3- attempt to update the MMOItem vars via mmoApi.setMMOItemVariables()

And it fails at server side with this error:

Code: Select all

   at com.smartfoxserver.v2.api.SFSMMOApi.executeSetItemVariables(SFSMMOApi.java:274)
   at com.smartfoxserver.v2.api.SFSMMOApi.setMMOItemVariables(SFSMMOApi.java:188)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: MMOItem: [id: 1, null, {type={ N: type, T: STRING, V: bonus, H: false, P: false },  points={ N: points, T: STRING, V: 250, H: false, P: false }} ] needs to be added to an MMORoom before its variables can be updated!


So it seems that an exception is actually thrown, in this situation.

Were you running your code inside a Scheduled Task, perchance?
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
DavidL
Posts: 3
Joined: 11 Oct 2020, 17:44

Re: SetMMOItemVariables

Postby DavidL » 13 Oct 2020, 13:16

Lapo wrote:UPDATE: we tested with the latest SFS2X a similar scenario.
1- create a number of MMOItemVars
2- create the MMOItem
3- attempt to update the MMOItem vars via mmoApi.setMMOItemVariables()

And it fails at server side with this error:

Code: Select all

   at com.smartfoxserver.v2.api.SFSMMOApi.executeSetItemVariables(SFSMMOApi.java:274)
   at com.smartfoxserver.v2.api.SFSMMOApi.setMMOItemVariables(SFSMMOApi.java:188)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: MMOItem: [id: 1, null, {type={ N: type, T: STRING, V: bonus, H: false, P: false },  points={ N: points, T: STRING, V: 250, H: false, P: false }} ] needs to be added to an MMORoom before its variables can be updated!


So it seems that an exception is actually thrown, in this situation.

Were you running your code inside a Scheduled Task, perchance?
Cheers


Not inside the scope of a scheduled task but rather in an object created by a scheduled task. I have a scheduled task for spawning mobs every 20 seconds after the server is ready. The scheduler loops through an arraylist of spawners and calls the 'spawn' function inside every spawner (which, by the way, doesn't extend any SFS related objects) and the piece of code in my first post was actually supposed to be for the itemdrop(s) after a mob dies, but for testing purposes I had it in the spawn function and that's where this problem happened.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SetMMOItemVariables

Postby Lapo » 13 Oct 2020, 13:30

Not inside the scope of a scheduled task but rather in an object created by a scheduled task.

Ok. The only thing to keep an eye for is that Scheduled Tasks need to try/catch exceptions in their run() method. If you forget about it the Exception will not bubble up and automagically be thrown at the top of the call chain (as it is usual in Java).
Instead it will simply evaporate without you knowing, plus the Task will stop running (if it's a repeating one).

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Baidu [Spider] and 146 guests