1
\$\begingroup\$

So for practice and as a learning exercise i foolishly decided to make a text based rpg on my phone using the c4droid ide. ive done well and have learned a lot.

So far you can create a new character or load the previous one. and you can walk around in the rooms ive made. currently the rooms load from a file when you enter it, and then it loads again every time you do anything except leave,which loads the next room instead. ill be tweaking my gameloop so it doesnt reload the room everytime soon.

My next step is the addition of mobs. and here ive run into a problem i cant find the answer to with google. My plan was/is to make a class for mobs and instantiate it when there was a mob present and load data from a file. But then i was trying to figure out how to code which room to load them in. First answer was to have the room file dictate which mobs, if any to load. great. except that i want the mobs to stay dead, not reapawn everytime the room reloads. so now im wondering if i should be loading the rooms from file all at the same time before the game loop. Potentially too memory intensive down the line when the game has more rooms and mobs? and what to load them to? i havent used arrays in c++ yet, though i have used map which seems similar. Im not even sure what a vector is. i expect ill be needing to read up on those very soon.

so the basic question is this: leave loading the rooms the eay it is, load them all at the beginning of the game, or something else?

all help is appreciated

\$\endgroup\$
  • \$\begingroup\$ std::vector is basically an array encapsulated in an object-oriented wrapper. \$\endgroup\$ – Philipp Jul 6 '18 at 14:06
  • \$\begingroup\$ Careful with Vectors in C++, as you could be talking about the std::vector as mentioned above, or be talking about directional vectors such as Vector3D in the DirectX framework and similar. \$\endgroup\$ – blurry Jul 6 '18 at 15:21
1
\$\begingroup\$

To add to other answers, we don't really know how many rooms you have (or plan to), so loading every room in the beginning might work, but not in all cases.

As a text-based game however, you can still keep track of all spawned enemies, something like this:

  • Move to a new room, load it
  • Enemy is in the room (based on the room file), check if that enemy has already spawned (exists) in the "enemy array"
  • if not, spawn a new enemy, and add its instance to the enemy array.
  • if yes, do not load that enemy again, as its most likely wondering around the map.

Now every time you visit a new room, keep in memory the previous room (that the enemy is in) so that it can navigate through the room to find you. If you move too many rooms away, maybe you can despawn the enemy (unload and remove from enemy array) or you can keep all previous rooms in memory, until the enemy navigates past them. It all depends on how you want enemies to behave.

Since you are using C++, the best choice is a std::vector, as mentioned in the comments as well.

The details really are up to you to decide, as its implementation will slightly change the enemy's behaviour (if you want it to respawn, or chase you indefinitely etc etc). Also, I'm not sure what's the size of the "map" that contains all the rooms, but if you have divined your game in stages, or something that can make you "progress" to another set of rooms (like ascending, descending floor), you can load all rooms of that stage in the memory, and spawn all enemies the moment you appear there. Assuming that your gave mimics the appearance of rogue-like games, it shouldn't be intensive at all memory-wise.

\$\endgroup\$
0
\$\begingroup\$

You didn't post any numbers regarding the RAM usage of your game, but reading between the lines I get the impression that your game is still far too simple to require so much memory that you can't load all the rooms at game startup and then keep their state in memory while the game is running.

But if you want to keep your architecture where you load every room from the filesystem when you enter it and unload it when you leave it, then you could add a second "change file" for each room. This file represents the state of any changing properties of the room (like the status of any mobs in it). After you loaded the room file, check if there is a change file for that room and when you have one, load it and apply the changes to the room data you just loaded. When these properties change, write a change file for that room with those changes.

And by the way, you now have a working savegame system.

I'm looking forward to playing your game.

\$\endgroup\$
  • \$\begingroup\$ thanks! ill share it somewhere for folks to try it out when it gets to a playable point. \$\endgroup\$ – Richard Carpenter Jul 7 '18 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.