Alternate GetActionName

Instead of having the GetActionName then having to generate the method in each of the MoveAction and SpinAction scripts. I found it much simpler to just do the following:

[Header("Base Action Information")]
[SerializeField] protected string actionName;

protected Unit unit;
protected bool isActive;
protected Action onActionComplete;

protected virtual void Awake()
{
    unit = GetComponent<Unit>();
}

public string GetActionName() { return actionName; }

This provides the same results with less code and I, personally, feel it is more User friendly since we can now set the Action Name in the inspector.

1 Like

There are many ways to do this. I don’t really see the need to change the name in the inspector, so mine looks like this:

public string GetActionName() => "Shoot";
2 Likes

Is that an arrow function? :thinking:

I’ve never seen an arrow function in C# before, only in JavaScript. It doesn’t mean they don’t exist in C#, I’ve just never seen one in C#.

Yes, it’s a Lambda function. If you watch from around 06:00 in the LevelGrid video, Codemonkey explains about them.

1 Like

With that you can indeed set the name in the Editor, however it also means you can give the exact same Action class a different name which would be very confusing. Whereas if you define the name in the code then all instances of that class will share the same name.

2 Likes

One other problem with this solution in this specific case is that you lose the compiler warnings when you forgot to change the name for a new action. (As in, if you just don’t define GetActionName() in a new class when the method is purely abstract the compiler would warn you, but with this you have no warning).

But on the other hand your solution might be pretty useful for debugging, so may still be worth it sometimes :slight_smile:

1 Like

something here Im worried about is translation.
I guess you would call the name from another script on the return once you start dealing with that sort of stuff

1 Like

@Gamesdisk This is actually easy to implement when you have a method that is called to get the label. Instead, you just get the label from your localisation system and return that. If you use a serialized field to be able to set it in the editor, getting a localised string is less simple. So, I would not worry about that.

Indeed, it would be the UI method’s responsibility to take care of handling localization by querying the localization system “hey, what’s the label string to put on a button for a MOVE action?”
In a more complex UI system one might have some base class for standard elements that would have some UpdateLabel() method calling for the localized string, so it would be all in one place…

Privacy & Terms