Entity Does Not Exist

Blitz3D Forums/Blitz3D Programming/Entity Does Not Exist

MajesticSpaceBen(Posted 2013) [#1]


I receive an "Entity does Not Exist" error when the ToggleDoor function is called, specifically when it tries to move the testCube. I cannot figure out why it's doing this.


Yasha(Posted 2013) [#2]
You've got a case of scope confusion going on. The variable testCube in the main program is not the same as the variable testCube within the function - they just happen to have the same name. Functions create a new scope (set of names) with their own set of variables that "shadow" those outside; since testCube in the function is never assigned, its value will be 0 on every invocation. (Variables inside the function are also "reset" every time it is called, so you can't just move the CreateCube to inside the function as the cube will be lost when it returns.)

A very similar question to this received a wonderfully in-depth answer from Rob the Great, which you can read here: http://www.blitzbasic.com/Community/posts.php?topic=101520#1206238 , with some examples of how to resolve this situation.

(Important to note that this isn't a bug, introducing new scopes is an important part of what functions are supposed to do.)


Rob the Great(Posted 2013) [#3]
I think scopes are the most under-explained concept in programming, next to object oriented programming. I didn't even know about the existence of scopes when I first started programming. It was only when I encountered similar problems as the one above that I asked others on this forum what was going on.

To me, it makes the most sense to think of a function as a separate, mini-program within your main program. Once you're inside a function, everything created inside is specific only to that function and has nothing to do with anything else outside of the function. Names are irrelevant, which is why you can have two variables named the same thing and holding different values. The only exceptions are with global variables, types and their fields, and passing local variables to and from that function.