Improving the ThingComp code

Started by superpirson, September 10, 2014, 11:28:46 AM

Previous topic - Next topic

superpirson

Hi!

I'm requesting that thing comp "def"s become actual defs, containing PostLoad() functions.

long story on this thread:  http://ludeon.com/forums/index.php?topic=5939.0 short story to follow:

Basically, I wanted to make a library for animating things using frames. The best way I've found in the code to do this is through use of a ThingComp, however the "def" field in ThingComp is just a class, not an actual def. Consequently, I am unable to write functions like PostLoad() for the thing comp, which is troubling seeing as I need to make a hash table for the textures so that I can Identify and load them quickly by a string. (I can explain why I settled on this implementation if you want, just ask!)

This could be done from the PostLoad() of ThingDef, just simply foreach-ing through the CompProperties and running their PostLoad().
specs: mid2009 MBP OSX 10.9.3 NVIDIA GeForce 9400M 256 MB

Tynan

Well, it can't be a Def, but it could get a PostLoad.

You could achieve a similar effect without that, though. Just lazy initialize it:


private Dictionary<string, Texture2D> texturesTableInt = null;

public Dictionary<string, Texture2D> TexturesTable
{
  get
  {
    if( texturesTableInt == null )
      InitializeTextureTable();

    return texturesTableInt;
  }
}
Tynan Sylvester - @TynanSylvester - Tynan's Blog

superpirson

AHH! I was unaware that was called that! (I love the little names programmers give these tricks)

I considered some solutions like that, but I feared that it would tangle up the load order. On top of that, I don't think it would scale: It could cause a lot of problems having to load something like a 200 frame animation at runtime. I really want this lib to be in line with the RimWorld design that I have learned by studying your code.

Thanks for the response Ty!
specs: mid2009 MBP OSX 10.9.3 NVIDIA GeForce 9400M 256 MB

Tynan

The 'tricks' are better known as 'design patterns'. Another name for you :) They're quite important; people make lists of them and discuss them and write books about them. If you're working on programming you should learn some more design patterns!
Tynan Sylvester - @TynanSylvester - Tynan's Blog