Question about States

Hello all!

I was just curious, is there a way to reference a state directly, as in by name? I have an interesting mechanic that I want to add, however it would require me to reference specific states by name that I have created.

For example, it would look like this:

int honor;

// Update is called once per frame
void Update()
{
   HonorUpdate();
}


public void HonorUpdate()
{
    if (currentState == "STATE NAME")
    {
        honor = honor + 1;
    }
    else if (currentState == "Other State Name")
    {
        honor = honor - 1;
    }
    else
    {
        //do nothing
    }
}

(Although I probably wouldn’t put it in update, just have it synced with the buttons to update as it goes)

and then on the UI a bar goes up and down to show the current Honor score. Theoretically, then once you reach a certain honor score it could trigger a special ending or something (planning it out so that you would can only gain enough at the end of the story)

Is this possible? to be able to reference a State by name or number so that I can use the states as triggers? Or is there a better option to accomplish this goal?

/Jeff

edit: replaced the screenshot with a block of code.

Hi Jeff,

You could add a member variable your State.cs class, something like this;

public class State : ScriptableObject
{
    [SerializeField] 
    private string _name;
} 

then you could add a property to expose it publicly;

public class State : ScriptableObject
{
    [SerializeField] 
    private string _name;

    public string Name
    {
        get { return _name; } 
    } 
} 

Each new State asset you create you can now assign a name via the Inspector, then you can use code similar to that which you wrote earlier;

if(currentState.Name == "Example") 
{
    //... 
} 

Not sure what the names are specifically for but there would be ways to tighten this up a bit also.

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

Thanks! I will have to dig into the return types and how to call functions (and variables) from other scripts lecture again to make sure I fully understand that process.

Sorry about the screenshot. I am a total noob at all of this. I have edited the original post to have the code block instead of a screenshot. I will keep that in mind going forward. Thanks again!
/Jeff

No problem, its more for your benefit really, the easier it is for people to reply the more likely you will get replies when you need help :slight_smile:

1 Like

Privacy & Terms