Variable Question. [Simple] & [JS]

I’m new to programming and I was wondering if having to many Variables and Functions in one script will slow down the performance/UnityEngine? If not what would it affect? I heard that if you want to do these sorts of things (Having multiple tasks in a single script) it’s better to program in C++. Is that true? Thanks!

  1. First and foremost, I really wouldn’t worry about having too many variables/functions in a game. Variables are relatively small and don’t take a up a great deal of space/resources in a game unless you are using things like arrays. Arrays are what you have to worry about specifically with gaming, anything where you are duplicating whole game objects/classes is going to become a problem down the road if you don’t manage how many are in your game. For example, shooting a bullet and never destroying it when it hits anything or goes too far off screen can become a problem. Your came can have hundreds of normal variables before it would really have any negative issues.
  2. Functions do not have a negative impact on game performance unless they are doing some kind of repetitive/cumbersome task. Functions and loops deal with the mathematics behind “computer algorithms”. I’ve been in college for five years now on programming, computer science, and the works. I took a whole course on the science behind algorithms. Basically the demanding parts of your game are going to be in repetition/loops. There are ways to calculating how fast (or “efficient”) they run using summation formulas. But overall, a function just isn’t going to slow your game too much unless it’s performing a repetitive task.
  3. C++ is one of the superior programming languages out there, if not the most superior for general programming and gaming applications. Remember that Unity, though, does NOT use C++. It uses Javascript, Boo, and C#. I programmed games in C++ for a while in Dark GDK for college. C++ is very fast and one of the reasons it blows all other languages out of the water is because it avoids using several user-friendly “error checks” in programming. For example, in some languages if you write a loop a certain way, some languages will detect there is an error in the programming (can’t remember the exact example of this). Anyway, in most languages, you would get a compiler/console error notifying you there is an error in the code. C++ doesn’t really check for these errors making it the faster language. This is just one example of why it is faster. On the downside, not having that error checking can make it a hassle to write and code in C++.
  4. In addition, I believe Unity (and other frameworks) steer away from C++ for a couple reasons. Number one, that lack of “error checking” built in makes it very difficult to sometimes diagnose problems in your code. Another reason is when you write classes, they require “header” files to store variables/functions in. This is not only confusing to newcomers like yourself, but it also gets very annoying to manage these header files when you have a lot of them. Another reason C++ is avoided is because it does not have “string” variables. Strings are lovely to work with. You can store things like a players name, enemies name, or something else in your code with it. In C#, you have to use an array of “characters” instead, and it gets VERY annoying. There are some workarounds to this, but ultimately C++ does not have a native string function built in. This is again of the things that makes it faster, but also makes it harder to program in. Lastly, computers (and mobile devices) are getting so fast these days, that the little performance boost you get from C++ isn’t really worth using it, especially for the many simple games that people make in Unity. Games like Angry Birds do not need a dramatic amount of resources to run properly.

Might I ask why did you go the Javascript route for your game? I know Javascript extremely well as my main job is working with website development. But in terms of programming a game, unless you’re going for the web player it doesn’t make sense to me to use it. I strongly encourage you to bite the bullet and learn C# instead. C# is a very powerful and popular languages these days. It’s used in ALOT of things from computer applications to games. I never understood the reason why so many people like it on here, and another thing is I wouldn’t doubt that it is not quiet as fast as Javascript.
Anyway I hope I answered all your questions. I am a die hard programmer of eight years now outside of Unity. I don’t quiet have the “geekiness” for it like others and remember all the fine details, but I think I answered your questions. If you want to know more about variables and how they work in computer memory, I would pick up a good book. Most introduction programming books explain how variables are stored in computer memory and will also teach you about the different data types (like int and float and how much space numerical variables take up and how they are stored). Have fun!