Debris 서비스를 사용하면 개발자가 Debris:AddItem 메서드를 사용하여 코드를 생성하지 않고도 객체 제거를 예약 할 수 있다.
Debris는 서비스이므로 ServiceProvider:GetService 메서드를 사용하여 만들어야 한다.
Why use Debris?
더 이상 필요하지 않은 객체는 시스템 메모리를 사용하고, 이로 인해 시간이 지남에 따라 게임이 느리게 실행될 수 있다.
따라서 더 이상 필요하지 않은 객체에 대해 Instance:Destroy 함수를 통해 해당 객체를 제거하는 것이 좋다.
방금 발사된 발사체의 예를 들어 보자. 다음을 사용하여 발사체를 정리할 수 있다.
wait(3)
projectile:Destroy()
그러나 이 접근 방식에는 여러 가지 문제가 있다. 첫째, wait 코드를 생성해야 하고, 이것은 항상 바람직하지는 않다. 둘째, 3초가 지나기 전에 발사체가 이미 파괴되었을 수 있다. (예를 들어, Workspace.FallenPartsDestroyHeight에 도달 한 경우)
delay(3, function()
if projectile and projectile.Parent then
projectile:Destroy()
end
end)
위 코드는 해당 발사체가 파괴될 수 있는 상태일 경우에만 Destroy() 함수를 실행시키기 때문에 이러한 문제를 해결할 수 있다. 그러나 이 시점에서 간단한 명령문은 이미 상당히 복잡해지고, 불필요한 스레드가 생성되고 있다.
이제 Debris가 등장할 차례이다. 다음 코드는 Debris를 사용함으로써 위에서 언급한 모든 문제를 해결한다.
Debris:AddItem(projectile, 3)
Debris는 새 스레드가 필요하지 않고 객체가 이미 파괴 된 경우에도 오류가 발생하지 않는다. 이러한 이유로 Debris를 사용하는 것은 수명이 정해진 객체를 정리하는데 권장되는 방법이다.
Code Samples
while 루프에 part를 생성하고 parent를 Workspace로 지정한 다음 Debris.AddItem을 사용하여 정리한다.
local Debris = game:GetService("Debris")
local ball = Instance.new("Part")
ball.Anchored = false
ball.Shape = Enum.PartType.Ball
ball.TopSurface = Enum.SurfaceType.Smooth
ball.BottomSurface = Enum.SurfaceType.Smooth
ball.Size = Vector3.new(1, 1, 1)
while true do
newBall = ball:Clone()
newBall.BrickColor = BrickColor.Random()
newBall.CFrame = CFrame.new(0, 30, 0)
newBall.Velocity = Vector3.new(math.random(-10, 10), 0, math.random(-10, 10))
newBall.Parent = game.Workspace
Debris:AddItem(newBall, 2)
wait(0.1)
end
참고 링크
developer.roblox.com/en-us/api-reference/class/Debris
Debris
This Platform uses cookies to offer you a better experience, to personalize content, to provide social media features and to analyse the traffic on our site. For further information, including information on how to prevent or manage the use of cookies on t
developer.roblox.com
'API 문서' 카테고리의 다른 글
WeldConstraints (0) | 2021.04.17 |
---|---|
ManualWeld (0) | 2021.04.11 |
TweenService (0) | 2021.04.11 |
UserInputService (0) | 2021.04.10 |
ContentProvider (0) | 2021.04.10 |
댓글