Hey, it’s me, that viewportframe guy! I’ve improved a lot since my last one and wanted to share a far more robust module I just wrote for ViewportFrames!
Features:
-
Handles multiple ViewportFrames
-
Allows picking specific FPS refresh rates for individual objects
-
Ability to remove, pause, hide, or change FPS refresh rates of objects post-creation
-
Support for NPCs/Characters
API:
Possibly most OOP-styled module I’ve done so far, so there’s a lot of API. I’ll do my best to keep this organized.
Module API:
function ViewportHandler.new(Frame)
Creates and returns a VF_Handler object for the given ViewportFrame
Parameters:
-
Frame
[ViewportFrame]
The ViewportFrame for the VF_Handler
Returns:
-
VF_Handler
[Custom Object]
The handler created for the ViewportFrame
This allows you to use the module on multiple ViewportFrames, simply by creating and using multiple VF_Handlers.
function VF_Handler:RenderObject(Object,FPS,Parent)
Creates and returns an Obj_Handler for the given Object
Parameters:
-
Object
[BasePart]
The Object for the Obj_Handler -
FPS
[Optional Number]
The refresh rate for the object. If left out or set to 0, object will never refresh. (Useful for static objects like map parts) -
Parent
[Optional Instance]
The Parent for the render object clone. Defaults to the Frame of the VF_Handler. (Used mainly internally, you’ll probably never use it)
Returns:
-
Obj_Handler
[Custom Object]
The handler created for the Object
function VF_Handler:RenderHumanoid(Character,FPS,Parent)
Creates and returns an Hum_Handler for the given Character
Parameters:
-
Character
[Model]
The Model for the Hum_Handler -
FPS
[Optional Number]
The FPS for the humanoid refresh rate. Defaults to maximum FPS. -
Parent
[Optional Instance]
The Parent for the render object clone. Defaults to the Frame of the VF_Handler. (Used mainly internally, you’ll probably never use it)
Returns:
-
Hum_Handler
[Custom Object]
The handler created for the Character
function VF_Handler:Destroy()
Destroys the VF_Handler and all Obj_Handlers+Hum_Handlers currently being rendered by it
Parameters:
nil
Returns:
nil
The following APIs are designed to make mass changes easier. They are the equivalent to calling their respective methods on all the current objects being handled.
function VF_Handler:Refresh()
Refreshes all objects in the handler
Parameters:
nil
Returns:
nil
function VF_Handler:Pause()
Pauses all objects in the handler
Parameters:
nil
Returns:
nil
function VF_Handler:Resume()
Resumes all objects in the handler
Parameters:
nil
Returns:
nil
function VF_Handler:Hide()
Hides all objects in the handler
Parameters:
nil
Returns:
nil
function VF_Handler:Show()
Shows all objects in the handler
Parameters:
nil
Returns:
nil
Obj_Handler API:
function Obj_Handler:Destroy()
Destroys the Obj_Handler and the object clone being rendered by it
Parameters:
nil
Returns:
nil
function Obj_Handler:SetFPS(NewFPS)
Updates the Obj_Handler refresh rate
Parameters:
-
NewFPS
[Number]
The Refresh rate for the Obj_Handler. (Clamped between 0 and 9999)
Returns:
nil
function Obj_Handler:Pause()
Stops the Obj_Handler refresher
Parameters:
nil
Returns:
nil
Freezes the object in the Frame. Behaves similar to :SetFPS(0)
function Obj_Handler:Resume()
Resumes the Obj_Handler refresher
Parameters:
nil
Returns:
nil
The counterpart to :Pause()
function Obj_Handler:Refresh()
Runs the Obj_Handler refresher once
Parameters:
nil
Returns:
nil
Updates (once) the object in the Frame. Used for updating a :Pause()
ed object without having to :Resume()
and quickly :Pause()
again
function Obj_Handler:Hide()
Hides the object in the Frame
Parameters:
nil
Returns:
nil
Can be used to stop showing things when not needed without having to delete and recreate Obj_Handlers.
function Obj_Handler:Show()
Shows the object in the Frame
Parameters:
nil
Returns:
nil
Undoes :Hide()
. If not hidden, the function won’t do anything.
Hum_Handler API:
function Hum_Handler:Destroy()
Destroys the Hum_Handler and the char clone being rendered by it
Parameters:
nil
Returns:
nil
Yeah, that was convoluted.
Let’s get into some usage examples so you’ll see it’s actually really simple.
local ViewportHandler = require(script.ViewportHandler)
local Frame = script.Parent.ViewportFrame
local VF_Handler = ViewportHandler.new(Frame)
local baseplate_Handler = VF_Handler:RenderObject(workspace.Baseplate)
local part_Handler = VF_Handler:RenderObject(workspace.Part,20)
local char_Handler = VF_Handler:RenderHumanoid(game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait())
wait(4)
--Set refresh rate of part to 10 (previously 20)
part_Handler:SetFPS(10)
wait(3)
--Set refresh rate of part to 60 (previously 10)
part_Handler:SetFPS(60)
wait(5)
--Remove baseplate from ViewportFrame
baseplate_Handler:Destroy()
wait(2)
--Remove character from ViewportFrame
char_Handler:Destroy()
wait(3)
--End the entire handler (removes Part and disconnects render loop)
VF_Handler:Destroy()
Module File:
This is the first prototype of this that I actually got working, so please reply below with all forms of feedback, suggestions, and bug reports!
Future Updates:
I do hope to add to this. Here’s what I’ve got planned.
- Additional features for Hum_Handlers
- :SetFPS(), :Pause(), that kind of thing.
I also plan to optimize it, but I’m putting that off until the new VM because idk what’s fast anymore.
I’ll also create a few examples (applicable usage, not just basic API testing) and post them in the replies.
Notes:
Objects will refresh visual properties (Color, Material, etc) instantly regardless of FPS rate. For these, it’s faster and better to hook the .Changed event than it is to poll for changes at the set rate.
You can reach into the various custom objects and toy with their properties. You’ll break them, so you’re best just sticking to the given APIs. Regardless, here’s the internals of each one.
Object Internals
VF_Handler:
- HandlerID
- Frame
- ObjectsRenderQueue
Obj_Handler:
- ObjectID
- Object
- ObjectClone
- WaitTime
- LastUpdate
- Enabled
- Showing
- Running
- Handler
Hum_Handler:
- ObjHandlers