Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Storing functions with arguments as String

Guru ,
Jul 28, 2014 Jul 28, 2014

I need to store a function with arguments in one string.

Without arguments, you can simply do sth. like that:

functionName:String = "hello";

this[functionName]()

function hello():void{

   "Hello, World";

}

But as soon as you introduce arguments it`s getting tricky.

I came up with this:

function getArgsFromFunction(_function: String): Array {

    var argstart: int = _function.indexOf("(");

    var argend: int = _function.indexOf(")");

    var argstring: String = _function.substr(argstart + 1, argend - argstart - 1);

    var args: Array = argstring.split(",");

    return args;

}

function getNameFromFunction(_function: String): String {

    var nameEnd: int = _function.indexOf("(");

    var nameString: String = _function.substr(0, nameEnd);

    return nameString;

}

var f:String = "showMessage(A,B)";

function showMessage(_args: Array): void {

    trace(_args);

}

var _funct: Function = this[getNameFromFunction(f)];

_funct.call(this, getArgsFromFunction(f));


but its not very versatile, not very robust, and seems unnecessarily complicated.

Anyone know a quicker way to get this done?

TOPICS
ActionScript
588
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 28, 2014 Jul 28, 2014

function parseF(s:String):void{

    var a:Array = s.split("(");

    var arg:Array = a[1].split(",");

    this[a[0]](arg[0],arg[1].substr(0,-1));

}

Translate
Guide ,
Jul 28, 2014 Jul 28, 2014

Take a step back and explain what it is you're trying to do. You are correct that this is a terrible way to do it (whatever "it" is), but the ickness starts as "I want to store everything as a string" and permeates from there.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 29, 2014 Jul 29, 2014

I have a static class that mainly functions as a mediator between my main and other classes. This static class has a dictionary that looks like this:

    public static var dict:Dictionary = [
   {index: 1, effect: "upgrade(front,1)",condition:"!isFront(empty,self)" }....}]

I want to be able to call the stored function like this:

Function(dict[0].effect).call()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 29, 2014 Jul 29, 2014
LATEST

Here are some cleaner alternatives:

Instead of using a static class as a mediator, create an EventDispatcher and pass a reference to it to the instances that need to communicate (in other words, an event bus). Have them listen on the event bus and call their own methods when an event that concerns them occurs.

Go ahead and register your instances (but not with a static Class--that's just asking for trouble), but have them implement an Interface. That way, you already know what methods are available and what arguments they take.

As a variation on the above, create a "communication" method that takes a generic object that's defined by the Interface supplied by your instances. Pass in the object and let each Class worry about what parts of the Object it cares about.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2014 Jul 28, 2014

function parseF(s:String):void{

    var a:Array = s.split("(");

    var arg:Array = a[1].split(",");

    this[a[0]](arg[0],arg[1].substr(0,-1));

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jul 29, 2014 Jul 29, 2014

That looks a lot more compact, thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 29, 2014 Jul 29, 2014

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines