Jump to content

  • Log In with Google      Sign In   
  • Create Account

ChaosEngine

Member Since 09 Jun 2000
Offline Last Active Today, 03:14 PM

Posts I've Made

In Topic: The Problem With Capitalism

24 September 2016 - 04:02 PM

This thread has devolved into name calling and cherry picking on both sides.

Obviously, capitalism is in its current form is deeply unfair and the cause of great suffering in the world.

Equally obviously, totalitarian communism has spectacularly failed.

But hey, instead of thinking of lessons each could learn from the other system, let's just make stupid points about how poor people are lazy or mortgages are the new slavery.

In Topic: How to use Unit-Tests

23 September 2016 - 03:44 PM

Well, in order to test my classes, I would have to implement or rather copy them from the original project.
Is that right or is there any possibility to refer to my original files which saves rewriting/copying a class?
As the projects are separated, I doubt this is possible though.

Typically you'd put the thing you want to test into a library project (static lib, dll, whatever).

You would then have an executable project that contains the tests and runs them (depending on the testing framework; some frameworks load dlls into a "test runner")

Finally you have your game executable which just loads your games library and starts it.

So in visual studio, something like
SuperAwesomeGame.sln
+ SuperAwesomeGameStuff (library)
- all your game code goes here
+ SuperAwesomeGame (exe)
- just basically has a "main" that starts your game
+ SuperAwesomeTests (exe)
- contains all the tests and runs them

In Topic: Why C# all of a sudden?

20 September 2016 - 03:01 AM

Outside of games: You're late. C# has seen widespread use in client software development for a long time.


Not just client. It's been popular in server side for at least 10 years now (between "web methods", WCF, and lately ASP.net web APIs).

In Topic: Why can't I print out my std::string vector?

13 September 2016 - 03:24 PM

Should I still not do this even if many other classes needs MapManager? (sorry for the super late reply...)

 
 
You've fallen for the most common misuse of the singleton pattern. The singleton is there is ensure a single instance of an object, not a single point of access.
 
Everyone who misuses a singleton in this fashion says the same thing: "I don't want to pass my X/Manager/God/Controller/Whatever object everywhere". As @Álvaro said, at least then your dependency is obvious.
 
If you find that you're passing a certain object everywhere down through layers of code, this is a code smell and a sign that you should refactor your code.
 
That said, sometimes a global really is the lesser evil. Things like a log object for example.
 
But let's say you look at your options and you decide that it really would be less painful to not pass MapManager everywhere. In that case, just create a global and own that decision. 
 
You have you MapManager class defined as normal (MapManager.h/.cpp). These files should know nothing about the global, otherwise you can't use them without it.
Create a header where you will declare your global MapManager
 
Globals.h

#include "MapManager.h"

// namespaces are great!
namespace InsertGameNameHere
{
   namespace Globals
   {
      // the global map state
      extern MapManager TheMap;
   }
}

Then in your main.cpp instantiate TheMap

#include "Globals.h"

// instantiate the map. this should be in exactly one place in the code
MapManager InsertGameNameHere::Globals::TheMap;

int main()
{
   InsertGameNameHere::Globals::TheMap.DrawMap();
   // do more stuff
}

Anywhere else you need to access the map, just include Globals.h without instantiating the map.

Monster.cpp

#include "Globals.h"

namespace InsertGameNameHere
{
    void SpawnMonster()
    {
       Monster monster;
       monster.SetPosition(Globals::TheMap.GetRandomSpawnPoint());
       // etc.
    }
}
  


In Topic: Why can't I print out my std::string vector?

29 August 2016 - 05:00 PM

ohh, I forgot to add a static to the temp variables, how did I not notice that o.o
Thank you though :P


Seriously, just don't do that.
Let's have a look at your code.

#pragma once

#include <vector>

// don't need iostream for the header
// as a general rule, don't force consumers of a class to include something they don't need
//#include <iostream>

class MapManager
{

// do you really want map to be public? 
// anyone using this class could easily overwrite the contents of it
// public:

	std::vector <std::string> map =
	{
		"###############################################################",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"#                                                             #",
		"###############################################################"
	};

// ok, this needs to be public
public: 
	void DrawMap();

// ugh, singleton... no need for it. 
//	static MapManager &GetInstance();

// don't need these, compiler will auto generate them for us
//private:
//	MapManager();
//	~MapManager();

};

cpp

#include "MapManager.h"

// include iostream where it's needed
#include <iostream>
// don't need ctor or dtor

void MapManager::DrawMap()
{
    // as khatharr said, but without the evil braceatendofstatement style :p
    for(auto& line : map)
    {
       std::cout << line << "\n";
    }
}

// nope nope nope nope nope
//MapManager &MapManager::GetInstance()
//{
//	MapManager temp;
//	return temp;
//}

and using it

MapManager map;
map.DrawMap();

so much cleaner... less code, less bugs... easy to use, hard to abuse.


PARTNERS