[size="4"]So here is the deal: My tile-based editor reads the state of every tile and outputs a number to a .txt file (actually at the moment its a .AWESUM file because I was curious whether suffixes really mattered). The save function works as it should. HOWEVER: the load function does not. What the load function does is it takes the .AWESUM or .txt file and reads the numbers and, according to a 2d array of the tiles, distributes the surfaces to the tiles accordingly. It does work when I call upon it as it starts, as part of the init function, but during the game, when I want the user to be able to, say, load a custom map to be able to edit it further, nothing happens! I used a sort of log-debug technique and determined that the function is indeed running in its entirety, but I notice no change in the tiles. Even if I save the map thinking that its just not drawing according to change, and restart the program and load it as the Main map, it comes out looking like it did when i saved it. Im stuck. Its rather big so Ive provided the program in a .zip. I'm currently using C++, Windows 7, and SDL 1.2, and CodeBlocks as my IDE.
[attachment=1682:TileEditor.zip]
PS if you feel the need to comment on some of the other faults, go for it. Im by no means a master.
Load Function Makes No Change
In the save function you pass the world by value so the world you update is just a copy of the original world object. Pass by reference or pass a pointer instead,
I think I get what youre saying. So I shouldnt do world World, but I should do world* World?
Yes, passing a pointer will work but you will have to change the code to operate on the pointer.
If you pass by reference you will only have to change the parameter: int load(world& World, TTF_Font *font, SDL_Color color);
But both will work equally well so use the way you prefer.
If you pass by reference you will only have to change the parameter: int load(world& World, TTF_Font *font, SDL_Color color);
But both will work equally well so use the way you prefer.
Okay I tried doing what I understood was your suggestion, however when I converted world World to world* World, and made the necessary conversions from . to -> for it to compile, the game pops a segsev and points to the first line of world->init(); Sorry, but I have no idea what im doing here, and I don't have a clue what to do with pointers :\
EDIT: I found the function you are quoting and its in input_manager right? the input manager class does nothing but handle text input (like /save). the load function can be found in world.cpp, which was why I was confused. The load function does activities with the map within itself. it has a 2d array of tiles that it cycles through.
EDIT: I found the function you are quoting and its in input_manager right? the input manager class does nothing but handle text input (like /save). the load function can be found in world.cpp, which was why I was confused. The load function does activities with the map within itself. it has a 2d array of tiles that it cycles through.
You don't need to pointers everywhere. only in inside inputManager::load. To convert an World to pointer you do &World. If you don't know about pointers it's probably easier to just change
If you don't know pointers I recommend you learn them because they are good to know. Even if you don't have to use them it is good for understanding how things work.
EDIT: You use world::load once at the start but after that you use inputManager::load called from inputManager::handle_input.
int inputManager::load(world World, TTF_Font *font, SDL_Color color)
to int inputManager::load(world& World, TTF_Font *font, SDL_Color color)
and you can still use it like a normal world object like you did before.If you don't know pointers I recommend you learn them because they are good to know. Even if you don't have to use them it is good for understanding how things work.
EDIT: You use world::load once at the start but after that you use inputManager::load called from inputManager::handle_input.
The reference thing didn't change anything :\ and yes, its true that its called from InputManager, however, InputManager calls it from the world object which is why it is included.
Its like this: User Presses Enter-> Input Manager -> Get File Name -> Call the World.Load function
Its like this: User Presses Enter-> Input Manager -> Get File Name -> Call the World.Load function
You have the same problem with inputManager::handle_input so you will have to do the same here.
Holy cow! It works! Thanks man. If it's not too much trouble, can you help me understand why it worked? So I can learn and stuff?
So, you said that it was only modifying a copy of the world class, or the World object? How was it made?
So, you said that it was only modifying a copy of the world class, or the World object? How was it made?
I will try to explain. Here is an example:
When a is passed to foo1 i will be a copy of a. If i is manipulated it will not affect the value of a because i is just a copy of a.
When b is passed to foo2, i is a reference to b, this means that i just contains the memory location of b and when do something with i it will affect b.
In the example we used int but it works the same for all types. When you don't want to copy you have to use references (or pointers). If the object is large it is often more efficient to pass by reference because copying a large object is slower than just copying the address of the object (which is what the reference contains). For this reason you probably want to pass the world in inputManager::save by reference as well to avoid copying.
void foo1(int i)
{
i = 5;
}
void foo2(int& i)
{
i = 5;
}
int main()
{
int a = 0;
int b = 0;
foo1( a );
foo2( b );
// a == 0
// b == 5
}
When a is passed to foo1 i will be a copy of a. If i is manipulated it will not affect the value of a because i is just a copy of a.
When b is passed to foo2, i is a reference to b, this means that i just contains the memory location of b and when do something with i it will affect b.
In the example we used int but it works the same for all types. When you don't want to copy you have to use references (or pointers). If the object is large it is often more efficient to pass by reference because copying a large object is slower than just copying the address of the object (which is what the reference contains). For this reason you probably want to pass the world in inputManager::save by reference as well to avoid copying.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement