0
\$\begingroup\$

How do online games handle creation of new entities? I currently have an EntityFactory class that requires an ID (specifying the entity type). A bunch of subroutines are executed to determine exactly what the entity is and how it should be composed, and then the final function spits out the result.

I see a few potential issues with this system though:

  • First of all, it's difficult to add new entity types. I don't like having to go into my game's code in order to change entity behaviour.
  • Second, it's impossible to modify the way entities are generated at runtime. It would be nice to have this functionality, especially when testing game mechanics.
  • And finally, since all of the information about creating objects is inside my game code, I'm worried about performance becoming an issue when I have a large number of entities that can be created. I use inheritance for entities in the same "class" to reduce the amount of code I need to write, but it's still a lot of code.

I've heard of using JSON to combat some of these issues, but I can't picture how that would work.

If I load all of the JSON data into the game upon starting the server, then I solve the first issue, but not the second and third.

If I only "require" the JSON as I need it and have none of the data in memory, then I solve the second issue as well, but isn't this even worse for performance? I can't imagine this would be better than keeping everything in memory, because I would need to deserialize in realtime.

I could do both, keeping all "current" data about entity initialization in memory, and only deserialize when a change is made to a file. I can't help but feel like there are better options though. For example, if a monster is only spawned once every 100 hours, it doesn't really make sense to me to not have it in a separate file somewhere and only read when necessary.

I guess I can keep data that is needed frequently deserialized at all times and data that isn't required often can be deserialized on request?

If anyone has experience with these topics I'd really appreciate some insight into the matter.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Are you expecting this entity data to be a significant memory burden? I'd expect a back-of-napkin estimate would put it in the order of megabytes to maybe dozens of megabytes if you have a lot of it, versus gigabytes of memory available on modern devices. Is this really a limiting factor for your game? \$\endgroup\$ Commented Oct 11, 2022 at 5:53
  • \$\begingroup\$ Pretty much, I use an ID that denotes the type of entity and then the subtype and so on, and then I have a chain of functions that handle composing the object starting from the top. So like a for poison frog I would first generate a basic animal, then modify its properties to become a frog, then modify it again because it is poisonous. I'm honestly not sure about memory usage, I will have a ton of data eventually and I wasn't sure if this was a bad practice to have it all available at once, but maybe I'm overthinking it. \$\endgroup\$ Commented Oct 11, 2022 at 18:27

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.