diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 360916feb8..5419272642 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -10887,6 +10887,8 @@ static void FinaliseObjectsArray() } } } + + ObjectSpec::BindToClasses(); } /** diff --git a/src/newgrf_commons.cpp b/src/newgrf_commons.cpp index 296100a09b..5f68163ff4 100644 --- a/src/newgrf_commons.cpp +++ b/src/newgrf_commons.cpp @@ -317,7 +317,6 @@ void ObjectOverrideManager::SetEntitySpec(ObjectSpec *spec) /* Now that we know we can use the given id, copy the spec to its final destination. */ memcpy(&_object_specs[type], spec, sizeof(*spec)); - ObjectClass::Assign(&_object_specs[type]); } /** diff --git a/src/newgrf_object.cpp b/src/newgrf_object.cpp index 6bdcea5dd7..888a4b3615 100644 --- a/src/newgrf_object.cpp +++ b/src/newgrf_object.cpp @@ -93,6 +93,18 @@ uint ObjectSpec::Index() const return this - _object_specs; } +/** + * Tie all ObjectSpecs to their class. + */ +/* static */ void ObjectSpec::BindToClasses() +{ + for (auto &spec : _object_specs) { + if (spec.IsEnabled() && spec.cls_id != INVALID_OBJECT_CLASS) { + ObjectClass::Assign(&spec); + } + } +} + /** This function initialize the spec arrays of objects. */ void ResetObjects() { @@ -107,20 +119,17 @@ void ResetObjects() for (uint16 i = 0; i < lengthof(_original_objects); i++) { _object_specs[i].grf_prop.local_id = i; } + + /* Set class for originals. */ + _object_specs[OBJECT_LIGHTHOUSE].cls_id = ObjectClass::Allocate('LTHS'); + _object_specs[OBJECT_TRANSMITTER].cls_id = ObjectClass::Allocate('TRNS'); } template /* static */ void NewGRFClass::InsertDefaults() { - ObjectClassID cls = ObjectClass::Allocate('LTHS'); - ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_LTHS; - _object_specs[OBJECT_LIGHTHOUSE].cls_id = cls; - ObjectClass::Assign(&_object_specs[OBJECT_LIGHTHOUSE]); - - cls = ObjectClass::Allocate('TRNS'); - ObjectClass::Get(cls)->name = STR_OBJECT_CLASS_TRNS; - _object_specs[OBJECT_TRANSMITTER].cls_id = cls; - ObjectClass::Assign(&_object_specs[OBJECT_TRANSMITTER]); + ObjectClass::Get(ObjectClass::Allocate('LTHS'))->name = STR_OBJECT_CLASS_LTHS; + ObjectClass::Get(ObjectClass::Allocate('TRNS'))->name = STR_OBJECT_CLASS_TRNS; } template diff --git a/src/newgrf_object.h b/src/newgrf_object.h index e6579119a4..74cc089a85 100644 --- a/src/newgrf_object.h +++ b/src/newgrf_object.h @@ -136,6 +136,8 @@ struct ObjectSpec { static const ObjectSpec *Get(ObjectType index); static const ObjectSpec *GetByTile(TileIndex tile); + + static void BindToClasses(); }; /** Object scope resolver. */