• Advertisement

C# Concepts Useful For JRPGs?

Recommended Posts

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?
 

Share this post


Link to post
Share on other sites
Advertisement
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.
Edited by Nypyren

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by Nypyren

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Timothy Sharp
      just to test out my animations, let's say i had a trip animation. How would I code a script to where my character would fall it mud and their clothes would become all muddy? I saw this in a game on Unreal 3 but is this possible with Unity?
    • By cesarpachon
      hello, I am trying to implement a realistic simulation of a roulette wheel. it is not clear for me what is the proper way to simulate the initial status of the ball, when it spins against the edge of the wheel until it loss energy and start falling towards the centre.
      I modelled the conic table as a height map, as I assume that would provide the smoother surface. but I see anyway there is rough squared corners everywhere, so really I don't have a smooth inner wall to slide against.
      I wonder if I should ignore the wall and simulate the sliding by code. i.e: apply force (or impulse?) each frame to keep the ball at a fixed radius and somehow force it to follow a desired angular speed..  later, when I want to execute the falling behaviour, just stop applying that forces and let the simulator and the gravity do their work.. makes sense?
      any advice is highly appreciated!
       
    • By getoutofmycar
      I'm having some difficulty understanding how data would flow or get inserted into a multi-threaded opengl renderer where there is a thread pool and a render thread and an update thread (possibly main). My understanding is that the threadpool will continually execute jobs, assemble these and when done send them off to be rendered where I can further sort these and achieve some cheap form of statelessness. I don't want anything overly complicated or too fine grained,  fibers,  job stealing etc. My end goal is to simply have my renderer isolated in its own thread and only concerned with drawing and swapping buffers. 
      My questions are:
      1. At what point in this pipeline are resources created?
      Say I have a
      class CCommandList { void SetVertexBuffer(...); void SetIndexBuffer(...); void SetVertexShader(...); void SetPixelShader(...); } borrowed from an existing post here. I would need to generate a VAO at some point and call glGenBuffers etc especially if I start with an empty scene. If my context lives on another thread, how do I call these commands if the command list is only supposed to be a collection of state and what command to use. I don't think that the render thread should do this and somehow add a task to the queue or am I wrong?
      Or could I do some variation where I do the loading in a thread with shared context and from there generate a command that has the handle to the resources needed.
       
      2. How do I know all my jobs are done.
      I'm working with C++, is this as simple as knowing how many objects there are in the scene, for every task that gets added increment a counter and when it matches aforementioned count I signal the renderer that the command list is ready? I was thinking a condition_variable or something would suffice to alert the renderthread that work is ready.
       
      3. Does all work come from a singular queue that the thread pool constantly cycles over?
      With the notion of jobs, we are basically sending the same work repeatedly right? Do all jobs need to be added to a single persistent queue to be submitted over and over again?
       
      4. Are resources destroyed with commands?
      Likewise with initializing and assuming #3 is correct, removing an item from the scene would mean removing it from the job queue, no? Would I need to send a onetime command to the renderer to cleanup?
    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By devbyskc
      Hi Everyone,
      Like most here, I'm a newbie but have been dabbling with game development for a few years. I am currently working full-time overseas and learning the craft in my spare time. It's been a long but highly rewarding adventure. Much of my time has been spent working through tutorials. In all of them, as well as my own attempts at development, I used the audio files supplied by the tutorial author, or obtained from one of the numerous sites online. I am working solo, and will be for a while, so I don't want to get too wrapped up with any one skill set. Regarding audio, the files I've found and used are good for what I was doing at the time. However I would now like to try my hand at customizing the audio more. My game engine of choice is Unity and it has an audio mixer built in that I have experimented with following their tutorials. I have obtained a great book called Game Audio Development with Unity 5.x that I am working through. Half way through the book it introduces using FMOD to supplement the Unity Audio Mixer. Later in the book, the author introduces Reaper (a very popular DAW) as an external program to compose and mix music to be integrated with Unity. I did some research on DAWs and quickly became overwhelmed. Much of what I found was geared toward professional sound engineers and sound designers. I am in no way trying or even thinking about getting to that level. All I want to be able to do is take a music file, and tweak it some to get the sound I want for my game. I've played with Audacity as well, but it didn't seem to fit the bill. So that is why I am looking at a better quality DAW. Since being solo, I am also under a budget contraint. So of all the DAW software out there, I am considering Reaper or Presonus Studio One due to their pricing. My question is, is investing the time to learn about using a DAW to tweak a sound file worth it? Are there any solo developers currently using a DAW as part of their overall workflow? If so, which one? I've also come across Fabric which is a Unity plug-in that enhances the built-in audio mixer. Would that be a better alternative?
      I know this is long, and maybe I haven't communicated well in trying to be brief. But any advice from the gurus/vets would be greatly appreciated. I've leaned so much and had a lot of fun in the process. BTW, I am also a senior citizen (I cut my programming teeth back using punch cards and Structured Basic when it first came out). If anyone needs more clarification of what I am trying to accomplish please let me know.  Thanks in advance for any assistance/advice.
  • Advertisement