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

Is there a way to "unrender" (or hide) objects to decrease lag?

Asked by 4 years ago
Edited 4 years ago

I want to make a script to hide/remove a high quantity of objects to decrease the lag, but I don't know if there is a way to do that. I don't know if putting these objects clientside in Lighting or ServerStorage will do the trick I need. So can someone please tell me if hiding objects in Lighting or ServerStorage is what I should do or if there is a better way.

3 answers

Log in to vote
1
Answered by 4 years ago

For really big games, it's worth looking at StreamingEnabled, which basically loads and unloads parts of the game for players depending on their location, the downside of it is that you have to use :WaitForChild() a lot more since some assets may not be loaded in.

0
I have considered using StreamingEnabled for long ranges, but I want to really just find a way to toggle some decorative props that take up a lot of space. Thanks anyway tho, I will probably end up using it. Unity_456 47 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Yes! ServerStorage is the best way to do it as it is the "storage" of the game, while lighting may still operate scripts, The ServerStorage doesn't do that. Benefits of the server storage: The Objects are not drawn, so they reduce lag, scrips do not operate, so they do not take up power. I hope it helps!

0
It is better to use built in functions rather than cloning and destroying or reparenting a large amount of objects because that will just shift the resources used from the gpu to the cpu, and I doubt he wants a tradeoff. SteamG00B 1633 — 4y
Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years ago

There is a built in way to do this by using Camera.Focus. It basically prioritizes what to show in full quality based on where the focus currently is. By default, Camera.Focus is always on the Camera.Subject, however if you change the default camera scripts, you can control where the focus is via scripting. So to control the Camera.Focus, you need to set the camera type to scriptable, and you need to override the default Camera scripts. And also, to update it every frame, you can use BindToRenderStep rather than having a while true loop.

So for instance, if you have an fps game, and you want to unrender most of the objects directly behind the player, you can set the focus to be downrange of the player, so that the game renders everything in front of them to what their graphics quality is set at, but leaves out all the lighting and graphics changes behind them.

You can do the same with a 3rd person game, it just becomes a bit more difficult in that you need to use a bit more math when setting the focus in relation to the camera instead of humanoid root part of the 1st person method.

Here's an example of focus following the point in 3D space that the mouse is pointing at:

local UserInputService = game:GetService("UserInputService")
UserInputService.InputChanged:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        local ray1 = Camera:ScreenPointToRay(input.Position.X, input.Position.Y, 0)--creates a unit ray towards the mouse (ray with a magnitude of 1)
        local ray = Ray.new(ray1.Origin,ray1.Direction * 1000)--takes the original ray and multiplies the magnitude by 1000
        local part, hitPos = workspace:FindPartOnRayWithIgnoreList(ray,character)--if there is an intersection before 1000 studs, it gives the position
        local Focus = CFrame.new(hitPos, --placed at the position in 3D space the mouse is pointing at
            ray.Origin) --facing the camera
             * CFrame.Angles(0,math.rad(180),0) --flips it around 180 degrees so it is facing towards the mouse
        Camera.Focus = Focus
        print("Mouse moved, Focus is now: "..Focus.Position.X,Focus.Position.Y,Focus.Position.Z)
    end
end)

Just click view source on the code block to see the comments better, I've explained what is going on line by line.

I also use the UserInputService rather than render stepped here because if you want it to follow the mouse, then you would want to save some performance by not having it shoot rays every frame if you don't have to.

0
I will look into that, if I ever get around to doing something this complex (I'm not good at working with maths in code, yeah.) Unity_456 47 — 4y
0
Its actually not that complex, it just takes an understanding of vectors and their magnitudes. I'll update my answer with a an example on how to get the focus to follow whatever the mouse is pointing at. SteamG00B 1633 — 4y

Answer this question