2D Roguelike: Q&A

This is the official thread for discussion, issues and Q&A for the 2D Roguelike project.

Please use this thread if you have any questions, issues or feedback on this project.

2D Roguelike is a learning project on our Learn Site:

Learn how to make a 2D Roguelike game with this project. Over the course of the project will create procedural tile based levels, implement turn based movement, add a hunger system, audio and mobile touch controls. This video series was filmed in Unity 5, but is compatible with Unity 4.6 as well.

Post your questions and the team and we will try to help you out!

IMPORTANT:
If you want to post a script you are having trouble with, please post the whole script that is throwing an error using proper code formatting detailed in the thread below, a screenshot of the public variables in the inspector and copy and paste the error message from the console if there is one.

Known Issues:

  • Please turn “annotations” on, as we try to annotate these videos if any issues are found. Many people turn annotations off, and miss these comments.
  • Due to changes in the Unity API related to loading scenes since Unity 5.3, we’ve modified the scripts in the Completed folder to reflect the new API, and added an upgrade guide PDF to the asset package detailing the modified steps for the tutorial.
  • Download the upgrade PDF directly from here.
4 Likes

I’m up to part 5. I’m not following the tutorial exactly as I’m building a different type of game, but I’m using it to give me an understanding of how to set things up.

I’ve set things up as you have, except when I run my code, I’m getting the error “Object reference not set to an instance of an object” on the line where I try to instantiate the board manager (which in my code is called SetupGame) in the GameManager script.

I think this is because the boardScript variable is not set, and at this point in the tutorial the board manager script has not been attached to any game object to be set as a variable in the Game Manager prefab.

Do I have to attach the boardScript script to a GO and then set it in the Game Manager prefab?

I’m new to Unity and C# so maybe I’m missing something.

Both the GameManager and BoardManager scripts should be attached to the GameManager prefab and have those same script names, since they’re referred to by those names within the scripts. Since you’re doing your own thing it’s a bit hard to tell what else may be wrong but make sure that you have BoardManager attached to an object and that the GM can somehow get a reference to it. If necessary make the boardScript variable in GM public and drag a reference.

Hi Matt,

First of all, thanks for this tutorial. I found it really informative and it’s really cool to see an intermediate tutorial coming out from UT. And I’d love to see more.

But there appears to be a seam between the tiles, ( http://prntscr.com/63isrw) and I think this ties in with Unit’s pixel perfect camera problem that I was talking about some time ago, if you remember. In fact, I’m sure that if you do move the camera with the character, there will be some pixel issues.

Has any solution for these issues popped out yet?

Cheers

hey Shadeless,

I could be wrong but in my experience that seam / line is only visible in the editor. It doesn’t appear in builds of the game (or at least builds I’ve made).

I don’t have a new update re pixel issues BUT we do have a whole new team of developers working exclusively on 2D recently (quite a few more than were on it before) so I think we can expect some good progress in that area in coming months! Glad you’re enjoying the tutorial!

Hi Matt,

Thanks a lot for this it’s exactly what I have been looking for. Just a quick question, how hard would it be to modify this to include the generation of multiple rooms per level? With doors and such and are there any examples of this?

You’re welcome! There are many examples of procedural dungeon generation online, here is one:

http://www.roguebasin.com/index.php?title=Dungeon-Building_Algorithm

The implementation in BoardManager is mostly random and therefore pretty simple. It’s definitely possible to extend it in a more dungeon-y direction but it’ll take some doing. If you make progress follow back up and let us know what you did!

3 Likes

Hi Matthew, the video for number 8 is set to private :slight_smile:

hey there,

Is it still showing private for you? I had to swap out that one last night because of an editing error, it should be fixed and public now, both in the playlist on youtube and on the learn site here:

Thanks for the heads up!
M.

Can you do one for this
http://www.vlambeer.com/2013/04/02/random-level-generation-in-wasteland-kings/

there,

I’m having some troubles with the second tutorial.

I have created the three first animations for Player

and moved the controller to AnimatorController

then if I look inside the player controller, all the animations appear there

but not like in the tutorial with an “initial state”, but the Idle one is like selected by default.

but when I click on play button only a blue background is displayed.

Do you know what could be happening?

System: Mac OS X 10.9.5

The “initial state” is a Unity 5 feature if I’m not mistaken? The blue background, Is it a bar under the name of the animation? When you attach transitions that bar should move to the animation that is playing. Everything you’ve done seems correct so far!

I’ll take a look at it. I LOVE Nuclear Throne and actually was just playing last night (still terrible though, only got past the snow area w/ the robots once lol). I do want to do some more stuff on level generation and the approach in Scavengers is still fairly simple, I may do something in one of the live training sessions. Thanks for the link!

1 Like

@RamonBoza yes, the tutorial is filmed in Unity 5 so it will look a bit different than what you have if you’re in 4.6. That said what you have is correct and the orange, default state for PlayerIdle should be playing when you play the scene. Make sure that the Player game object is in the scene and in view of the camera with a sprite renderer, animator attached.

I feel a bit like this tutorial is intermediate just because it can be. I’ve made more advanced games than this, but I’ve not touched many of the concepts covered in this tutorial. I’ve been making games for years, but I’m self thought and I haven’t got a programming background. I feel the tutorial glosses over why it feels it’s necessary to make things so complex, at least in my opinion, for such a simple game.

Here’s some of the things I’m curious about:

  1. Why have the loader class which instantiates the two managers instead of just adding the two managers to the scene manually?

  2. What are the benefits to singletons? I usually just have my managers as gameobjects and then I use GameObject.Find in the Awake() function of my scripts to find them. That way I don’t have to access them with manager.instance.whatever each time. I can just do manager.whatever. This benefit in itself I find is big enough to never want to use singletons. Otherwise I just use a static class if I can.

  3. Why use DontDestroyOnLoad() on the managers and then reload the scene each time you complete a level instead of just have a function that resets the board? Everything happens in the same scene anyway. Doing it this way just seems so unnecessary?

  4. I’m still unsure as to what virtual, abstract, override and protected does. What are the benefits of all this over just using Unity’s component system? Why not have MovingObject as a normal component that you add to both the player and the enemies?

  5. Is that optimized move code really necessary? I find it very hard to read. Is it something that will make a difference in a game like this or will it only be noticeable if we were to have thousands of enemies moving at once or something? I’m thinking about the square magnitude and multiplying instead of dividing parts. Basically part 6 of the tutorial. I find it very easy to relate to the magnitude of a vector as a distance on my screen, but square magnitude becomes just an abstract concept so I stay away from it. Also why do you have to compare it to float.Epsilon instead of just 0? I’ve never seen float.Epsilon before. I didn’t even know it was a thing. :stuck_out_tongue:

Basically, are there performance benefits to doing it this way? With all this inheritance stuff I mean. Will it scale better than components? It’s the Enemy, Player and MovingObject scripts I’m having the most issues with. The rest of the tutorial/project seem very straight forwards, but these scripts just feel much more complex than they could have been.

From my point of view it just ends up being very hard to read with little benefit. Like using Thesaurus on a piece of text. For instance I find it very hard to see just what functions actually come into play for the player and the enemy. Both the player and the enemy seem to override the AttemptMove() function, but inside it they call the AttemptMove() function of the base class. Trying to follow that code line for line I find to be very hard. :stuck_out_tongue:

I’m obviously not demanding an explanation, I feel it’s a nice tutorial and there’s surely a lot for me to take from it. I’m just saying I would really appreciate some more information about the more complex parts. :slight_smile: I’ve looked in the completed folder to check out the code comments for the completed scripts, but I didn’t get much from them.

I think I will attempt to write the code the way I would have done it myself to see what changes I will have to make to make it work like in the tutorial. Perhaps that will shed some light on this for me. Maybe I’ll realize why you would choose inheritance for certain things. :slight_smile:

Lastly, who did the music for the game? It’s really good.

2 Likes

I do similar things in a very similar project - they make more sense when you have slightly different scenes that share managers.

I’ve got a game manager that loads if necessary, otherwise it inherits the existing instance. It doesn’t just hold the current state and score, but also tells the loader where to return after special scenes (mini-games, for example). Different tile sets and map generators are used in each scene, while input, player and game managers stick around between scenes. The game manager just grabs the map settings object from the named object and builds the appropriate map.

I wouldn’t mind seeing some additional videos where these sort of concepts are explored.

1 Like

hey Twiik,

Last things first, I did the music and sound myself (I’ve produced electronic music for many years). I’m glad to hear you liked it :slight_smile:

I’ll try to go step by step through your questions, but holistically I understand your complaint. Everyone has to find a workflow that balances readability, efficiency and makes sense for them and their projects. In this project I actually wrote the game first and then collaborated with Mike Geig and James Bouckley (gameplay programmer on the content team) to write the final scripts.

One of the goals of an intermediate as opposed to a beginner tutorial is to help our users to stretch and learn new concepts. In this case some of the concepts that I think are a little bit of a stretch for some but are definitely quite important and valuable are some you mentioned: singletons and inheritance, and generics which you didn’t mention. So your point that it’s “intermediate for the sake of being intermediate” is partially true. This felt like a good opportunity to show those concepts and so we decided to include them. We could have cut those parts or worked around them to make it simple but we felt it was a good teachable moment that would help people.

Now to your specific questions:

1. Why have the loader class which instantiates the two managers instead of just adding the two managers to the scene manually?

This prevents cases where you might inadvertently cause two managers to be added to a scene, especially with both set to DontDestroyOnLoad. If the GameMeaner is set to not destroy, and then you reload the scene with it in there, you’ll end up with two.

2. What are the benefits to singletons? I usually just have my managers as gameobjects and then I use GameObject.Find in the Awake() function of my scripts to find them. That way I don’t have to access them with manager.instance.whatever each time. I can just do manager.whatever. This benefit in itself I find is big enough to never want to use singletons. Otherwise I just use a static class if I can.

The risk of GameObject.find is that you’ll have a case where the object isn’t there, throw a null reference exception and crash. It’s also expensive performance wise, especially if you’re instantiating lots of enemies for example in a complex scene, each which has to find the manager at runtime during it’s Awake().

3. Why use DontDestroyOnLoad() on the managers and then reload the scene each time you complete a level instead of just have a function that resets the board? Everything happens in the same scene anyway. Doing it this way just seems so unnecessary?

In this case reloading the scene is just easier and cleaner in my mind. I don’t see exactly what about it is un-necessary or particularly undesirable? I actually wrote one version with resetting the board and re-initializing everything and it was just way more work and less reliable.

4. I’m still unsure as to what virtual, abstract, override and protected does. What are the benefits of all this over just using Unity’s component system? Why not have MovingObject as a normal component that you add to both the player and the enemies?

In this case part of the motivation here was to get people to start thinking about inheritance. I actually did try writing a motor component and doing it the way that you said and in the end ending up duplicating my movement code into the enemy and player scripts, which James then suggested I do using inheritance. I can’t remember what the issue I had was originally unfortunately but this would be a good case for you to try both and see what you like.

That said I think you should be able to imagine situations where using inheritance in a larger project and having lets say a base class Weapon which MachineGun inherits from would be sensible and useful so this was partly an opportunity to start people thinking about the potential there.

5. Is that optimized move code really necessary? I find it very hard to read. Is it something that will make a difference in a game like this or will it only be noticeable if we were to have thousands of enemies moving at once or something? I’m thinking about the square magnitude and multiplying instead of dividing parts. Basically part 6 of the tutorial. I find it very easy to relate to the magnitude of a vector as a distance on my screen, but square magnitude becomes just an abstract concept so I stay away from it. Also why do you have to compare it to float.Epsilon instead of just 0? I’ve never seen float.Epsilon before. I didn’t even know it was a thing. :stuck_out_tongue:

Yes. Generally speaking we try to show best practices that people can learn from in terms of architecture and performance. Because this game is small and simple we certainly could have cut corners and gotten away with things performance wise, but then we would have taught people those behaviors were OK and they might have taken them into larger projects.

Basically, are there performance benefits to doing it this way? With all this inheritance stuff I mean. Will it scale better than components? It’s the Enemy, Player and MovingObject scripts I’m having the most issues with. The rest of the tutorial/project seem very straight forwards, but these scripts just feel much more complex than they could have been.

Short answer, there are not (to my knowledge) performance benefits in terms of speed, but this will allow you to keep your project organized and avoid spaghetti code. For example lets say I had the move code copied in both player and enemy, then realized I needed to change it, I’d have to change two scripts (and potentially forget to change one) etc. Inheritance is pretty standard practice in object oriented programming, check out Mike’s lesson inheritance (as well as some other intermediate scripting concepts) here:

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/inheritance

Hope that helps! And as always, find an approach that works for your skill level and your projects. There are many, many ways to skin a cat.

3 Likes

Thanks for the reply. It cleared things up a lot for me. There have been intermediate Unity tutorials in the past, but the intermediate part has been the scale of the project, not the complexity of the code. That may be the reason I was a bit surprised here because this is a very small project.

I have Handmade Hero (https://handmadehero.org) running in the background while I code and his approach is similar to this tutorial I guess. He makes things much much more complex than they have to be to teach people how it would be if the game (engine) they’re making actually was more complex.

I like extending Unity tutorials with extra features so I guess I will try it both ways and see if I end up switching back to my old ways or if I actually learn something from this and stick with inheritance. :stuck_out_tongue:

As for pt. 3 I guess I had this preconceived notion that reloading the level was the most expensive and cumbersome way to remake the map for the next level. Guess I will try this both ways as well.

By generics I’m guessing you mean the part? I was going to mention that, but I forgot. I’ve always used things like SendMessage when I don’t know what component I’m dealing with.

All in all, I guess I’m a prime candidate for such a tutorial. :stuck_out_tongue:

PS: Thanks for the link to the scripting tutorials. I haven’t seen those before.

1 Like

Heh, the first 2 minutes of that video explained what the protected keyword does. I’m not sure if you mentioned it and I missed it, but small things like that would have helped me understand why the code is the way it is. Or perhaps just put a disclaimer in the beginning that the viewer should have at least skimmed through the intermediate scripting tutorials before taking on this tutorial. You may have said something along those lines and I may have missed that as well.

There was one more thing I forgot to mention that was bugging me. One thing I haven’t seen mentioned anywhere before. This line:

using Random = UnityEngine.Random;

If you create a new C# script in Unity you get these by default:

using UnityEngine;
using System.Collections;

And you can use Random as much as you want. Why do you need that line? I can see in my project that Random stops working if I remove it in this particular script file, but I’ve never had it before and I’ve used Random in all my projects.

And one more question that’s been bothering me for a long time. :stuck_out_tongue:

Why do you have to type this to use lists?

using System.Collections.Generic;

Instead of just:

using System;

Both Collections and Generic are inside System. Shouldn’t “using System;” include everything in it? I would think that was the whole point. That you could specify just the parts you needed or instead just include the whole shebang.

Edit: I answered the question about Random myself. I see that I’ve always written [System.Serializable] instead of [Serializable] so I haven’t had to write “using System;” and thus I’ve never gotten the Random name collision. The things you learn. :stuck_out_tongue:

1 Like

“Intermediate knowledge level” to me implies “already knows C#”, so it’s perfectly understandable to skip basic concepts :wink:

Besides, there are plenty of C# tutorials out there, and I think many people hate retreading old ground. If they’re going to teach Unity, they shouldn’t spend 5 episodes of a tutorial series on generics, public vs. private variables and details of .NET collections. Microsoft have some good tutorials and references on MSDN.

The “using” directive only gives you a shortcut to the specified part of a namespace hierarchy. Again something a tutorial on MSDN will explain :slight_smile:

Tangentially related, possibly off-topic:
What they SHOULD do is include a series of intermediate tutorials (probably text, as pure code isn’t fit for video) that summarises the differences in Unity’s .NET vs. MS/Mono .NET. Specifically the bits that are missing. I know certain functionality either isn’t there or requires manual addition of assemblies (third-party or copying Mono stuff around), like zlib compression. So a guide for .NET programmers to get up to speed.

3 Likes