UPDATE- RPG Current Dependencies & My version of Suspicion + QUESTION

Hello Everyone!

First here is where we are (I previously forgot to link Mover and Fighter to ActionScheduler).

  • AIController now also uses ActionScheduler
    *Also "Requires " is hopefully a bit clearer to read

Also Here is what I did for the challenge, which is a bit less efficient since I GetComponent too often:

image

image

QUESTION: CancelCurrentAction() doesn’t work for me:The enemy keeps following me DURING investigation time instead of pausing.
I have to have to call the cancel methods from Fighter and Mover specifically to produce the desired behaviour.
My enemy does have the scheduler script and I am also using the Core namespace. Any suggestion?
Cheers

1 Like

@Nina

Make sure that Fighter.Attack() includes the line:

GetComponent<ActionScheduler>().StartAction(this);

I don’t have your Mover script, but I’m assuming that FocusOnMoving() is the equivalent of our StartMoveAction() method. Make sure that this script also has the same line.

1 Like

Good morning!

Thanks for the suggestion. But those methods did have the line
In Mover:
image

And Fighter:
image

For now it is not breaking anything so maybe I ought to move on and fix it later…
Thank you for your help once again!

If the ActionScheduler isn’t cancelling the existing actions when needed, it is broken.

Paste in your Action Scheduler script (hint: It’s easier,and more readable to those on mobile devices to actually past in the text instead of a screenshot… first type the ` line (that’s a backwards apostrophe) three times on it’s own line and press enter, then paste in your copied code, and then on a new line, type the backwards apostrophe three more times to end the code bock.
Alternatively, you can select the code you’ve pasted in and press the </> button in the format bar.)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Core
{
    public class ActionScheduler : MonoBehaviour
    {
        IAction currentAction;
        public void StartAction(IAction action)
        {
            if(currentAction == action) return;
            if(currentAction!= null)
            {
                currentAction.Cancel();
            }
            currentAction = action;
        }
        public void CancelCurrentAction()
        {
            StartAction(null);
        }
    }
}

Here is the script! fun fact I was trying the apostrophes in the script itself before doing it here

Hmmm… I can’t explain why CancelCurrentAction() would not be cancelling the Fighter or Mover script. There may be an issue in the AI logic… You can test this by replacing our AI script (you can get it from the repository link against the lecture) with yours. (Comment out the contents within public class AIController, and paste in the contents from the course). You may have to do a little refactoring to make the method names match your method names.

1 Like

Hello!
Thank you for the suggestion, I will try right away!

Alright, so the problem is when I originally used CancelCurrentAction(), I cached a reference to the ActionScheduler script

scheduler.CancelCurrentAction();

It didn’t work.
But when I used it exactly as it is in the repository,

GetComponent<ActionScheduler>().CancelCurrentAction();

It works! It has to do with the order of execution I believe. I notice the same thing with Mover. Sometimes when I cache the reference it doesn’t work. I have to GetComponent exactly when I need it.

Thank you very much for your help, I like to think I am a tiny bit smarter tonight!

Where are you caching the reference? The best place to cache component references that are on the same GameObject is in the Awake() method.

void Awake()
{
     scheduler = GetComponent<ActionScheduler>();
}
2 Likes

Good morning Brian,

I was caching everything in Start()… thank you for this I should have taken my own hint from the previous post. It works fine now !

1 Like

Privacy & Terms