Is it easy to make a Turn based Game?

same here

If you can’t explain it simply, you don’t understand it well enough.

At its bare necessity, you need a list that says who will go first and who will go next. In C# you can use a generic list. You can, for example, use a list of strings. Add values inside like “Human”, and then “Enemy”.

You’ll need a separate class that will handle all this. Let’s call is TurnOrderManager.

Then TurnOrderManager needs to keep tabs on whose turn it is now. He is like a referee, ensuring only the current turn owner will act. A simple int that represents the position on the turn order list will do.

Every time a player (or AI) decides to end its turn, it just needs to tell the TurnOrderManager so. Then the TurnOrderManager simply adds 1 to his int, which means the int is now pointing to the next guy in the list, until it reaches the end, where it resets it to the first position.

The rule is then, that only the current turn owner (determined by the int) can do stuff. (if its not this guy’s turn, then disregard any of his requests/attempts to move his characters)

You don’t need any infinite loop here.

3 Likes

Simply yes, just put together a simple turn based game guide/recipe here:
http://forum.unity3d.com/threads/128405-New-to-Unity-advice-on-where-to-get-started-on-a-turn-based-tactical-combat-game?p=904731&viewfull=1#post904731

Should get you started, Unity is fun enjoy!

1 Like

I wont speak to whether it’s easy to make a turn-based game. Anything is easy if you know how to do it. Conversely, if you don’t know how, it’s hard.

An essential part of my turn-based game is the GameController class, which checks the game status every frame and reacts based on that. The state (a public static enum) can be set from any class and for a number of reasons. Here is how it goes when the state has been set to switch turns:

case StateType.TURNOVER:
                print("Turn is over!");
                //check if all players controls a goalpost,
                //remove those that don't
                if (CurrentPlayerHasLost())
                {
                    print(nowPlaying.name + " does not control any Goalposts, and is removed from the game.");
                    GuiBuilder.infoText = nowPlaying.name + " does not control any Goalposts, and is removed from the game.";
                    sceneSc.lordList.Remove(nowPlaying.gameObject);
                    lordList.Remove(nowPlaying);
                }

                //Is game over?
                if (sceneSc.lordList.Count < 2)
                {
                    SetState(StateType.GAMEOVER, this);
                    print("Game is over. " + lordList[0].name + " is Victorious!");
                    GuiBuilder.infoText = "Game is over. " + lordList[0].name + " is Victorious!";
                }
                else
                {
                    SetState(StateType.STARTTURN, this);
                }

                ChangePlayer(); //Do this from server!
                AddEnergy();
                break;

There’s quite a few things going on here, but I think the gist of it is fairly clear. According to the thread, one function of interest might be ChangePlayer().

private void ChangePlayer()
    {
        numShots = 0;
        numBuys = 0;
        timeUsed = 0;

        int index = lordList.IndexOf(nowPlaying);
        if (index >= lordList.Count - 1 || index < 0)
        {
            nowPlaying = lordList[0];
            turn++;
        }
        else
            nowPlaying = lordList[index + 1];
    }
1 Like

Sorry to (slightly) necro this thread, but you guys are aware that C# supports getters and setters, right?

I find them very useful in creating a poor man’s event-driven system to avoid repeatedly polling the “whose turn is it?” state every game cycle. The following is pasted from my Tic-Tac-Toe tutorial:

	public enum gameStates { Idling, PlayerX, PlayerO, VictoryAnimation};
	
	private gameStates _gameState;
	
	public gameStates gameState
	{
		get 
		{
			return _gameState; 
		}
		set 
		{	
			if (value != _gameState)	// This ensures we only call GameStateHasChanged() if it's actually changed.
			{
				_gameState = value;
				GameStateHasChanged();	// call any code relying on gameState changes.
			}
		}
	}

This triggers an event (i.e. calls a function) automatically, but only when the game state has actually changed. From a coding perspective, it’s entirely transparent: you set the “gameState” variable like any other variable. The variable itself uses its modified “set” function to provide the necessary ‘intelligence’ to call the “GameStateHasChanged()” function.

(NOTE: I can remember when we called them “subroutines” and would GOSUB directly to line numbers. It’s the same damned thing, regardless of the fancy, la-de-dah jargon academics love to invent just to confuse people even more. I say, Bah! to your “methods”! Bah!)

1 Like

@stimarco: use proper events! Us .NET devs aren’t heathens with hardcoded function calls :wink: Handy if you also need to update the UI or what have you without extreme coupling.

(you may well know that already, it’s just a little part of me dies every time I see a list of function calls in a setter…)

Actually sending an event from a setter is generally considered bad practice because it provides a poor mechanism for subclasses to control the property (i.e. subclasses have to subscribe to events, cannot send the event, etc). Instead you should provide a protected virtual function which sends the event and can be easily called or overridden by subclasses if required.

i.e

public event EventHandler GameStateChanged;
protected virtual void GameStateHasChanged(EventArgs args){
  EventHandler myHandler = GameStateChanged;
  if (myHandler != null) myHandler(this,args);
}

This may or may not not be what Stimarco is doing; the convention is to use the name On**Changed and for it to take the event arguments as an input.

In the end pick the approach that suits your project. If you are writing a simple tic-tac-toe game then you probably don’t need events at all.

EDIT: Updated the code (namely avoiding possible race condition by making a copy of the handler).

Good to know, although one of my goals is to provide code that can be easily translated to Boo and Unity’s flavour of Javascript, so I’ve deliberately avoided using too much code that might be too C#-specific.

Also, I’ve been learning Objective-C 2 on OS X, which is a very lightweight (and surprisingly elegant) implementation of OOP on top of C. I suspect some of my code may reflect that.

A fair point, but as it’s not a true Event in the C# sense, I don’t think it’s a good idea to confuse matters by making my fake event look like a real one.

This.

My intention is to keep the code easy to read and to show how a simple turn-based game can be built in Unity. As my target audience may be at any level of proficiency, I really don’t want to get bogged down in details like explaining protected virtual functions as the tutorial isn’t meant to be a C# tutorial as well.

Also, while I’m happy to base my projects around common design patterns, I don’t consider those patterns to be gospel. I’ll happily tear MVC a new one if it saves me pages of code and layers of function calls. Does the code work? Yes? Job done.

Totally agree, that was meant to be my point I just took a long time to get there :slight_smile:

Check this at timecode 3:00
This is a turn based RPG game, the turn based game is only the fighting part, the rest is adventure like.
This was sadly NOT founded on Kickstarter and was canceled.
I took part in the development of the whole prototype for the client (the game directors and writers).
It was made with Unity 4 in 6 months.

I suggest you to look at this project which lets you learn how to code a casual turn-based game play:

Snakes and Ladders in Unity3D for Android, iOS and Windows


Hope it helps.

necroing, double posting, and advertising your own stuff. that’s three awards in one. Good going.

@ Whippets,
Can you please kindly tell me what I did wrong with promoting a thing which can actually help starter (and others with the same problem) get what they wants? I read most of the posts above, and @ JRavey somewhere suggested that a S&L could be useful for such situations. So I introduced mine.

If you think I’m abusing this topic, I will remove my post immediately, as I’m just trying to help.

Thanks.

Hi, can you provide more code practice about this subject, Turn based game, event usage, state driven architecture?

This helps me, but I want to see more code and actual usage, benefit about it.

Holy cow, this really got you guys fired up… 3 years ago.

It’s like Minecrafty voxel engine games, turn-based games and generic first-person shooters… those are the games that everybody thinks they can do for some reason.

Yes! It’s all very easy and YOU CAN DO IT in one weekend. And you don’t even have to write one line of code, because of our once-in-a-lifetime super spiffy XBOX live achievement lifetime achievement bananarific free-to-try, 100% guaranteed unconditional money back guarantee YOU TOO can MAKE THE GAME OF YOUR DREAMS without ever making that annoying Space Invaders clone all those old guys told you you should make before moving on to bigger and better games because YES, YOU ARE SPECIAL! Programming is for lesser people who don’t have LEGENDARY TALENT that GOD personally loaned you.

No, turn based is not easy.

“Simple event-driven system”… wow. It’s like, you can put “simple” before anything and it will just make you seem super smart, like you totally know what you’re talking about.

Nothing is as simple as it first appears.

That’s because underlying all that “simplicity” are actual, real-life details. Your mind abstracts away a lot of these details when you think of these things… you’re not actually envisioning the entire system in your mind, you’re just imagining what it would be like to envision the entire system.

What is simple about randomly creating objects and placing them in a field, then determining which objects should go first, keeping track of all of their hit points and other stats and animating all of the attacks and spells and items that can be cast, designing an entire inventory system that will be accessible only by certain characters only during certain actions, building in battle logic into AI units to make them interesting to fight, programming in all of the running-away logic, then there’s the whole “what to do when a character dies” problem… and then there’s the MASSIVE, MIND-BLOWINGLY thorough GUI you HAVE to make, and make it look nice, the HP and MP have to be animated, the gauges have to blink pretty-like, you need a boat load of sound effects, a boat load of art resources, a boat load of written captions and descriptions…

Then you get to add like 80-100 random monsters for variety.

Then you get to test it over and over and over until your eyes bleed.

And there will still be bugs. At least 17 major bugs in the final version. Crap like, this enemy doesn’t die when hit with an attack from this character because of a weird class-specific thing that you put in for testing in the first week and marked with a

// REMOVE LATER!!!

but since you haven’t edited that .cs file in a month you forgot about it.

And so… you say simple. Yeah, simple until you find that there’s a typo in your final build.
Simple until you realize there’s a bug that lets the character duplicate items if they hold left while using them.

Stop underestimating games and start respecting the engineering feats that they are.

5 Likes

This is a pretty good summary of debugging software in general. :stuck_out_tongue:

1 Like

Its not hard at all, it just takes alot of work.

I know the feeling. Was trying to flush some errors out of SQL code at 2am, but the supposed erroneous line was being flagged about 30 rows above where the actual error was and with a very minimal description. Still, these trials are what make us the slightly unstable, slightly hyper people that society has come to love (at least that what I tell myself every day)

1 Like

EDIT: My advice was not good enough…sorry for the post.

The difference between a working prototype and a finished game is pretty significant, not sure that you can call everyone else wrong based on that experience