rojo icon indicating copy to clipboard operation
rojo copied to clipboard

How to write Ref or Refs?

Open cskeogh opened this issue 3 years ago • 2 comments

In a .model.json file, how do I write a Ref? I checked the documentation and the documentation doesn't provide guidance.

Consider a file Rat.model.json

{
  "Name": "Rat",
  "ClassName": "Model",
  "Properties": {
    "PrimaryPart": "Torso"
  },
  "Children": [
    {
      "Name": "Torso",
      "ClassName": "Part",
      "Properties": {
        "Size": {
          "Type": "Vector3",
          "Value": [1, 1, 3]
        }
      }
    },
    {
      "Name": "Humanoid",
      "ClassName": "Humanoid"
    }
  ]
}

This fails with Wrong type of value for property Model.PrimaryPart. Expected Ref, got a string How do I Ref "Torso"?

I am using Rojo v7.0.0-alpha.4

cskeogh avatar Jun 06 '21 12:06 cskeogh

Hello, thanks for the issue! I've transferred it from rojo-rbx/rojo.space to rojo-rbx/rojo because I think this pertains to the core Rojo project.

There isn't a way to express refs in .model.json files right now. It should be feasible to add; the underlying code supports assigning temporary IDs to things and then referencing them later. We just need to surface that to the user.

In the meantime, the Roblox official model formats should work for you. They're a bit harder to read and write by hand, but support refs and other kinds of properties.

LPGhatguy avatar Jun 06 '21 17:06 LPGhatguy

Idea for implementation: A refs (JsonModel and Project) / $refs (ProjectNode) property that is typed (luau-style, I don't currently know Rust) { [string]: JsonModel/ProjectNode }. Then, the refs can be used in the tree by adding the key to the Children array, and used as a property via an explicit Ref cast. (This seems easier to implement than my previous suggestion, which I removed from this comment due to incompatibility.)

Edit: Okay, Rust is hard for beginners. I'll just start with implementing a $declareRef (declareRef for model users) property which acts globally, unless someone wants to pick up the previous suggestion.

PoolloverNathan avatar Dec 18 '22 05:12 PoolloverNathan