Scene loader string for loading last scene

In previous lectures we were able to call scenes based on their number in relation to the build. For example, LoadScene(0). Which makes me think that there is an array of scenes that we can call on a specific value for. Like if I needed the second scene I could specify LoadScene(1).

My question is why can’t we call those scenes in a reverse index? So instead of specifying LoadScene(“Game Over”) I could just say that I need the last scene in the build. LoadScene(-1). Which is a logic that works in other programming languages.

I already tried the code and it didn’t work, but I just wanted to understand why.

Hi Emilio,

The LoadScene method either takes in a string or an int. The int value gets used as an index. In C#, indices start at 0. -1 does not exist.

I don’t know any other programming language which magically interprets -1 as some kind of a “reverse index” without any reference index. In which language is that the case?

Python has the ability to reverse index. But it does it through the programming logic, not magic.

I don’t know Python (in the sense of: I cannot write Python code; of course, I have heard about Python) but if that language has got methods, knows methods overloading and has got an int type, -1 does not automatically reverse anything. It is very likely that there is a distinct syntax and a specific context in which Python interprets “-1” as a command instead of an ordinary int value. I took a brief look at the Python syntax, and it does not look as if one can simply use -1 for the index, and reverse indexing happens.

For this reason, unless the code inside a method checks for < 0, I doubt that Python would do anything else than C# in the case of LoadScene. But I don’t know Python, so I might be wrong.

And even if C# had the same functionality as Python: All we know about LoadScene is the method signature and the description in the API. We cannot expect anything else to happen just because something happens somewhere else.


Updated Wed Nov 25 2020 12:41

I did some more research to figure out what you could have meant by -1. As it seems, [-1] represents the last element of an array in Python. The context I was missing in your question was the brackets: [ ]. -1 is just an int. In C#, [-1] would throw an IndexOutOfRangeException error.

This topic was automatically closed after 28 hours. New replies are no longer allowed.

Privacy & Terms