Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

ObjectValue references cannot be cloned - How to work around it?

Asked by
doomiiii 112
7 years ago

I am a big fan of Unity's Prefab and GameObject model. Basically how it works is that you can assign GameObjects or Prefabs (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:

  1. Create a Turret model with a Shoot script

  2. Add an ObjectValue called Bullet to Turret

  3. Prepare the Bullet model, put it in ServerStorage, and assign to the Turret's Bullet ObjectValue

  4. In the Shoot script, use whatever Bullet model is referenced in that ObjectValue 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 ObjectValues manually. That is especially annoying when there are many different ObjectValues 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:

  1. Use a ModuleScript instead of ObjectValue that returns the hard-coded object reference of choice. Any script references to the object could just be changed from X.Value to require(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.

  2. Use a StringValue where we'd reference an object by it's full name in the DataModel 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 as ServerStorage.NPCs.WeakHobo). Changing object references would not require code changes, so that's a plus.

  3. There is somehow a way to successfully clone ObjectValues???

My question is two-fold:

  1. Why can ObjectValues Value property not be cloned (assuming a shallow clone, just keep the reference to the original; should not be too hard)? and:

  2. 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.

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
7 years ago

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!

0
Sorry, only recently came back to this stuff. So yeah, I just came across this very problem: You cannot copy+paste ObjectValues, which is a bit of a b*tch.... I wonder what a good work-around is to easily copy a configuration containing ObjectValues then? doomiiii 112 — 6y
0
About the original issue: Yeah, it must have been fixed. I found, a lot of things have improved since then, so that's good news! doomiiii 112 — 6y
Ad

Answer this question