Search code examples
c#animationunity-game-enginecallbackitween

How to use oncompleteparams in Unity iTween plugin?


I have code:

iTween.MoveTo(
gameObject,
iTween.Hash("x",_x,"z",_y, "time", 2.0f, "easetype",
iTween.EaseType.easeInExpo,
"oncomplete", "afterPlayerMove",
"oncompleteparams", iTween.Hash("value", _fieldIndex)
));

but I don't know how to use oncompleteparams. There is no example in the official manual.

How do you use oncompleteparams?


Solution

  • Here is the direct doc for the Itween.MoveTo function.

    enter image description here

    oncompleteparams expects Object as argument. This means that almost any datatype can be passed in to it. For example, string, bool, int, float, double, and object instance are one of datatypes that can be passed to it.

    On the callback side, you make the callback function take Object as parameter. Inside the callback function, you cast the Object parameter to the type of datatype you passed to it.

    Example:

    "oncomplete", "afterPlayerMove",
    "oncompleteparams", 5)
    

    Callback:

    public void afterPlayerMove(object cmpParams)
    {
        Debug.Log("Result" + (int)cmpParams);
    }
    

    As you can see, we are passing 5 to the oncompleteparams function and 5 is an integer. In the afterPlayerMove callback function, we cast it back to integer to get the result.


    In your example, you used iTween.Hash for the oncompleteparams so you must cast to Hashtable since iTween.Hash returns Hashtable. After that, to get the values in the Hashtable, you have to cast to that type too.

    "oncomplete", "afterPlayerMove",
    "oncompleteparams", iTween.Hash("value", _fieldIndex)
    

    Callback:

    Assuming that _fieldIndex is an int.

    public void afterPlayerMove(object cmpParams)
    {
        Hashtable hstbl = (Hashtable)cmpParams;
        Debug.Log("Your value " + (int)hstbl["value"]);
    }
    

    Finally, your code is unreadable. Simplify this code to make it easier for others to help you next time.

    Complete simplified Example:

    int _x, _y = 6;
    
    //Parameter
    int _fieldIndex = 4;
    float floatVal = 2;
    string stringVal = "Hello";
    bool boolVal = false;
    GameObject gObjVal = null;
    
    void Start()
    {
        Hashtable hashtable = new Hashtable();
        hashtable.Add("x", _x);
        hashtable.Add("z", _y);
        hashtable.Add("time", 2.0f);
        hashtable.Add("easetype", iTween.EaseType.easeInExpo);
        hashtable.Add("oncomplete", "afterPlayerMove");
    
        //Create oncompleteparams hashtable
        Hashtable paramHashtable = new Hashtable();
        paramHashtable.Add("value1", _fieldIndex);
        paramHashtable.Add("value2", floatVal);
        paramHashtable.Add("value3", stringVal);
        paramHashtable.Add("value4", boolVal);
        paramHashtable.Add("value5", gObjVal);
    
        //Include the oncompleteparams parameter  to the hashtable
        hashtable.Add("oncompleteparams", paramHashtable);
        iTween.MoveTo(gameObject, hashtable);
    }
    
    public void afterPlayerMove(object cmpParams)
    {
        Hashtable hstbl = (Hashtable)cmpParams;
        Debug.Log("Your int value " + (int)hstbl["value1"]);
        Debug.Log("Your float value " + (float)hstbl["value2"]);
        Debug.Log("Your string value " + (string)hstbl["value3"]);
        Debug.Log("Your bool value " + (bool)hstbl["value4"]);
        Debug.Log("Your GameObject value " + (GameObject)hstbl["value5"]);
    }