Advertisement

C# Concepts Useful For JRPGs?

Started by November 25, 2017 04:06 PM
8 comments, last by jazzyspazglobal 6 years, 9 months ago

Hey all, been working through Daniel Schuller's "How to Make an RPG", except in  Unity in C#.
Its been fun so far, finished basic stats and leveling, and eveything works as expected. However:

                    knowledge of C# is lacking,

                    What aspects of C# would be useful for this kind of project?

 

For example I have been using Dictionaries to store stat keyvaluepairs.

Any specific aspects of C# I should dig into more?
 

generics for templates.
extension classes that came with LinQ. I really love GroupJoin

Advertisement

Try reading this http://gameprogrammingpatterns.com/contents.html
I found it very clearly written and very helpful when thinking about programming quandaries.

Video Game Programmer.
5 years in industry.

6 hours ago, WolfWin said:

For example I have been using Dictionaries to store stat keyvaluepairs.

RPG stats seem like you would always know what all of the stat keys are ahead of time, and every character would have a value for every stat.  I would use a class containing one field per stat.

Differences between the two approaches:

  • The elements you add to a Dictionary<K,V> have to share a common base type (K for keys, V for values), and if you are using derived classes you'll have to know which type to cast to each time you access one of them.  With fields you can make each field any type you want.  Perhaps your stats are all ints or floats right now, but later you might want one stat to be something different.
  • You can much more easily access a field than a Dictionary entry.  You have IDE autocompletion for the field name.  With a Dictionary the key might or might not exist in the Dictionary.  With Dictionary keys you have at least one GetHashCode and one or more Equals calls that occur on every key lookup and member fields just use some basic pointer math which will always be faster.
  • You can foreach over a Dictionary much more easily than member fields, though I can't think of a case where I would want to loop over stats.  If you need to loop over member fields you can use a function that uses 'yield' to return a set that you can loop over.
  • Unless your Dictionary uses an enum type for its key, you will be able to find code using one specific stat more easily with fields.
  • If your Dictionary uses strings for its key, change that to an enum immediately even if you don't want any of the other advantages of fields listed above.

Thanks Nyperen! 

My stats class holds some other information as well such as modifiers, but Ill consider your approach. Definitely going to use enums instead of strings though!  

Going to read up more on linq. 

Is their anything you guys recomend for code structure for turnbased games in unity? Looking for examples of implementation.

For general code structure, since you're still learning, don't try to get too complex.  Do the simplest thing that you think might work, and only make it more complex if you need to.  For a turn based game, the simplest things you NEED are a way to keep track of whose turn it is (one variable?), having a way to perform some action(s) during a turn (something which gets input from a player or an NPC), and how/when to go to the next turn.

The main suggestion I have for any new programmer is:  Try to keep every function very short and simple.  If you need to make something complicated, split it up into simpler parts.  Turn each simple part into a function, then you can make your more complicated function by just calling the simple ones.

There is a tendency to do the same kinds of things in multiple places in a game; you should try to make simple functions so that you can reuse it instead of copy-and-pasting the same code in multiple places.

Always make sure to test your code frequently!  Even experts make silly mistakes and it's best to catch them early.  You can also automate this testing process.  If you want to jump right in, look up:  "Unit testing" (Unity has built-in support for these as well!) and "Test Driven Development".  This stuff is somewhat separate from C#, kind of like how knowing how to use a debugger is separate.  Just like knowing how to use a debugger, knowing how to use unit testing can make profound improvements to your development experience.

Advertisement

If you have never done C# before, I would not learn it by using Unity, or any game engine for that matter, that is my personal preference.

I would create games/tools/application the vanilla way, from the ground up. 

On 11/25/2017 at 8:28 PM, danielricci said:

If you have never done C# before, I would not learn it by using Unity, or any game engine for that matter, that is my personal preference.

I would create games/tools/application the vanilla way, from the ground up. 

I disagree, using Unity you can get instant visual feedback when working through programming problems. Starting with just C# and no other framework is a recipe to get bored and not see any interesting results for a while and using something like WPF for windows applications has the added headache of markup languages, not to mention the support and tutorials are nowhere near as good.

On 11/25/2017 at 1:30 PM, Net-Ninja said:

Try reading this http://gameprogrammingpatterns.com/contents.html
I found it very clearly written and very helpful when thinking about programming quandaries.

Definitely, check this out. It has a very nice section on the State pattern. You should also get involved with Arrays and Lists. They could be useful for keeping track of Enemy data. 

This topic is closed to new replies.

Advertisement