I am a big fan of Unity's Prefab
and GameObject
model. Basically how it works is that you can assign GameObject
s or Prefab
s (which are GameObjects that are not in the world, but fully prepared to be spawned whenever needed, such as bullets) to properties on scripts, and scripts can then use those properties just like (in theory) you could do that with ObjectValue
instances.
For example - Here is how I would want to create a turret in Roblox:
Create a
Turret
model with aShoot
scriptAdd an
ObjectValue
calledBullet
toTurret
Prepare the
Bullet
model, put it inServerStorage
, and assign to theTurret
'sBullet
ObjectValue
In the
Shoot
script, use whateverBullet
model is referenced in thatObjectValue
for shooting
This has a range of advantages. Most importantly: One script can support any amount of bullets without requiring any changes to the code.
However, in reality, when I clone my turret, I have to re-assign all ObjectValue
s manually. That is especially annoying when there are many different ObjectValue
s on the same object.
What's worse is that ObjectValue
cannot be used at all with any object that is spawned/created by script (because it would immediately be invalidated). For example, right now, I am working on a simple pick-up system where tens of pick-ups are randomly spawned on the map, but different pick-up "templates" are supposed to contain different items/objects, with different scripts/effects.
I want to avoid hard-coding of the referenced objects at any price. I can imagine several work-arounds, including:
Use a
ModuleScript
instead ofObjectValue
that returns the hard-coded object reference of choice. Any script references to the object could just be changed fromX.Value
torequire(X)
. Changing the referenced object would require code changes (which is bad), but due to the simplicity of the script, it would not be too error-prone.Use a
StringValue
where we'd reference an object by it's full name in theDataModel
hierarchy. In this case, any script references would require an extra function that decodes and looks up objects by full path in the hierarchy (since the path of an object should contain full names in dot notation, such asServerStorage.NPCs.WeakHobo
). Changing object references would not require code changes, so that's a plus.There is somehow a way to successfully clone
ObjectValue
s???
My question is two-fold:
Why can
ObjectValue
sValue
property not be cloned (assuming a shallow clone, just keep the reference to the original; should not be too hard)? and:Is there a better option here?
PS: (Not sure if anyone cares, but...) Unity is rather smart about this. When copying, all Prefab
and non-local GameObject
references are kept as-is. However, if you reference an object that is a descendant of the object being copied, it will update the reference to match the newly instantiated copy of the descendant instead.
I realise that this is quite an old question, but I've just been experimenting in Studio to verify your claims. Perhaps ROBLOX have updated the behaviour of ObjectValue
since you made them, because both cloning and duplicating in Studio preserve the references.
Additionally, cloning or duplicating also preserves local references within a model.
The only case which doesn't work is copying and pasting something which contains non-local references. Duplicating and cloning still preserve non-local references, though!