I have read a book (it’s on the web if you want to read about it) about important programming patterns, especially in game design, and I think I know why Roblox is not able to create things like moving grass, mainly because it’s too much data being loaded individually.
The ability to load tons of individual things onto memory, each with individually loaded textures and such, can lag the game a ton, even if it is the same TextureId
every time it is used. This is the place where the flyweight comes into play in most Triple-A games. The idea is that the data from each object is separated into two states:
- its intrinsic state, the data between the many instances that are similar,
– in this thread, it’s the TextureId
- and its extrinsic state, which is the data that makes them different from each other.
– this could be the timing of the wind blowing through them
The renderer then uses the same data from it’s intrinsic state and loads it into every decal, and loads its extrinsic state individually for each decal of grass.
Apparently, the idea behind this method is to reduce the amount of data that needs travel to the GPU so that it can use this data faster.
I don’t think there is a technique in Lua that utilizes the same exact data to load Instances in roblox, only the ability to spread and copy the data into them, which is not what this design pattern was made to do.
It’s like, say you had a table with a thousand different Vector3
positions in workspace
, and let’s call this table partPositions
. You want to generate a part for each position in the table, and each part is exactly the same except for the position.
Usually, you would do this:
for i = 1, 1000 do
local part = Instance.new("Part")
part.Position = partPositions[i]
end
What the flyweight pattern demands for, however, is this:
local part = Instance.new("Part")
part.Position = partPositions[1], partPositions[2], partPositions[3],...
or to make it simpler:
part.Position = partPositions -- the table itself
Essentially, the part is rendered in every spot from the table, all at the exact same time. Surprisingly, this probably wouldn’t slow down your PC too much because it’s just one part in a thousand positions, instead of a thousand parts each manifesting one position.
I would understand why this isn’t included in Roblox, because it’s really complex and it would be hard to create an api for, not to mention that it would make rendering parts very inflexible and confusing, and how you would you go about creating a physics solver from this?
However, this could probably be done with decals, since you would be creating just one texture in many different positions (and orientations), and you would only have to worry about rendering it since it does not interact with physics or anything else.
I could be very, very wrong, and Roblox engineers are well aware of the power this design pattern has, and are actively using it in the engine, or at least have a reason not to use it. I don’t really know anything behind the scenes, but this is my opinion.