Getting ActionScript UserVariables with Java

Post here your questions about Actionscript and Java server side extensions development.

Moderators: Lapo, Bax

Post Reply
cevans
Posts: 15
Joined: 02 Jul 2007, 11:00

Getting ActionScript UserVariables with Java

Post by cevans »

Hi!

In my current project I'm using a mix of ActionScript and Java (like 6.6 in the Docs). Basically I use embedded Java classes for areas that need optimal performance.

I'm in the process of converting one of my Actionscript functions to a Java method, but I'm running into a snag. In Actionscript I've stored a numerical list into a user variable, like this:

Code: Select all

var mylist = [1,2,3,4,5]
user.properties.put("MyList", mylist)
Now in my Java method I'm trying to retrieve the value of the user variable. However, it only returns a string type:

Code: Select all


UserVariable mylist_uvar = myUser.getVariable("MyList");
	
 String mylist_nums = mylist_uvar.getValue();
	  
/** 
* Obviously this next step won't work because it's the wrong type
* But I need to somehow to get it so I can read the integers
* from the list.
*/		
   int num1 =mylist_nums[0];
   int num2 = mylist_nums[1];
   int num3 = mylist_nums[2];

I looked at the UserVariable.setType() method, but I don't think that will convert the value string to an integer list. Can you only use strings for user variables in Java?

Please note, I'm a Java beginner. :) So I very well could be missing something very obvious. :P

Thanks for any help.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

You're using the wrong code to retrieve the data.

Use this instead:

Code: Select all

Object obj = user.properties.get("MyList")
To avoid problems with types in Java, I would recommend using a Java list on the AS side instead of a generic Array. It will save you a lot of headaches.

Use an ArrayList or LinkedList instead, so when you grab it on the Java side you know exactly what type it is. (Also use generics too if you're using Java 1.5 or higher)
Lapo
--
gotoAndPlay()
...addicted to flash games
cevans
Posts: 15
Joined: 02 Jul 2007, 11:00

Post by cevans »

Yeah a day later I noticed I was using the wrong code, but I still got stuck trying to convert the Object to an Integer list.

So you recommend using a Java list in the ActionScript code? Doh... I've already done a ton of code using the AS arrays and objects. Can you post a tiny code snippet of initializing a Java ArrayList or LinkedList in AS?

Even though I'm having some questions about the Java code, I just want to say again how well documented the SmartFox server is. I've been using SmartFox since February and I haven't had to post until now because the documentation has so much information.

If I can just get my head around the AS/Java interaction, I'll be golden.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

cevans wrote: So you recommend using a Java list in the ActionScript code? Doh... I've already done a ton of code using the AS arrays and objects. Can you post a tiny code snippet of initializing a Java ArrayList or LinkedList in AS?
Well no... I am not saying that you should replace all your AS arrays with Java collections. I would recommend to use them when you have to pass them back to Java code.

Initializing a List is as simple as it is in Java

Code: Select all

// reference to the java package
var _util = Packages.java.util

// Create an ArrayList instance
var myList = new _util.ArrayList()

// Add an item
myList.add("This is cool!")
The ECMA-Script engine used by SmartFoxServer is Mozilla Rhino, so if you want to learn more about integrating AS code and Java you can have a look here -> http://www.mozilla.org/rhino/ScriptingJava.html

Also the engine supports the latest Javascript 1.7 version which has some very interesting new features, not available in Actionscript.
You can check them out here --> http://developer.mozilla.org/en/docs/Ne ... Script_1.7
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply