So people tell me not to use == when comparing strings. Whats so wrong with that? Is there some clean way of doing it?
Depends on what you’re doing. The main issue with comparing strings is that more often than not, that’s not actually what you really want to do. Traditionally, people were against it because it would take a long time to do, but nowadays that doesn’t matter quite as much. Traditionally, people were also against it because you would introduce code security problems very easily, but that isn’t so much of a problem if you are using something that manages strings for you.
Nowadays, the main problem is that you might compare strings when what you really want to do is tell that, for example, you are checking a game unit’s identity, and it’s better (as well as faster) to use an ID number for that sort of thing, because what if you change the string that identifies the unit? Or what if the unit has the same identifier as another? Or what if you typo something, which means the error can’t be caught at compile time? It’s way easier to design things so that the ID number will never change, and will always distinguish things you want to be different.
(This isn’t necessarily something you learn in a typical programming course, but it’s an important part of database design, something I think not enough programmers study. A class in database design can be pretty expensive, though.)
So just think about what you are really doing before you ever compare strings. On modern computers, as a coding shortcut, it doesn’t actually hurt as much as experienced people might lead you to believe. What you don’t want to do, however, is put too much load on the system, especially in a game where you could be doing the same calculations many times a second. Strings can kill you there.
Also, someone told me to use enums.
Probably should. Use enums any time you use a hard-coded number to mean one of a limited number of distinct choices (red, green, blue parts of a color; X, Y, Z in a 3D coordinate; archer, mage, knight as a unit type...). You never want to go hunting around for hardcoded numbers.
And I get kinda annoyed when writing those for loops. So is there a more efficient way of doing it?
In C++? In terms of time it takes you to write them out?
Noooot exactly. It’s one of the first things I’d love to see changed in C or C++. Some ways to do this are in C++ already, such as std::for_each or “range-based for loops” (a C++11 feature). Unfortunately, these on their own will not be sufficient (without writing a class) for one of the single most unnecessarily repetitive tasks, which is to count through an integer range. The Boost library does provide boost::irange, which can be used in conjunction with C++11 range-based for loops to function this way (example
). This isn’t a thing people normally use, but I think this will change eventually.
You have perhaps accidentally stumbled onto why “functional programming” has been all the rage lately, and why these features have been added to C++. Again, it’s all about meaning. In many for loops, you don’t necessarily care that i is first 0, then
2 and so forth. Perhaps you just want the same thing done to each part of a set of data. Perhaps, if you are working with a very large set of data, you want it to be split up into multiple sets and sent off to different processors so that the work gets done faster. C++ is not quite there when it comes to supporting this at the language level, but it is possible this might change eventually.
I also need to start organizing my code. Got any tips for that?
It’s hard, but worth it. When you learn a language, you also learn about its design. Thus, you learn how to split things apart when they get too big. Unfortunately, a sufficiently thorough study of this is could take multiple college-level courses to learn. Designing programs on a higher level is a pretty hard thing; that’s what they pay software engineers for.
So for now, continue to focus on things like maintaining consistent spacing and making sure all the brackets and semicolons are in the right place, and making sure there is no = where you mean ==. If you never forget these things, that’s one step to “cleanliness” (a bit of a subjective term, that).
Splitting things off into functions if they need to be found in more than one place, or if reading the name is all someone who reads a section of code really needs to know about that section.
You do not use classes here despite using C++, which is not necessarily a bad thing in my opinion (I’m a little bit of an OOP skeptic, actually), but consider learning how to use them. If you have a structure to data and you have functions that only really concern that structure, that’s the kind of thing you should make a class.
Also, learn how to split code into multiple files. As a rule of thumb, you should do this as a way of hiding code that doesn’t matter a whole lot to what you’re coding right now.
And I would like to know some tips for the enemies targeting the players. Because I would have to make all the enemies have the same amount of abilities because of that etarget var.
This, too, is a matter of designing your data, and it is a difficult thing to understand the overall principles of that.
You could, for instance, maintain (or generate) two lists of allies and enemies instead of (or as well as) treating that as a status someone has. Then you can go through those lists if you need a target. You might even sort those lists by some attribute if you only want to target players with certain statuses. It’s all up to how you want to organize it.