Jump to content

[SOLVED] AnimationEvent - trying to pass reference to a class via objectReferenceParameter


Recommended Posts

While making my animation plugin i started my last (pre-release) phase of adding sound support (Configuration, Animation, Control, Utility and Visual are done)

I want to add sound fx to my animation(s) but my currently employed method of control/tracking animations properties can be a hit-and-miss for sound support

(controlling animations looks fine for now).

It works via PartModule.Update() and PartModule.OnUpdate(). These are not directly visible in PartModule (ie not suggested by VS IDE), but can be used

because it inherits from Monobehaviour.

After successfully doing some basic audio tests, i wanted to write an event handler that receives a reference to my class holding all needed data. And this is where i hit a wall.

AnimationEvent handler is not designed to receive data in arbitrary form.

There is objectReferenceParameter, but i don't know how to reference my class this way, it expects UnityEngine.Object.

If example is needed, i'll try to provide a short version.

 

Reference material

 

 

Edited by fatcargo
Link to comment
Share on other sites

While you could do that (any UnityEngine.Object or derived class ... like PartModule or MonoBehaviour), you don't need to. You already know exactly where your data is. You could cache the PartModule or wherever your data is being kept in whatever script you have as an event listener and call methods directly

Link to comment
Share on other sites

Thanks for reply ! I did just that (and by sheer luck i googled the answer after thinking WHERE i could set these custom paramaters). I've set a several custom variables inside a listener, which get converted/packed into string and then event handler "unpacks" the string and gets data. Lucky for me, event handler lives inside my class so all i had to send is a few ints used as indexes used with my lists/arrays.

What i did was to add, for each animation event, a listener via AddComponent() with customized parameters. All listeners then send a customized message to same single event handler inside my original class. What i became aware of later, is that i was also very very lucky that for each call to AddComponent(), a new instance of listener is created and all parameters are nicely separated. If it were static and all subsequent calls to AddComponent() either failed or have overwritten previous listener, this whole thing would not be possible.

Link to comment
Share on other sites

12 hours ago, fatcargo said:

Thanks for reply ! I did just that (and by sheer luck i googled the answer after thinking WHERE i could set these custom paramaters). I've set a several custom variables inside a listener, which get converted/packed into string and then event handler "unpacks" the string and gets data. Lucky for me, event handler lives inside my class so all i had to send is a few ints used as indexes used with my lists/arrays. 

What i did was to add, for each animation event, a listener via AddComponent() with customized parameters. All listeners then send a customized message to same single event handler inside my original class.

But why?

Here's what I meant:

        private class JellyListener : MonoBehaviour
        {
            private JellyfishDishMk2 _jellydish; // your PartModule here

            private void Awake()
            {
                _jellydish = GetComponentInParent<JellyfishDishMk2>();
                if (_jellydish == null)
                    Debug.LogError("Didn't find expected Jellyfish PM");
            }

            private void FirstAnimationFrame(UnityEngine.Object param)
            {
                var pm = param as JellyfishDishMk2;

                if (pm != null)
                    pm.PlayStartingSound();
            }

            // alternative method: note the lack of argument
            private void LastAnimationFrame()
            {
                _jellydish.PlayEndingSound();
            }
        }
// add events at beginning and end
jellyfish.clip.AddEvent(new AnimationEvent()
{
    time = 0f,
    functionName = "FirstAnimationFrame",
    objectReferenceParameter = this // note that any UnityEngine.Object works here: we could pass a sound, texture, ScriptableObject, MonoBehaviour, ...
});

jellyfish.clip.AddEvent(new AnimationEvent()
{
    time = jellyfish.length,
    functionName =  "LastAnimationFrame" // note didn't use objectRef here, see listener method
});

 

Link to comment
Share on other sites

Thanks for the idea ! It made my code simpler since i needed to add event in my partmodule, then initialize listener for every event, customize its variables and then call my player function from those listeners.

Now i have just one listener and one player. Also, i kept the string var to pass needed arguments via stringParameter. Though it still needs a single call to myAnimation.gameObject.AddComponent<MyListener>() to work.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...