Controllers
Controllers are the main building blocks of Crdle. They are exclusively client sided code that can be used to organise your code into logical groups. Controllers are used to create a modular system that is easy to maintain and expand.
For this tutorial we will be creating a simple Camera Controller which will manage the in-game camera.
Creating a Controller
Controllers can be made on the client side using:
local CameraController = Cradle:CreateController({
Name = "CameraController",
})
return CameraController
The Name
field is required and must be unique. This is used to identify the controller and is used in the Cradle:GetController()
method.
Controller are not accessible from the server. If you need to communicate with the server, you can pair your controller with a Service.
See the Services Guide for more information.
Adding Methods
To add methods to your controller, simply add them to the controller object.
function CameraController:ZoomIn()
-- TODO: Code to zoom in
end
function CameraController:ZoomOut()
-- TODO: Code to zoom out
end
function CameraController:ResetZoom()
-- TODO: Code to reset zoom
end
Adding Properties
Properties can be added to your controller by adding them to the object. You can do this in two ways:
CameraController.DefaultZoomLevel = 60
CameraController.ZoomLevel = 0
or
self.DefaultZoomLevel = 60 -- Inside a method
self.ZoomLevel = 0 -- Inside a method
or using the Cradle:CreateController()
method:
local CameraController = Cradle:CreateController({
Name = "CameraController",
DefaultZoomLevel = 60, -- FOV
ZoomLevel = 0,
})
Usng Methods and Properties
We can now use the properties and methods we have created. For example, we can add methods to zoom the camera in and out:
function CameraController:SetZoom(input: number)
self.ZoomLevel = input
end
function CameraController:ZoomIn()
self.ZoomLevel += 1
end
function CameraController:ZoomOut()
self.ZoomLevel -= 1
end
function CameraController:ResetZoom()
self.ZoomLevel = self.DefaultZoomLevel
end
Adding Init and Start Methods
Init and Start methods can then be added to this object:
The Init function is called when running Cradle:Start(), however it is not guaranteed that any other controller will be fully initialised, so in this stage other controllers should not be referenced.
local RunService = game:GetService("RunService")
function CameraController:Init()
local Camera = workspace.CurrentCamera
RunService.RenderStepped:Connect(function()
if Camera and Camera.FieldOfView ~= self.ZoomLevel then
Camera.FieldOfView = self.ZoomLevel
end
end)
end
The Start function is called when running Cradle:Start() once all :Init()
methods have ran, only at this stage other controllers are considered initialised and safe to use.
function CameraController:Start()
self:ResetZoom()
end
This is optional, however it is recommended to use these methods or leave them empty.
Server Communication
Server communication is done through Services. Services allows client code to access server methods and properties that have been explicitly exposed in the Client table.
See the Services Guide for more information.
An example of communicating with a service:
function CameraController:Start()
local SomeService = self:GetService("SomeService")
SomeService:DoSomething()
SomeService.SomeProperty:Get()
SomeService.SomeRemoteEvent:FireServer()
SomeService.SomeGroup.SomeRemoteEvent:FireServer()
SomeService.SomeEvent:Connect(function()
print("SomeEvent fired!")
end)
end
If the Client
table is omitted, the service will be server only and cannot be accessed from the client.
Full Example
CameraController
local RunService = game:GetService("RunService")
local Cradle = require(game:GetService("ReplicatedStorage").Cradle"))
local CameraController = Cradle:CreateController({
Name = "CameraController",
DefaultZoomLevel = 60, -- FOV
ZoomLevel = 0, -- FOV
})
function CameraController:SetZoom(input: number)
self.ZoomLevel = input -- Set the ZoomLevel property
end
function CameraController:ZoomIn()
self.ZoomLevel += 1 -- Increase the ZoomLevel property
end
function CameraController:ZoomOut()
self.ZoomLevel -= 1 -- Decrease the ZoomLevel property
end
function CameraController:ResetZoom()
self.ZoomLevel = self.DefaultZoomLevel -- Reset the ZoomLevel property
end
function CameraController:Init()
local Camera = workspace.CurrentCamera
RunService.RenderStepped:Connect(function()
if Camera and Camera.FieldOfView ~= self.ZoomLevel then
Camera.FieldOfView = self.ZoomLevel
end
end)
end
function CameraController:Start()
self:ResetZoom()
end