Hey, I am new at coding and I started learning Unitiy and C# while making an incremental game because this kind of games use a lot of coding and I was not planning to focus on assets and animation (Play Idle accelerator, a free online game on Kongregate) the problem is that after 6 months testing, checking videos and so somehow I make the things “work” but my code is a mess.
Will try to explain what I need because I dont know programming words for what I want:
I will start now with an Android version and I would like to improve and optimize the code, I have lot of upgrades in the game but I have the same code copied and pasted loooot of times and I know that I could use a way to make a base class that can be used for every upgrade. Same with particles, if I want to add a new one I have to add small code lines in a lot of different scripts. Hope you understand what I am trying to say.
Do you know any tut about this thing or could you tell me the name? How do people make a base class and create new stuff just by adding a line of code and some properties?
Some things you want to look into:
-Software engineering; This is how you write & structure code, look for topics on this at it will show better ways to implement things
-Inheritance; This is less used in c#, but still exists and allows the base class thing
-Interfaces; These are handy for adding functionality that is used across multiple things, but they need to handle it themselves
-Data structures; Lists/Dictionaries(Maps)/Arrays/Stacks/Queues, these are just a few to look into, they are helpful for avoiding duplicating code & handling large amounts of data.
The easiest way to get better is look at other code, work out how it works. If you get stuck ask how it works, ask the writer or post it on a programming forum & ask how.
Also feel free to post code on here & ask how to do it better. There are a few regulars here who are happy to help
Also I can recommend http://gameprogrammingpatterns.com/. There is a book available as well but it is free online.
It might be a bit more advanced for a beginner but it is worth a look as it teaches a number of basic programming methodoligies.
Also Inheritance is not always the answer, maybe what you need is centraliced logic. So you make one Class which has methods that are called on other scripts.
Well, basically the previous two answers give you all the information you need. I want to point out one thing already, even though you definately will get to hear that in one of those tutorials/books, though, as I think it is essential.
Whenever you use the same lines of code multiple times, extract them and put them in a new method.
Let’s have a small, fictional example: in your game, there are three men. And those men happen to be identical triplets, so they do have certain similarities.
Instead of writing
public void SetupAllTriplets(){
Character adam = new Character();
adam.name = "Adam";
adam.sex = male;
adam.age = 30;
Character brian = new Character();
brian.name = "Brian";
brian.sex = male;
brian.age = 30;
Character carl = new Character();
carl.name = "Carl";
carl.sex = male;
carl.age = 30;
// do something with them...
}
you should notice, that you have to assign the same sex and age to all of them. That’s work you want to avoid, so you better go with something like
public void SetupAllTriplets(){
Character adam = SetupOneTriplet("Adam");
Character brian = SetupOneTriplet("Brian");
Character carl = SetupOneTriplet("Carl");
// do something with them...
}
private Character SetupOneTriplet(string name){
Character char = new Character();
char.name = name;
char.sex = male;
char.age = 30;
return char;
}
Now, if you want to change something about the triplets you only need to change it one time. Say, you wanted to make your triplets teenagers at first… something like 14 years old. But then you decided it would be cooler to change the setting from school to work maybe and, therefore, your triplets need to be adults… so let’s make them 30. Instead of changing their age one by one, you have one assignment to change and everything is good to go.
Don’t hestitate to abuse this rule even if it’s only a single assignment per object you’re making. Better write a new method for everything.
lord constant: Interfaces are best thought of as limited type of inheritance, not a separate thing.
In C++, which came before Java, which came before C#, you have multiple inheritance, which lets you make an interface. Write a class with only abstract virtual functions – no variables and no implementations – and anyone inheriting from it gets nothing, is forced to write those functions, but then counts as that class for where it could be used. Using a class that way was informally called an interface.
When Java was written, it banned multiple inheritance, but then added most of it back by making formal interface classes. It’s basically a hack to fake multiple inheritance. See the Wikipedia entry for “Interface (Java,)” 3rd paragraph. Later, C# borrowed the idea (C# started life as “Java for windows.”)
So my point here is that inheritance thinking works equally well for classes/C#-interfaces. If you have “void myFunc(A a)” you can save effort by not worrying whether A is an interface or a class. If it’s an interface it is a class since an interface is a fake class anyway. Either way, it counts as an A, you can use all functions and public variables of A, you can check for a sub-class … . If you inherit from a class and two interfaces, it’s easier to just say you inherit from all three classes, again, since an interface is just a fake class.
For this particular example, sure, that would be a solution. My point, however, was about extracting similiar lines of code from different sections, rather than rewriting the same stuff with only slight changes. Of course, you could then start parameterizing and generalizing stuff, but imho that’s another step.
And because I think the latter is often easier to understand and apply, I wanted to explicitly emphasize the aforesaid as it is crucial for bigger projects.
Yes, yes. But you know a lot of this forum (and even more-so UA) is beginners helping beginners. A good course or textbook will demonstrate how useful function are pretty quickly. But otherwise they seem like a real discovery. You might first see built-in functions, where you’re hit with input/output parameters, namespaces … a bunch of tricky stuff all at once.
The idea that you can write your own functions takes longer to discover on your own. And they don’t do anything, like if’s or loops do. I remember old assignments where students were required to use at least 4 functions, but clearly didn’t understand to idea of finding re-usable code. They just wrote 4 arbitrary pointless functions, and thought the idea of them was stupid.
So Karrsun is reading the OP and thinking “you’re still a novice programmer like I just was.” Which is probably correct. Discovering the idea of reusable functions is pretty exciting - Karrsun is showing the spirit of a real programmer here. People get jazzed about explaining something they just learned, and it’s probably one of the things that OP doesn’t know yet.
But, yes, a constructor is better for this. And the best way to learn all of this is to read a real programming textbook. I like the one at taxesforcatses.com, but I’m sure there are others.
Cheers for this I guess, but due to interfaces having their own keyword & being bound to certain rules & being allowed to have multiple where you can only have on direct inheritance it is best to be learnt of as a separate thing to begin with.