Sprite Swap Tips/Suggestions

Discuss issues pertaining to the Creature Animation Editor here.
blady
Posts: 5
Joined: Tue Sep 12, 2017 9:52 pm

Re: Sprite Swap Tips/Suggestions

Post by blady » Sat Sep 30, 2017 2:01 pm

Hey Chong,

I got an issue with using the creature pack with object pooling.
Everyone works for the first time when I spawn an enemy, but whenever I disable the gameobject/renderer/meshrenderer for a sec it looses a conenction in CreaturePackStateMachineBehavior: creature_renderer.pack_player = null.
If I go to the creature_renderer itself, it still had a pack_player, but when trying to access it from the CreaturePackStateMachineBehavior, there is no pack_player to be found and animations won't be activated animore (add a return function means the last active animation keep on looping).

chong
Posts: 706
Joined: Thu Feb 19, 2015 2:21 am

Re: Sprite Swap Tips/Suggestions

Post by chong » Sat Sep 30, 2017 4:44 pm

Hello,

I am wondering if it has something to do with what the CreaturePack object does when it is in the Awake() call?
I think for pooling the following will need to be possibly modified.

Can you please try the following in CreaturePackRenderer.cs, in InitData(), line 111, make this change:

Current it is:

Code: Select all

        if(pack_asset) // We will change this line
        {
            var pack_loader = pack_asset.GetCreaturePackLoader();
            pack_player = new CreaturePackPlayer(pack_loader);
            pack_player.setActiveAnimation(active_animation_name);
        }
Change to:

Code: Select all

  if(pack_asset && (pack_player != null) // Make it like this 
Does this help in addressing your issue?

blady
Posts: 5
Joined: Tue Sep 12, 2017 9:52 pm

Re: Sprite Swap Tips/Suggestions

Post by blady » Sat Sep 30, 2017 5:22 pm

You are a life saver man! I tried so many things, but this one seemed to solve it!
Couple of things though:
1. I added a debug.log to the start of the Innit and it seems to run quite a lot (every frame). This might just be my code though.
2. The line you gave me is faulty. I chanegd it to:

Code: Select all

 if(pack_asset && (pack_player == null)) // Make it like this 
If you keep it at: != , it won't even run the first time.

Thanks again! You are doing wonders :D

chong
Posts: 706
Joined: Thu Feb 19, 2015 2:21 am

Re: Sprite Swap Tips/Suggestions

Post by chong » Sat Sep 30, 2017 5:27 pm

Hello,

Oops sorry yes I made a typo :P Ok sounds great, let me make the patch and check it into the official GitHub Repo :)

Cheers

blady
Posts: 5
Joined: Tue Sep 12, 2017 9:52 pm

Re: Sprite Swap Tips/Suggestions

Post by blady » Mon Oct 02, 2017 8:08 pm

Welp... I seems like it still doesn't work.. I remains null, even after this fix.
I thought it worked, but that was still within the "bad" framework of instantiating them when needed.
Any other suggestions?

EDIT:
To clarify, I don't disable them anymore. This is what I do and what happens:
1. I create a bunch of certain animations (object pool), I don't disable them or anything, just put them as a child of object pool on location 999,999
2. The Enemy script requests an animation and the animation that fits will put put as a child of that enemy (location 0,0), everything works fine
3. Enemy dies, I return the animation to the objectpool as a child
4. When a new enemy needs that animation, it requests and now the animation is stuck on a loop of the last animation that played

The object pool doesn't disable ANYTHING! It only parents the animation to an object and hold a list of all currently used animations.

chong
Posts: 706
Joined: Thu Feb 19, 2015 2:21 am

Re: Sprite Swap Tips/Suggestions

Post by chong » Mon Oct 02, 2017 9:17 pm

Hello,

Actually I am trying to understand what is going on in your scenario.
One other idea you can try is to reassign the pack_renderer attribute in the CreaturePackStateMachineBehavior to the actual pack_renderer of your character when you activate or use that character again.

It sounds like the CreaturePackStateMachineBehavior is referencing an old or invalid pack_renderer. So if you can force it to reference the newly updated one, will that work for you?

blady
Posts: 5
Joined: Tue Sep 12, 2017 9:52 pm

Re: Sprite Swap Tips/Suggestions

Post by blady » Mon Oct 02, 2017 9:30 pm

Alright, first of all, that works!
There is something weird though (don't worry, everything is fixed for me now ;) )
This line at the start of the OnStateEnter function worked for me: pack_renderer = animator.GetComponent<CreaturePackRenderer>();
The odd thing is that pack_renderer is not null at that stage, but it's linked to an invalid component at that stage.
The class for me now looks like this and it works.. Adding more null checkers will destroy it again because it still finds the reference, altough faulty:

Code: Select all

public CreaturePackRenderer pack_renderer;
    public string play_animation_name;
    public bool do_blending = false;
    public float blend_delta = 0.1f;
    
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        pack_renderer = animator.GetComponent<CreaturePackRenderer>();
        if (pack_renderer.pack_player == null)
            return;
        if (!do_blending)
        {
            pack_renderer.pack_player.setActiveAnimation(play_animation_name);
        }
        else
        {
            pack_renderer.pack_player.blendToAnimation(play_animation_name, blend_delta);
        }
    }
In my own version I have a Debug.log in the return function as well to debug any more errors to see what's going on. My problem before was that pack_renderer still had a reference, but pack_player was null. The linked gameobject from pack_renderer was still the correct one, but apperently still faulty (no idea how that works.)

I hope either this code will help you with fixing the bug for anyone else or are able to find the root of the bug.

Thx again for your help!

chong
Posts: 706
Joined: Thu Feb 19, 2015 2:21 am

Re: Sprite Swap Tips/Suggestions

Post by chong » Mon Oct 02, 2017 9:59 pm

Hello,

Sounds great! I believe it has something to do with the intricacies of how Unity handles object lifetimes with respect to the StateMachineBehaviour. This again depends on your use case and probably many other variables :)
In any case, it's definitely great to hear that it is working out for you! Maybe in the future if you figure anything else out, you can make a pull request in the long run.

Cheers

Post Reply