I'm still inexperienced with C++. I am trying to load game entities and their xyz coordinates from a text file "1.ent" I thought it would be a good idea for a map format.
here is the MyMap.cpp:
Code:
#include "CEntity.h"
#include "MyMap.h"
CEntity* MyMap::GetEntity(std::string type )
{
if( type == "player" )
return new CPlayer(/*you can stick parameters for the constructor in here*/);
if( type == "entity" )
return new CEntity();
/*if( type == "cactus" )
return new Cactus();
*/
// And so on, ad infinitum.
}
bool MyMap::LoadMap(char* File)
{
FILE* FileHandle = fopen(File, "r");
if(FileHandle == NULL) {
return false;
}
fscanf(FileHandle, "%i\n", map_size);
//read size from the first line of the file.
obj_array = new CEntity[map_size];
char read_type[255];
std::string type;
float xval;
float yval;
float zval;
for (int i=0;i<=(map_size-1);i++)
{
//type = first string per Entity
//xval = second string
//yval = third string
//zval = fourth string
fscanf(FileHandle, "%s\n", read_type);
fscanf(FileHandle, "%f\n", xval);
fscanf(FileHandle, "%f\n", yval);
fscanf(FileHandle, "%f\n", zval);
type = read_type;
//have a special string called "end" at the end of the map file.
if (type != "end")
{
if ((obj_array[i] = GetEntity(type)) != NULL)
{
//by this point the constructor on the new object is already called.
//but we won't use the constructor we will set the values directly.
obj_array[i].X = xval;
obj_array[i].Y = yval;
//obj_array[i].z = zval;
//switch this to a list specific to this map when you expand the engine.
CEntity::EntityList.push_back(obj_array[i]);
}
}
else
break;
}
}
void MyMap::OnCleanUp()
{
//not sure if this part is necessary
for (int i=0;i<=(map_size-1);i++)
{
if (obj_array[i] != Null)
delete obj_array[i];
}
//considering I have this
delete [] obj_array;
}
here is the text file:
Code:
3
player
400.0
40.0
0.0
player
500.0
40.0
0.0
player
600.0
0.0
0.0
end:0:0:0
When I compile I get a big error on this line:
Code:
[if ((obj_array[i] = GetEntity(type)) != NULL)]
here it is:
Code:
error: C:\Projects\2D_Engine\MyMap.cpp|54|error: no match for 'operator=' in '*(((MyMap*)this)->MyMap::obj_array + ((unsigned int)(((unsigned int)i) * 116u))) = MyMap::GetEntity(std::basic_string<char>(((const std::basic_string<char>&)((const std::basic_string<char>*)(& type)))))'|
note: C:\Projects\2D_Engine\CEntity.h|30|note: candidate is: CEntity& CEntity::operator=(const CEntity&)|
Why are you using C constructs in a C++ program?
There is no need to use dynamic arrays, use std::vector instead.
Prefer to use the C++ file streams over FILE* and related functions.
Originally Posted by UVmatician
Code:
if ((obj_array[i] = GetEntity(type)) != NULL)
You are assigning a pointer to an object. Since you want to treat entities polymorphically, you need to make obj_array a vector of pointers.
Originally Posted by UVmatician
//switch this to a list specific to this map when you expand the engine.
Why not do so now? In fact, you already have this, so why do you need a static container of all entities?
Originally Posted by UVmatician
here is the text file:
Note that the last line won't be read properly.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Not really related to your question, but since you are clearly making a game, you might want to google component based entities and data-oriented design. A rigid hierarchy of entity classes and arrays of structures can be a performance and programming nightmare when it comes to video games.
@D Drmmr
thanks, apparently I need to do some more research on how std::vectors work.
@Chris F
thanks, yeah I had heard about component based Entities. Is it simply for the sake of minimizing inheritance? All the stuff I've found on DOD is for people who already know how to make an OOP engine, and I don't really know enough yet (obviously)
#include "CEntity.h"
#include "MyMap.h"
CEntity* MyMap::GetEntity(std::string type )
{
if( type == "player" )
return new CPlayer();
if( type == "entity" )
return new CEntity();
return NULL;
/*if( type == "cactus" )
return new Cactus();
*/
// And so on, ad infinitum.
}
bool MyMap::LoadMap(char* File)
{
FILE* FileHandle = fopen(File, "r");
if(FileHandle == NULL) {
return false;
}
fscanf(FileHandle, "%i\n", map_size);
//read size from file, probably from the first line.
//obj_array = new CEntity*[map_size];
char read_type[255];
std::string type;
float * xval = NULL;
float * yval = NULL;
float * zval = NULL;
for (int i=0;i<=(*map_size-1);i++)
{
//type = first string per Entity
//xval = second string
//yval = third string
//zval = fourth string
fscanf(FileHandle, "%s\n", read_type);
fscanf(FileHandle, "%f\n", xval);
fscanf(FileHandle, "%f\n", yval);
fscanf(FileHandle, "%f\n", zval);
type = read_type;
//have a special string called "end" at the end of the map file.
if (type != "end")
{
MapList.push_back(GetEntity(type));
MapList.back()->X=*xval;
MapList.back()->Y=*yval;
//obj_array[i].z = zval;
}
else
break;
}
return true;
}
void MyMap::OnCleanUp()
{
for(int i = 0;i < MapList.size();i++) {
if(!MapList[i]) continue;
MapList[i]->OnCleanup();
}
MapList.clear();
}
Now it compiles fine with no errors but when I run it it returns with status 3. I think it might have to do with using a pointer that points to nothing. Any ideas?
@Chris F
thanks, yeah I had heard about component based Entities. Is it simply for the sake of minimizing inheritance? All the stuff I've found on DOD is for people who already know how to make an OOP engine, and I don't really know enough yet (obviously)
It's meant to avoid the pitfalls of inheritance as it is often implemented in game object hierarchies. Using the traditional approach, you inevitably end up with either a lot of redundant code or you end up with an oversized base class. It's really an anti-pattern, and component based systems alleviate some of that, although they can still use inheritance to accomplish it.
OOP isn't necessarily bad for games, but the traditional OOP approach is not good for performance. Here is a good article from Sony on the subject.
Bookmarks