Sign in to follow this  
Bombshell93

[help] how should I save objects?

Recommended Posts

Bombshell93    245
I'll get straight to the point, I need a way to save objects into a level which can then easily be interoperated when loaded.
I have a system in another engine where on running it checks the version of the calling assembly with the version of a type library. if they match the type library is loaded and the typenames are parsed into a dictionary used while loading and saving the game. if they do not match the library is loaded, typenames parsed and the calling assembly is checked for new or missing types so it can update the type library.
Whenever I look up reflection I always find something about how it brings very notable overhead and is a practice to avoid, but I don't know how else I can make sure all loadable and savable entities have an identifier used in saving and loading to distinguish them from each other.

my current engine is Entity Component based, I intend on loading a level to load the tile data, level description, background, etc. then for each object in the level create an entity and load its components, so any entity can have components removed and added at will during level editing or gameplay.

[b]TL;DR?[/b]
When loading I need a way to tell what a component is, so I can run that components loading constructor wherein it builds itself with the data following its identifier, then it'll go onto the next component, check what it is, run its loading constructor, etc. etc. etc.

any and all help is greatly appreciated, links, tutorials, references, anything would help,
thanks in advanced,
Bombshell

Share this post


Link to post
Share on other sites
laztrezort    1058
[quote name='bombshell93' timestamp='1341700025' post='4956755']
When loading I need a way to tell what a component is, so I can run that components loading constructor wherein it builds itself with the data following its identifier, then it'll go onto the next component, check what it is, run its loading constructor, etc. etc. etc.
[/quote]

Can't you store the typename along with the object data, then use Activator.CreateInstance to create the objects?

[quote]
Whenever I look up reflection I always find something about how it brings very notable overhead and is a practice to avoid
[/quote]

If you are loading objects from disk inside your inner loop (60 times a second), I would think the IO would be the bottleneck, not reflection. Also, why would anyone suggest avoiding something as powerful and handy as reflection? Like all the other supposed performance killers (linq, multi-dimensional arrays, regex, polymorphism, etc.) its probably best to use what works well first before handicapping yourself with premature optimization. Especially if you are developing for PC, which I've found can handle an obscene amount of "overhead" [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

Share this post


Link to post
Share on other sites
SiddharthBhat    358
\agree 100% about premature optimisation.

Being a C++ user, I'd eat my right foot for a feature as handy as reflection. why you you not want to use a feature that is perfectly acceptable and downright simplifies your job?

as long as you're loading your Game Objects once per level, I don't really see the problem. If you need to keep creating game objects of the same type, then create an intermediate object that holds this information. (I'm not really sure what you're referring to, so I may be wrong).

Share this post


Link to post
Share on other sites
Bombshell93    245
I'd read using reflection to look through the assembly requires a bunch of extra meta-data be produces and that it can make method execution 4 times slower because of this.
is this wrong? or insignificant?
Also I intend to load the game objects on the go via chunks, so the levels can be fairly large.

Share this post


Link to post
Share on other sites
laztrezort    1058
[quote name='bombshell93' timestamp='1341764210' post='4956952']
I'd read using reflection to look through the assembly requires a bunch of extra meta-data be produces and that it can make method execution 4 times slower because of this.
is this wrong? or insignificant?
Also I intend to load the game objects on the go via chunks, so the levels can be fairly large.
[/quote]

Sure, reflection code is slower than non-reflection code (because it has to do a few extra steps), but I am still skeptical it would matter at all in this case, since the IO time will vastly overwhelm everything else. Reflection isn't [i]that[/i] slow, after all.

P.S.:

Let's take a completely contrived example, with completely contrived values:

You have your reflection-based object creation system, and it is taking 20 ticks of time. You spend a bunch of time optimizing it, replacing the simple, readable & maintainable code with hackish, brittle code, and now you have a 5 tick system. Great, but the creation system is being called in an outer loading method that is taking 1000 ticks on its own (due to disk IO). Congratulations, you've just increased performance by less than 0.1%. This is why you always see people harping on and on about "profile first, then optimize", since things like "4 times slower!" can be very misleading in the big picture. Edited by laztrezort

Share this post


Link to post
Share on other sites
Bombshell93    245
thanks for the information,
I may have been misunderstanding what I read, but the concern was if I depend on reflection of the assembly, all method execution would be slower,
I assume your saying the method execution is a non-issue, which I'd understand the draw calls and general game logic would probably take far more time.
if its really a non-issue / insignificant as you suggest then I guess I can continue as I am.
thanks again :)

Share this post


Link to post
Share on other sites
laztrezort    1058
[quote]
I may have been misunderstanding what I read, but the concern was if I depend on reflection of the assembly, all method execution would be slower,
[/quote]

I don't know anything about this, I'm no .Net expert but AFAIK the framework is pretty good about lazily loading metadata and caches as its needed. The compact framework or others may have tighter constraints on this stuff, though.

[quote]
if its really a non-issue / insignificant as you suggest then I guess I can continue as I am.
[/quote]

That is my suggestion - worse case, I am wrong, and the reflection code turns out to be an issue (after testing/profiling). Then there are ways to go back and optimize (e.g. smarter caching, caching reflection method calls to delagates, putting reflection lookups into dictionaries, etc.), but at least you won't have wasted time doing all this for no reason.

good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this