Page 1 of 1

user.properties.put("var",0);

Posted: 04 Oct 2005, 15:47
by Virusescu
The following code caused me a lot of headeache

putIt = 0;
players["plxxx"].properties.put("myVar",putIt);
getIt = players["plxxx"].properties.get("myVar");
trace(">>> Should trace 0 but getIt-> "+getIt);

Altough it traces 0.0 and I could check the value in a conditional statement, I used that property to retrieve the property in an array and it doesn't acted as indended. Meaning arr[0] is not the same as arr[0.0] alotough getIt == putIt ;).

Posted: 05 Oct 2005, 08:09
by Lapo
Thanks for pointing this out.
The problem is in the documentation where it is not clearly explained that the values used in the properties array should be always strings.

To be more precise there can be 2 ways of working with this object: a basic way and an advanced one.

1) BASIC: just save any property you need in string form and retrieve it according to your needs.
This is the most simple way to attach any number of attributes to your User or Room objects. You can also store numbers in there, just remember to cast them back to their type before using them. Like this:
var n = Number ( usrObj.properties.get("n") )
2) ADVANCED: if you better understand the properties object you can use it for more interesting tasks.
The properties obj is java.util.HashMap object >> http://java.sun.com/j2se/1.4.2/docs/api ... shMap.html

For example you can store entire nested objects in your properties, game attributes and so on.
Also you can use all the other usefull methods of the HashMap like size(), containsKey(), containsValue() etc...

One last note:
The items inside a HashMap are up-casted to the java.lang.Object type, that's why you should need to cast the values you get back from the map to their original type.

One nice workaround to avoid this casting operation is to wrap all your properties inside an Actionscript object.

Code example:

Code: Select all

var o = {}
o.name = "this is my name" // string
o.age = 20 // number
o.married = false // boolean

usrObj.properties.put("myData", o)
This way all the variables wrapped in the "o" object will have their type preserved and there's no need to cast them

Quite a long explanation :lol:

We're in the process of releasing a new update with many new little fixes and features. We'll surely add these infos in the docs as well.

Hope it helps! :)

Posted: 05 Oct 2005, 08:38
by Virusescu
Thanks a bunch.
The wrapper object ideea is a cool and easy to use workaround :)