#How can i implement Object Pooling inside of this module

1 messages · Page 1 of 1 (latest)

jolly mural
#
local raycastingModule = {}
    local a = game.Workspace.a
    local b = game.Workspace.B
    
    
    local function RayLaser(startPos, endPos)
        local rayLaser = Instance.new("Part", workspace)
        rayLaser.Name = "Raylaser"
        local distance = (endPos - startPos).Magnitude
        rayLaser.CanCollide = false
        rayLaser.CanTouch = false
        rayLaser.Anchored = true
        rayLaser.Size = Vector3.new(0.1, 0.1, distance)
        rayLaser.CFrame = CFrame.new(startPos, endPos) * CFrame.new(0, 0, -distance/2)
        rayLaser.Color = Color3.fromRGB(55, 255, 24)
        
        game:GetService("Debris"):AddItem(rayLaser, 0.1)
        return rayLaser
    end

while true  do
    local directionofray = b.Position - a.Position
    
    local params = RaycastParams.new()
    params.FilterType = Enum.RaycastFilterType.Exclude
    params.FilterDescendantsInstances = {workspace.Window}
    
    local RaycastingResult = workspace:Raycast(a.Position, directionofray, params)
    local rayLaser = RayLaser(a.Position, a.Position + directionofray)

    if RaycastingResult and RaycastingResult.Instance then
        if RaycastingResult.Instance == b then
            b.Color = Color3.fromRGB(89, 255, 0)
            rayLaser.Color = Color3.fromRGB(55, 255, 24)
        else
            b.Color = Color3.fromRGB(255, 0, 0)
            rayLaser.Color = Color3.fromRGB(255, 0, 0)
        end
    end

    print(RaycastingResult)
    task.wait(0.5)
end
return raycastingModule
#
I want to use Object pooling instead of constant Instantiation to make the raycast more optimized
zenith spoke
#

there was a module for it

#

i forgot what it was calle

#
#

id recommend looking into the scripts of these

#

i dont have an exact script for the implementation so these will help you get a general idea of how to go about implementing object pooling

#

by viewing the modules and such and what they do

#

i hope this helps somewhat, i recommend looking into these things before opening up a post because there are a lot of posts regarding object pooling, and you could probably find a devforum post that suits you and your needs, even if it's not directly related.

i assume this is a fairly common topic as raycasting and spawning instances can take up a lot of performance, and for some games like tds with a lot of bullets and effects, you need an effective solution for them.

have fun!

zenith spoke
#

i think you could do this by having a set of bullets, and then cloning a new one for use everytime you fire one, so it is just an infinite stock?

faint sierra
#

because cloning creates a new object, which is expensive

faint sierra