CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2011
    Posts
    2

    noob error: spawning entities from file.

    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&)|
    anyone know how to fix this?
    thanks,

  2. #2
    Join Date
    Dec 2010
    Posts
    20

    Re: noob error: spawning entities from file.

    firstly:
    obj_array = new CEntity[map_size];
    you store object in array instead of point, so pls change into this:
    obj_array = new CEntity*[map_size];

    Secondly:
    as for the compiling error, because you can't assign a point to an object!
    Last edited by deyili; June 16th, 2011 at 03:59 AM. Reason: finished

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: noob error: spawning entities from file.

    Quote Originally Posted by UVmatician View Post
    here is the MyMap.cpp:
    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.
    Quote Originally Posted by UVmatician View Post
    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.
    Quote Originally Posted by UVmatician View Post
    //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?
    Quote Originally Posted by UVmatician View Post
    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

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: noob error: spawning entities from file.

    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.

  5. #5
    Join Date
    Jun 2011
    Posts
    2

    Re: noob error: spawning entities from file.

    @Deyili
    thank you very much

    @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)

  6. #6
    Join Date
    Jun 2011
    Posts
    2

    Re: noob error: spawning entities from file.

    I changed my MyMap.cpp code to the following

    Code:
    #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?

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: noob error: spawning entities from file.

    I think it might have to do with using a pointer that points to nothing. Any ideas?
    Run it in debug mode and use the debugger to check what's going on.

  8. #8
    Join Date
    Aug 2008
    Posts
    902

    Re: noob error: spawning entities from file.

    Quote Originally Posted by UVmatician View Post
    @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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured