Why not use If/is?

Hey guys… I was happy to have done all of the challenges pretty perfectly so far. However, I believe my code should be more efficient and even easier to read. So that makes me fear that there is something inherently wrong with it. Before I get further into the course and then magically things aren’t working I wanted to run it by you.

    private void OnSelectionChanged()
    {
        if (Selection.activeObject is Dialogue)
        {
            selectedDialogue = Selection.activeObject as Dialogue;
        }
        else
        {
            selectedDialogue = null;
        }
        Repaint();
    }

    [OnOpenAsset(1)]
    public static bool OnOpenAsset(int instanceID, int line)
    {
        if (EditorUtility.InstanceIDToObject(instanceID) is Dialogue)
        {
            ShowEditorWindow();
            return true;
        }
        return false;
    }

I also want to point out(and SURELY a later lecture should cover this) that you really need to set selectedDialogue to null so the window will switch to “No Dialogue Selected” if you click anything other than Dialogue(for now. Until we start actually making things happen).

I am still trying to understand why we are adding an extra variable and casting unless it is purely demonstrative for people to grasp casts better.

Are you referring to the is Dialog? It’s fine. Nothing will break.

selectedDialogue = Selection.activeObject as Dialogue;
Repaint();

works, too. It will set the selectedDialogue to, well, the selected dialogue, and null if the selected item is not a dialogue.

I do believe, however, the code was specifically written to not unload the dialog when you click away. That’s why we don’t set the selectedDialogue to null. We want to keep the dialog loaded while we click around on other items.

Like I said above, we don’t want to unload the dialogue. The extra variable allows us to check if we have a new dialog, and switch to it if we do

1 Like

This is more my style, short and effective.

    private void OnSelectionChanged()
    {
        selectedDialogue = null;
        if (Selection.activeObject is Dialogue dialogue)
            selectedDialogue = dialogue;
        Repaint();
    }
2 Likes

Ah. Yeah later on I figured the not unloading would be handier. It just behaves weirdly when using it for this specific lecture. You wouldn’t expect it to be positive about a dialogue when you don’t have one selected.

All good. Thanks for the feedback guys. These ways make more sense than casting is my main point. I’ll just keep an eye on it as the code evolves to see if other ways make more sense. But for now, what I have makes sense in my brain which is important for down the line integration.

I believe Sam starts introducing the is clause either later in this course or in the next one. Unity spent so long so far behind in the C# code versions (It’s all the way up to C#7 now, weee) that we tend to forget to use the newer syntactic sugar when it’s available. I don’t remember precisely when Unity got is implemented.

It’s a handy feature, and is one of the best features for error checking and versioning in the saving system. For example: Suppose in Mover, you were storing the character’s position as a SerializableVector3, but you then realized later that you really should save the rotation as well… In this case, you could create a struct containing two SerializableVector3 and pass that in CaptureState, but then when you RestoreState with an old save file, the game will crash… Unless…

if(state is SerializableTransform sTransform)
{
     //restore position and rotation
} else if(state is SerializableVector3 sVector3)
{
    //restore the position from sVector3
}
If neither thing is the case, then the method exits gracefully, no harm, no foul.
1 Like

Hah! Good looking out, Brian. Tying back to a previous question of mine. You are going places, sir.

But yeah I absolutely do forget how behind Unity can lag. I was having this conversation awhile back in the discord with someone. I only even use C# with Unity but still somehow end up figuring out stuff that is only recently available.

Side note: I am loving this course so far. It’s very fascinating learning to make an editor tool. I have my own Editor hack that allows me to have inverted y for my mouse in the scene window when you hold RMB to zoom around like flying in an FPS. My brain is just wired that way (as I am sure comes as no surprise given my volume and variety of questions here since migrating to the official site).

Unity version 2020.2 is on C# 8 and from version 2021.2 onwards on C# 9, which is much more than I can say for my day-to-day where I broke a build yesterday because I inadvertently used C# 7.3 code. :unamused:

1 Like

OOF! Sorry to hear that :confused:

But not as bad as for my last full prototype I had worked on when I was using SteamVR API… And I boneheadedly updated from 2019 to 2020, forgetting to backup the project. When in 2020 the SteamVR API went bye-bye and OpenXR/VR was the new norm. It was the land of compiler errors and a very long process of manually getting everything out and back into a 2019 project :frowning:

Steam and Unity then had a sort of “it’s their fault there is no compatibility” period for a long time after that. I really enjoyed SteamVR’s API. It was sooooo easy to set up controls. But I am looking forward to OpenVR soon after I finish these courses. I even bought a VR character controller/interaction setup so I can hit the ground running!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms