/
Debris.yaml
156 lines (141 loc) · 4.58 KB
/
Debris.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
name: Debris
type: class
category: Utility
memory_category: Instances
summary: |
Allows scheduling the guaranteed destruction of an object without yielding.
.
description: |
The **Debris** service allows scheduling guaranteed destruction of an object
without yielding.
#### Advantages
Besides creating a bit of a mess, objects that are no longer required can use
up system memory and cause an experience to run slower over time. For this
reason, it's always advised to call `Class.Instance:Destroy()` on objects you
no longer need. In some cases, however, an object may have a specific period
of utility before it can be destroyed.
Consider a wall being smashed into individual bricks. If you want a brick to
linger for 3 seconds before being destroyed, you can use the following code:
```lua
task.wait(3)
brick:Destroy()
```
However, waiting causes the thread to yield which may be undesired. To avoid
yielding, a callback function can be scheduled to run on a new thread after 3
seconds:
```lua
task.delay(3, function()
brick:Destroy()
end)
```
Or in one line:
```lua
task.delay(3, brick.Destroy, brick)
```
While this now avoids yielding, it has a potential drawback in that the
scheduled callback will never run if the script is disabled or destroyed
before the callback runs.
This is where `Class.Debris` has a specific advantage, as it does not yield
the current thread and runs outside the context of the script, guaranteeing
the instance is eventually destroyed even if the script is disabled or
destroyed. The following code does not yield and guarantees the instance will
be destroyed:
```lua
Debris:AddItem(brick, 3)
```
Note that `Class.Debris` has a hardcoded maximum of 1,000 objects, so if more
than 1,000 items are added, the oldest debris will be destroyed instantly to
make room for new debris.
code_samples:
- Debris-AddItem
inherits:
- Instance
tags:
- NotCreatable
- Service
deprecation_message: ''
properties:
- name: Debris.MaxItems
summary: |
The maximum number of items that can be assigned to the `Class.Debris`
service at one time.
description: |
The maximum number of items that can be assigned to the Debris service at
one time.
If this number is exceeded, objects are automatically destroyed in order
from oldest to newest until the amount is less than or equal to MaxItems.
This property is currently restricted and will error if set. The value is
hardcoded to 1,000 items.
code_samples:
type: int
tags:
- Deprecated
deprecation_message: |
This property is deprecated and should not be used in new work.
security:
read: None
write: None
thread_safety: ReadSafe
category: Data
serialization:
can_load: true
can_save: true
methods:
- name: Debris:AddItem
summary: |
Schedules a given `Class.Instance` for destruction within the specified
lifetime.
description: |
Schedules a given `Class.Instance` for destruction within the specified
lifetime. After the `lifetime` argument has elapsed, the object is
destroyed in the same manner as `Class.Instance:Destroy()`. Note that the
`lifetime` argument is optional and defaults to 10 seconds.
Note that `Class.Debris` has a hardcoded maximum of 1,000 objects, so if
more than 1,000 items are added, the oldest debris will be destroyed
instantly to make room for new debris. This means you should treat the
`lifetime` parameter as a **maximum** lifetime, not an exact lifetime.
code_samples:
- Debris-AddItem
parameters:
- name: item
type: Instance
default:
summary: |
The `Class.Instance` to add to `Class.Debris`.
- name: lifetime
type: double
default: 10
summary: |
Number of seconds before the `Class.Instance` should be destroyed.
returns:
- type: void
summary: ''
tags: []
deprecation_message: ''
security: None
thread_safety: Unsafe
- name: Debris:addItem
summary: ''
description: ''
code_samples:
parameters:
- name: item
type: Instance
default:
summary: ''
- name: lifetime
type: double
default: 10
summary: ''
returns:
- type: void
summary: ''
tags:
- Deprecated
deprecation_message: |
This function is a deprecated variant of `Class.Debris:AddItem()` which
should be used instead.
security: None
thread_safety: Unsafe
events: []
callbacks: []