Announcement

Collapse
No announcement yet.

Call Unrealscript function based on string?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Call Unrealscript function based on string?

    I have a scaleform inventory system, and I want actionscript to be able to send calls to Unrealscript to run specific functions on inventory items.

    The HudGfx function would look something like this:
    Code:
    simulated function PerformInvItemAction(String ItemString, String Action)
    {
        local MyInventoryItem Item;
        Item = class<MyInventoryItem>(DynamicLoadObject("MyGame.MyInventory"$ItemString, class'class'));
       
        //This is where I'm left wondering what to do:
        Item.Action();//I know this won't work.
    }
    I've been reading up on delegates, which seem like they could be a good choice, but i'm unsure of how to use them in this scenario.

    #2
    Delegates seems to be a good choice indeed. However, the base delegate must be somehow flexible.

    Something like this:
    Code:
    delegate OnAction(Actor Sender, Optional int switch, Optional Object OptionalObject);
    
    simulated function PerformInvItemAction(String ItemString, delegate<OnAction> Action)
    {
        local MyInventoryItem Item;
        Item = class<MyInventoryItem>(DynamicLoadObject("MyGame.MyInventory"$ItemString, class'class'));
       
        Action(self,, Item);
    }
    
    simulated function Action_IncreaseLifeSpan(Actor Sender, Optional int switch, Optional Object OptionalObject)
    {
       if (Inventory(OptionalObject) != none)
       {
           Inventory(OptionalObject).LifeSpan += 20;
       }
    }
    
    exec function TestPerformInvItemAction()
    {
        TestPerformInvItemAction("MyPackage.MyItem", Action_IncreaseLifeSpan);
    }

    Comment


      #3
      Thanks for the response @RattleSN4K3. In you example it doesn't seem to be evoking the delegate from a string, which is what i'll have passed from actionscript.

      I'm not at my machine to test this atm, but in your example would you be passing a string as the delegate reference, as I not sure how I go from string to function?

      Code:
      exec function TestPerformInvItemAction()
      {
          PerformInvItemAction("MyPackage.MyItem", "Action_IncreaseLifeSpan");
      }
      ?

      Comment


        #4
        You could try to dynamic load the delegate but i'm not sure about that. Another option would be using SetTimer.

        Another tricky option would be creating Action classes.
        Code:
        class MyActionBase extends Object;
        
        static simulated function ExecuteAction(Actor Sender, Optional int switch, Optional Object OptionalObject);
        Code:
        class MyAction_IncreaseLifeSpan extends MyActionBase;
        
        static simulated function ExecuteAction(Actor Sender, Optional int switch, Optional Object OptionalObject)
        {
           if (Inventory(OptionalObject) != none)
           {
               Inventory(OptionalObject).LifeSpan += 20;
           }
        }
        Code:
        simulated function PerformInvItemAction(String ItemString, string ActionClassString)
        {
            local MyInventoryItem Item;
            local class<MyActionBase> MyAction;
        
            // dynamically create the static action class and call the static base function
            MyAction = class<MyActionBase>(DynamicLoadObject(ActionClassString, class'class'));
            if (MyAction != none)
            {
                Item = class<MyInventoryItem>(DynamicLoadObject("MyGame.MyInventory"$ItemString, class'class'));
                MyAction.static.ExecuteAction(self,, Item);
            }
        }
        Which can be called with:
        Code:
        PerformInvItemAction("MyPackage.MyItem", "MyPackage.MyAction_IncreaseLifeSpan");




        You could also setup a array with delegated functions.
        Code:
        struct SActionFunctionInfo
        {
            var delegate<OnAction> Action;
            var string ActionString;
        }
        
        delegate OnAction(Actor Sender, Optional int switch, Optional Object OptionalObject);
        
        var array<SActionFunctionInfo> MyFunctions;
        Delegated functions:
        Code:
        simulated function Action_IncreaseLifeSpan(Actor Sender, Optional int switch, Optional Object OptionalObject)
        {
           // !!! Important
           // The executed code on this class is always in the DEFAULTACTOR content
           // typecast sender to this class in order to change settings of the instanced class
        
           if (Inventory(OptionalObject) != none)
           {
               Inventory(OptionalObject).LifeSpan += 20;
           }
        }
        The perform code
        Code:
        simulated function PerformInvItemAction(String ItemString, string ActionString)
        {
            local int i;
            local MyInventoryItem Item;
            local delegate<OnAction> Action;
        
            for (i=0; i<MyFunctions.Length; i++)
            {
                if (MyFunctions[i].ActionString ~= ActionString)
                {
                    Action = MyFunctions[i].Action;
                    break;
                }
            }
        
            if (Action != none)
            {
                Item = class<MyInventoryItem>(DynamicLoadObject("MyGame.MyInventory"$ItemString, class'class'));   
                Action(self,, Item);
            }
        }
        And in your properties
        Code:
        defaultproperties
        {
            MyFunctions.Add((ActionString="Action_IncreaseLifeSpan",Action=Action_IncreaseLifeSpan))
        }

        Comment

        Working...
        X