Jump to content
  • Advertisement
WolfWin

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

  • Advertisement
  • Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By SickTwistGames
      Ok, firstly, Hi.
       
      This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.
       
      My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
      So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
      It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.
       
      Thanks
    • By ethancodes
      I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
    • By Scouting Ninja
      Once again Unity is frustrating me to the point of insanity.
      What I am looking for is a way to find a ray intersect with the edges of the mesh, using Unity's already made collision system. I want to point out that I know how to do a line intersect, what I want to know is if Unity supports this already.

      The image above shows how I sweep a ray,intersecting the mesh. The top green image shows what I want and the red shows what Unity is giving me.
      I want to know if there is some way, to find the edges in Unity without creating a custom line intersection tool.
      Most engines I know don't use rays for this but instead use a plane like this:

      I checked the Unity "Plane intersection" but it is just a ray cast. It will still need me to find the vertices on the collision mesh to cast the ray from; if I am doing that then making my own line intersection tool is better.
       
      I looked online and can find anything on this. Also I don't want to cut the mesh, so I don't need a way to know what side is what.
      Does Unity even have collisions that support edge only detection?
    • By Shtabbbe
      I've had a game idea for a while, and I wanted to finally try to create it.
      Its a 2D open-world tile-based MMO. The concept is it is one world and multiplayer only, so everyone shares one world no matter region, platform, etc.
      I am having problems finding out what to use to start development, I tried Unity but saw some of the negatives and refrained and now im stuck, could anyone recommend some intermediate friendly 2D engines that can support what I am looking for? Preferably in languages that are or are somewhat like Java, C#, Python, JavaScript, Lua.
      Thanks for your help, im very new at this if you cant tell
    • By JuliaAxt
      Please help me with this code, this error is currently stopping my project 
      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      [RequireComponent(typeof(Rigidbody2D))]
      public class TapController : MonoBehaviour {
          public float tapForence = 10;
          public float tiltSmooth = 5;
          public Vector3 startPos;
          Rigidbody2D Rigidbody;
          Quaternion downRotation;
          Quaternion forwardRotation;
          private void Start() {
              Rigidbody = GetComponent<Rigidbody2D>();
              downRotation = Quaternion.Euler(0, 0, -90);
              forwardRotation = Quaternion.Euler(0, 0, 35);
        
          }
          private void Update() {
              if (Input.GetMouseButtonDown(0))
              {
                  transform.rotation = forwardRotation;
                  Rigidbody.AddForce(Vector2.up * tapForce, ForceMode2D.Force);   (The name `tapForce`does not exist in current context)
              }
              transform.rotation = Quaternion.Lerp(transform.rotation, downRotation, tiltSmooth * Time.deltaTime);
          }
      }
          void OnTriggerEnter2D(Collider2D col){
          if (col.gameObject.tag == "scoreZone")
          {
              // register a score event
              // play a sound
          }
          if (col.gameObject.tag == "deadZone")
          {
              Rigidbody.simulated = false;   (`Rigidbody` does not contain a definition for `simulated´) 
              //register a dead event
              //play a sound
          }
           }

      }
       
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!