create enum in actionscript 3 and compare - actionscript-3

I need to create some enum values, give it a default value and then compare it.
I have this enum class
public class Car
{
public static const Tesla:int = 1;
public static const Ford:int = 2;
}
How do I initiate a new Car enumn variable with a default value of "Tesla" and how do I compare the variable? I'm looking for something like this:
public var c:Car = new Car(Car.Tesla);
if (c == Car.Tesla){
// Do something
}
Edit, it is now changed to the following:
public final class Car
{
public static const Tesla:String = "tesla";
public static const Ford:String = "ford";
}
And in the mxml file:
public var c:String = Car.Tesla;
if (c == Car.Tesla){
// Do something
}

I have this enum class
Just so we're on the same page about it: that's not an enum and there are no enums in as3. The language doesn't have that feature.
How do I initiate a new Car enumn variable with a default value of "Tesla" and how do I compare the variable?
You cannot, because Car is a type and the static properties it has are of type int which is something completely different.
What you can do is this:
var c:int = Car.Tesla;
if (c == Car.Tesla){
// Do something
}
If you want to have a Car object instead, add a brand property to the class of type int, which you can then assign the value of your constants to:
var c:Car = new Car();
c.brand = Car.Tesla;
if (c.brand == Car.Tesla){
// Do something
}
You could also add a parameter to the constructor and insert the value there.
Btw. changing
public static const Tesla:int = 1;
to
public static const Tesla:String = "tesla";
will give you the chance to get more meaningful values during debugging. The built in constants like MouseEvent.CLICK are defined this way.

Related

Unity Custom Editor Window Input Control for Type Determined by Reflection

I am creating a GUI for selecting a function in a Unity custom EditorWindow inspired by the UnityEvent GUI. I can't get UnityEvent itself to work; using a EditorGUILayout.PropertyField and referencing the UnityEvent member as a serialized property produces an empty foldout.
I have choosing a function working, but I can't figure out how to allow the user to specify the function parameter arguments.
using System.Reflection;
int functionIndex = 0;
MethodInfo[] methods = typeof(LvlGenFunctions).GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
string methodNames = new string[methods.Length];
for(int i = 0; i < methods.Length; ++i)
{
methodNames[i] = methods[i].Name;
}
functionIndex = EditorGUILayout.Popup(functionIndex, methodNames);
methods[functionIndex].Invoke(null, null);
I can get the ParameterInfo and thus the ParameterType, but I don't know how to create an input GUI field to specify an appropriate parameter argument.
How can I create a Unity IMGUI input field for a parameter whose type is determined by reflection?
To be clear
EditorWindow : ScriptableObject and ScriptableObject : Object
SerializedObject work with UnityEngine.Object so you can use this constructor:
new SerializedObject(Object target);
EditorWindow
public UnityEvent OnSomeEvent;
private SerializedObject serializedObject;
private SerializedProperty property;
private void OnEnable()
{
serializedObject = new SerializedObject(this);
property = serializedObject.FindProperty(nameof(OnSomeEvent));
}
private void OnGUI()
{
EditorGUILayout.PropertyField(property, true);
}
Now you have the inspector but it's not something that you want.
Problem is it`s too ugly.
Update your code like below:
public UnityEvent OnSomeEvent;
private SerializedObject serializedObject;
private SerializedProperty property;
// new fields
private UnityEventDrawer EventDrawer;
private GUIContent Content;
private void OnEnable()
{
serializedObject = new SerializedObject(this);
property = serializedObject.FindProperty(nameof(OnSomeEvent));
// new initialization
EventDrawer = new UnityEventDrawer();
Content = new GUIContent(property.displayName, property.tooltip);
}
private void OnGUI()
{
// new drawer
var eventHeigth = EventDrawer.GetPropertyHeight(property, label);
var rect = EditorGUILayout.GetControlRect(true, eventHeigth);
EventDrawer.OnGUI(rect, property, label);
}

how to pass argument to Marionette.CompositeView

how to pass a values dynamically to an Marionette.CompositeView during run time? like in java we create a method like the following
package com.test.poc;
public class SampleMethod {
public int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
SampleMethod method = new SampleMethod();
int firstValue = 90, secondValue = 90;
System.out.println("add : " + method.add(firstValue, secondValue));
}
}
the above is the simple java code anybody can understand like the above how to create and pass arguments to Marionette.CompositeView and work on them?
Best Regards
at the moment you instanciate a view, you can pass whatever arguments you want. normally you pass the model and the collection to be rendered in the compositeView, but you can pass more data if you need.
var MyCompositeView = Backbone.Mationette.CompositeView.extend({
initialize : function (options){
this.dataValue = options.dataValue1;
this.helperObject = options.helperObject;
this.useValues();
},
useValues: function () {
console.log(this.dataValue);
}
});
var helperObject = {
value3 : "I have a value",
value4 : "I dont!"
}; /// a js object literal
var myModel = new MyModel();
var myCollection = new MyCollection();
var myCompositeView = new MyCompositeView({model:myModel,
collection:myCollection,
helperObject:helperObject,
dataValue1:"Hi there"});
notice that Im passing 4 values in the at the time to intanciate the view, and Im reading just two of them, the model and the collection will be handled by marionette, but the other two you can read them in your initialize function.
hope that helps.

Storing parameter templates to create new entities from?

My entity system for my game uses templates to lay out how entities are created. Something like:
EntityTemplateManager.register("zombie",
new PhysicsComponent(10), // speed
new SpriteComponent("zombie"), // graphic
new HealthComponent(100) // health
);
I like this part of the system because it makes sure I don't miss any parameters or screw up their type.
Then I can create a new entity like this:
entity : Entity = EntityTemplateManager.create("zombie");
Pretty straight forward.
What I do now, to actually create the entity, is have the EntityTemplateManager create 1 entity to use as a default, then I call clone() on the entity and it goes through and clone()s all of its components and returns the result.
Actual code:
public function clone() : Entity
{
var components : Vector.<Component> = new Vector.<Component>();
var length : int = _components.length;
for (var i : int = 0; i < length; ++i)
components.push(_components[i].clone()); // copy my local components to the new entity
return new Entity(_name, _type, components);
}
The problem is, every time I create (design) a new component, I have to write (design) a clone() method inside of it AND keep track of all the parameters that were called on the constructor to create a brand new, default stated component.
So I end up with junk like this:
public class ComponentX
{
protected var _defaultName : String;
protected var _defaultStartingAnimation : String;
protected var _defaultBroadcastAnimationEnded : Boolean;
protected var _defaultOffsetX : Number;
protected var _defaultOffsetY : Number;
// other stuff hidden
public function ComponentX(name : String, startingAnimation : String, broadcastAnimationEnded : Boolean = false, offsetX : Number = 0, offsetY : Number = 0) : void
{
super();
_defaultName = name;
_defaultStartingAnimation = startingAnimation;
_defaultBroadcastAnimationEnded = broadcastAnimationEnded;
_defaultOffsetX = offsetX;
_defaultOffsetY = offsetY;
}
public function clone() : Component
{
return new ComponentX(_defaultName, _defaultStartingAnimation, _defaultBroadcastAnimationEnded, _defaultOffsetX, _defaultOffsetY);
}
// other methods
}
I really don't like this -- it's wasteful and error prone.
How could I best store the parameters of the EntityTemplateManager.register() function (including the components' constructors parameters) and use that new storage to create entities from instead?
I've tried some generic clone() methods like with a ByteArray or describeType(), but they don't work with protected / private variables.
Ideas?
Few months ago I wrote my own Entity-Component manager too. I've done something like this (in your case):
EntityTemplateManager.register("zombie", [
{type: PhysicsComponent, attr: {speed: 10}},
{type: SpriteComponent, attr: {graphic: "zombie"}},
{type: HealthComponent, attr: {health: 100}}
]);
Registering a new template works as below:
private static var definitions:Dictionary = new Dictionary();
public static function register(templateName:String, componentDefinitions:Array):void
{
definitions[templateName] = componentDefinitions;
}
The create function retrieves the specified component template and create components like this:
var components:Vector.<Component> = new Vector.<Component>();
// Create all components
for each (var definition:Object in componentDefinitions) {
var componentClass:Class = definition.type;
var component:Component = new componentClass();
// Set default parameters
for (var prop:String in definition.attr) {
component[prop] = definition.attr[prop];
}
components.push(component);
}
// Then create the entity and attach the freshly created components.
// ...

ActionScript - Determine If Value is Class Constant

i'd like to throw an argument error if a particular function doesn't work without a passed value that also happens to be a public constant of the class containing the function.
is there anyway to determine if a class owns a public constant instead of having to iterate thru all of them?
something like this:
public static const HALIFAX:String = "halifax";
public static const MONTREAL:String = "montreal";
public static const TORONTO:String = "toronto";
private var cityProperty:String;
public function set city(value:String):void
{
if (!this.hasConstant(value))
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
public function get city():Strig
{
return cityProperty;
}
currently, for this functionality i have to write the city setter function like this:
public function set city(value:String):void
{
if (value != HALIFAX && value != MONTREAL && value != TORONTO)
throw new ArgumentError("set city value is not applicable.");
cityProperty = value;
}
is this the only way to accomplish this task?
Yes, if you use reflections:
private var type:Class;
private var description:XML;
private function hasConstant (str : String ) : Boolean
{
if (description == null)
{
type = getDefinitionByName (getQualifiedClassName (this)) as Class;
description = describeType (type);
}
for each ( var constant:XML in description.constant)
{
if (type[constant.#name] == str) return true;
}
return false;
}
Note that for this to work, all constants must always be String objects declared public static const.
I was looking for an answer to this question myself and found it annoying that hasOwnProperty() did not work for static properties. Turns out though, that if you cast your class to a Class object, it does work.
Here's an example:
public final class DisplayMode
{
public static const one: String = "one";
public static const two: String = "two";
public static const three: String = "three";
public static function isValid(aDisplayMode: String): Boolean {
return Class(DisplayMode).hasOwnProperty(aDisplayMode);
}
}
I owe this solution to jimmy5804 from this discussion, so hats off to him.
You should be able to use bracket notation to do this. For example:
var foo:Sprite = new Sprite();
foo.rotation = 20;
trace( foo["x"], foo["rotation"]); // traces "0 20"
or more specific to your case:
var bar:String = "rotation";
trace( foo[bar] ); // traces "20"
The only thing you have to look out for here, is that the bracket accessor will throw a ReferenceError if you ask for an object property that isn't there, such as:
trace ( foo["cat"] ); // throws ReferenceError
But it will not throw if you are asking for a static property:
trace ( Sprite["cat"] ); // traces "undefined"
So in your case you might try:
if ( this[value] == undefined ) {
throw new ArgumentError("set city value is not applicable.");
}
EDIT:
Sorry, I was confusing the const's names with their values.
For this to work on your problem you would have to make the String value the same as the const's name, so for example:
public static const HALIFAX:String = "HALIFAX";
then you could use the query as described above and it would give you the desired result.

Get all static variables in a class

I have this ObjectType class which is a class to help me do something like this:
object.type = ObjectType.TWO
//ObjectType.as
package
{
public class ObjectType
{
public static var ONE:String = "one";
public static var TWO:String = "two";
public static var THREE:String = "three";
public function ObjectType()
{
}
}
}
Let's suppose I'm creating a new class and I need a property named type. In that property set function I want to make sure that it's value is one of the ObjectType variables. How can I achieve this?
public function set type(value:String):void
{
for (var o:Object in ObjectType) {
if (value == o)
this._type = value;
} else {
//error
}
}
}
Not performance aware but without modifying anything you can use describeType function to check the static field and get the value back:
function valueInClass(clazz:Class, value:*):Boolean {
return describeType(clazz).variable.(clazz[#name.toString()] == value).length() != 0
}
public function set type(value:String):void
{
if (valueInClass(ObjectType, value)) {
this._type = value;
} else {
//error
}
}
I suppose the second code example you presented doesn't work...
I think it is because you're using the for in loop a little bit wrong.
for (var blah:String in somewhere){
// blah represents a KEY of the somewhere object
// to get the value of this key, use:
var theValue = somewhere[blah];
}
It's the for each loop that loops through the values. But for now I'll use the for in.
Also, it's not in ObjectType, but rather in the class' prototype, that is in ObjectType.prototype.
So, to fix this:
for (var o:* in ObjectType.prototype) {
if (value == ObjectType.prototype[o])
this._type = value;
} else {
//error
}
}
You can solve this using reflection.
A similar question was asked just a few days ago, you should be able to use the same solution, found here.
It should be noted that while the the accepted answer is right, it's also really slow. Not something that you want to do a lot. There are three simpler solutions.
One: Check the value itself:
public function set type(value:String):void
{
if( value != ObjectType.ONE && value != ObjectType.TWO && value != ObjectType.THREE )
return;
}
Obviously, the more constants you have the check the harder this becomes.
Two: Use ints as your constants
Change your ObjectType class to use ints:
public class ObjectType
{
public static var NONE:int = 0;
public static var ONE:int = 1;
public static var TWO:int = 2;
public static var THREE:int = 3;
public static var TOTAL:int = 4;
}
Notice the NONE and TOTAL in there? This makes it easy to check if your value is in the right range:
public function set type(value:int):void
{
if( value <= ObjectType.NONE || value >= ObjectType.TOTAL )
return;
}
You can add more values as needed and you just need to update TOTAL and it'll still work. This needs each value to be in order though.
Three: Use Enums
While Flash has no in-build class for enums, there's a lot of solutions available. Check our the Enum class from Scott Bilas: http://scottbilas.com/blog/ultimate-as3-fake-enums/
Using this as your base class your ObjectType class becomes:
public final class ObjectType extends Enum
{
{ initEnum( ObjectType ); } // static ctor
public static const ONE:ObjectType = new ObjectType;
public static const TWO:ObjectType = new ObjectType;
public static const THREE:ObjectType = new ObjectType;
}
And your check now becomes:
public function set type(value:ObjectType):void
{
...
}
Here, your setter now becomes type safe and will throw errors if anything other than an ObjectType is used.
It turns out that if using an ENUM type of check you should check for the constants property, not variables as showin in the example here:
ActionScript - Determine If Value is Class Constant