Skip to content

Commit 4c87723

Browse files
ArthurVoxowladuermael
authored andcommitted
docs : update object
1 parent dc06c90 commit 4c87723

File tree

1 file changed

+97
-53
lines changed

1 file changed

+97
-53
lines changed

lua/docs/content/reference/object.yml

Lines changed: 97 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ properties:
3636
description: |
3737
Collision groups the [This] belongs to.
3838
39-
⚠️ It doesn't mean the [This] will collide with other [Object]s in these groups.
39+
⚠️ It doesn't mean the [This] will collide with other [Object]s in these groups.
4040
4141
If the [This] belongs to group number `3` for example, it means all [Object]s that have group number `3` in their [Object].[CollidesWithGroups](#property-collideswithgroups) property will collide with it.
4242
@@ -45,46 +45,79 @@ properties:
4545
- [Player]s collide with the [Map] only
4646
4747
That can all be configured differently depending on your needs.
48+
49+
Collision groups are best used as buckets created for gameplay needs, for example,
50+
- a group for "hittable" objects that can be damaged by the player's weapon
51+
- a group for recyclable Objects that are disabled and thrown back into their pool when they reach a certain trigger collider
52+
- a group for static scenery objects that moving objects will bump into
53+
- a group for objects that can get hit by raycasts executed for some specific game mechanic
54+
It may well be the case that one object belongs to several or all of these groups, depending on the gameplay associated with it.
55+
56+
In general, it is bad practice to assign or add all collision groups from one object to another (unless making exact copies), as it isn't future-proof if you later add more groups. Instead, you should assign individual groups depending on what this new object should contribute to in your world.
4857
4958
samples:
5059
- code: |
51-
local object1 = Object()
52-
local object2 = Object()
53-
object1.Physics = PhysicsMode.Dynamic
54-
object2.Physics = PhysicsMode.Dynamic
55-
56-
-- making sure 2 objects collide with each other
57-
-- NOTE: by default:
58-
-- Map.CollisionGroups == {1},
59-
-- Player.CollisionGroups == {2},
60-
-- Object.CollisionGroups == {3}
61-
object1.CollisionGroups = {5}
62-
object2.CollisionGroups = {5}
63-
object1.CollidesWithGroups = {1, 5} -- collides with Map + objects in group 5
64-
object2.CollidesWithGroups = {1, 5} -- collides with Map + objects in group 5
65-
66-
-- would also work this way if you don't
67-
-- remember Map's group (which can be changed too by the way)
68-
object1.CollidesWithGroups = Map.CollisionGroups + {5}
69-
70-
-- making an object collides with the Map and Players
71-
local object = Object()
72-
object.CollidesWithGroups = Map.CollisionGroups + Player.CollisionGroups
60+
-- these are the group numbers assigned by default to Map, Player, and all Objects
61+
local GROUP_MAP = 1
62+
local GROUP_PLAYER = 2
63+
local GROUP_OBJECT = 3
7364
74-
-- for Player (local player) to collide with other players and the Map
75-
Player.CollidesWithGroups = Map.CollisionGroups + Player.CollisionGroups
65+
-- by default, this object will collide with the map and other objects
66+
local o = Object()
67+
o.Physics = PhysicsMode.Dynamic
68+
69+
-- when in doubt, print the groups of your objects
70+
print(Map.CollisionGroups, Map.CollidesWithGroups)
71+
print(o.CollisionGroups, o.CollidesWithGroups)
72+
73+
-- let's say our world needs an extra group for a gameplay mechanic, for example, a group for objects that can be hit by player's weapon (which could perform a raycast in that group)
74+
local GROUP_HITTABLE = 4
75+
76+
-- here, the object would collide with anything that cares about GROUP_OBJECT or GROUP_HITTABLE, in addition to itself wanting to collide with GROUP_MAP and GROUP_PLAYER
77+
o.CollisionGroups = { GROUP_OBJECT, GROUP_HITTABLE }
78+
o.CollidesWithGroups = { GROUP_MAP, GROUP_PLAYER }
79+
80+
-- when adding new obstacle/scenery objects to our scene, you can simply re-use the GROUP_MAP as a way to say "this is part of the environment that is collidable"
81+
-- here, maybe this object only acts as an obstacle and doesn't contribute to anything else
82+
someBigRock.CollisionGroups = GROUP_MAP
83+
someBigRock.CollidesWithGroups = nil
84+
85+
-- doing this effectively prevents all physics interaction on an object
86+
o.CollisionGroups = nil
87+
o.CollidesWithGroups = nil
88+
89+
-- note that there is redundancy with CollisionGroups and CollidesWithGroups, so you can organize your groups however seems more convenient for you
90+
-- for example the following setups will all result in object1 and object2 colliding with each other
91+
object1.CollisionGroups = GROUP_OBJECT
92+
object1.CollidesWithGroups = nil
93+
object2.CollisionGroups = GROUP_OBJECT
94+
object2.CollidesWithGroups = GROUP_OBJECT
95+
96+
object1.CollisionGroups = GROUP_OBJECT
97+
object1.CollidesWithGroups = GROUP_OBJECT
98+
object2.CollisionGroups = GROUP_OBJECT
99+
object2.CollidesWithGroups = nil
100+
101+
object1.CollisionGroups = GROUP_OBJECT
102+
object1.CollidesWithGroups = GROUP_OBJECT
103+
object2.CollisionGroups = GROUP_OBJECT
104+
object2.CollidesWithGroups = GROUP_OBJECT
105+
106+
object1.CollisionGroups = GROUP_OBJECT
107+
object1.CollidesWithGroups = nil
108+
object2.CollisionGroups = nil
109+
object2.CollidesWithGroups = GROUP_OBJECT
76110
77111
- name: "CollidesWithGroups"
78112
type: "CollisionGroups"
79113
description: |
80-
Collision groups the [This] collides with.
114+
Collision groups the [This] collides with. See [This].[CollisionGroups](#property-collisiongroups).
81115
82116
By default:
83117
- [Object]s collide with the [Map] and other [Object]s
84118
- [Player]s collide with the [Map] and the [Object]s
85119
86120
That can all be configured differently depending on your needs.
87-
88121
samples:
89122
- code: |
90123
local object = Object()
@@ -247,12 +280,15 @@ properties:
247280
- name: "Tick"
248281
type: "function"
249282
description: |
250-
Tick is a [function] executed ~30 times per second when set ([nil] by default). Provides the [This] and elapsed time in seconds as parameters.
283+
Tick is a [function] executed each frame when set ([nil] by default). Provides the [This] and elapsed time in seconds as parameters.
284+
285+
For general purposes, you may consider using [Client.Tick](reference/client#functions-tick) instead. It is functionally the same, but is executed once for your world per frame, rather than once per frame per object.
286+
287+
The exact number of frames per second may fluctuate and depend on the device. It is typically around 60 frames per second.
251288
samples:
252289
- code: |
253-
-- executed ~30 times per second on each user device
254290
myObject.Tick = function(object, dt)
255-
print("elapsed:", dt, "seconds")
291+
print("elapsed:", dt, "seconds since last frame")
256292
end
257293
258294
- name: "LocalRotation"
@@ -267,8 +303,7 @@ properties:
267303
description: |
268304
Velocity of the [This] in world coordinates per second.
269305
270-
⚠️ `Velocity` only affects [This] when [This].[Physics](#property-physics) is `PhysicsMode.Dynamic`.
271-
Whenever [Physics](#property-physics) is set to `PhysicsMode.Disabled`, `Velocity` is set to `{0,0,0}`.
306+
⚠️ `Velocity` only affects [This] when [This].[Physics](#property-physics) is `PhysicsMode.Dynamic`.
272307
samples:
273308
- code: |
274309
-- makes myObject jump:
@@ -277,18 +312,11 @@ properties:
277312
- name: "Motion"
278313
type: "Number3"
279314
description: |
280-
Be aware, this `Motion` property is a hack regarding laws of physics. (sorry Isaac)
281-
282-
But it's very practical to move objects without worrying about forces at play.
283-
284-
This is what's being used by default when you're moving around with your avatar (see [Client.DirectionalPad](/reference/client#property-directionalpad)). It's the reason why you can stop moving horizontally while in the air.
315+
`Motion` is an instantaneous displacement that contributes to moving [This] every frame, without changing [This].[Velocity](#property-velocity) directly, and without being affected by collision responses and other forces. It is expressed in world coordinates per second.
285316
286-
Basically, `Motion` is an instantaneous displacement that contributes to moving [This] every frame, without changing [This].[Velocity](#property-velocity) directly.
317+
It's very useful to quickly implement character movement. This is what's being used by default when you're moving around with your avatar (see [Client.DirectionalPad](/reference/client#property-directionalpad)). It's the reason why you can stop moving horizontally while in the air.
287318
288-
`Motion` is expressed in world coordinates per second.
289-
290-
⚠️ `Motion` only affects [This] when [This].[Physics](#property-physics) is `PhysicsMode.Dynamic`.
291-
Whenever [Physics](#property-physics) is set to `PhysicsMode.Disabled`, `Motion` is set to `{0,0,0}`.
319+
⚠️ `Motion` only affects [This] when [This].[Physics](#property-physics) is `PhysicsMode.Dynamic`.
292320
samples:
293321
- code: |
294322
local speed = 10
@@ -297,24 +325,35 @@ properties:
297325
-- If the Camera rotates after this, it won't change where myObject is heading.
298326
299327
- name: "LocalScale"
300-
type: "number"
328+
type: "Number3"
301329
description: |
302-
Scale of the [Object], in its parent.
330+
Scale of the [Object], in its parent local space. Can be set to a `number` for a uniform scale.
303331
304332
Nested [Object] local scales are combined to obtain the "world scale" ([Object.LossyScale](#property-lossyscale)), the [Object]'s final scale.
305333
samples:
306334
- code: |
307-
myObject.LocalScale = 2 -- the Object is now 2 times bigger
335+
-- make the object twice as big
336+
myObject.LocalScale = 2
337+
338+
-- equivalent to
339+
myObject.LocalScale = Number3(2, 2, 2)
340+
myObject.LocalScale = { 2, 2, 2 }
308341
- code: |
309-
topLevelObject.LocalScale = 2
310-
local o = Object()
311-
o.LocalScale = 0.5
312-
topLevelObject:AddChild(o) -- o becomes a child of topLevelObject
313-
-- o ends up being displayed with a scale of 1
342+
-- demonstrate how scales are combined in the hierarchy
343+
local parentObject = Object()
344+
parentObject:SetParent(World)
345+
parentObject.LocalScale = 2
346+
347+
local childObject = Object()
348+
childObject:SetParent(parentObject)
349+
childObject.LocalScale = 0.5
350+
351+
-- here, the scales would cancel each other: 2 x 0.5 = 1
352+
print(childObject.LossyScale) -- returns (1,1,1)
314353
315354
- name: "LossyScale"
316355
read-only: true
317-
type: "number"
356+
type: "Number3"
318357
description: |
319358
Convenience property that attempts to match the actual world scale as much as it can. Note that [Object]s that have multiple levels of nested rotations and scales will return a skewed lossy scale.
320359
@@ -425,6 +464,9 @@ functions:
425464
426465
The `keepWorld` optional parameter, `false` by default, dictates whether to maintain the child's world or local position and rotation. Keeping world will ensure the object doesn't move in the scene, adjusting its local position/rotation accordingly; keeping local will have the object move in the scene in order to maintain an equivalent local position/rotation relative to its new parent.
427466
467+
It's a good practice to set child/parent relationships before setting positions, if you do not want to use the `keepWorld` parameter.
468+
469+
Note that there is a cyclic hierarchy protection that will prevent you from creating parenting loops.
428470
samples:
429471
- code: |
430472
local o = Object()
@@ -465,7 +507,7 @@ functions:
465507
466508
The `config` table accepts two boolean options:
467509
- `deepFirst`: if `true`, traverses depth-first instead of root-first. Default `false`.
468-
- `includeRoot`: if `true`, includes [This] in the recursion. Default `false`.
510+
- `includeRoot`: if `true`, includes [This] in the recursion. Default `true`.
469511
arguments:
470512
- name: "callback"
471513
type: "function"
@@ -567,7 +609,9 @@ functions:
567609
568610
The `keepWorld` optional parameter, `false` by default, dictates whether to maintain the child's world or local position and rotation. Keeping world will ensure the object doesn't move in the scene, adjusting its local position/rotation accordingly; keeping local will have the object move in the scene in order to maintain an equivalent local position/rotation relative to its new parent.
569611
570-
It's also a good practice to set child/parent relationships before setting positions.
612+
It's a good practice to set child/parent relationships before setting positions, if you do not want to use the `keepWorld` parameter.
613+
614+
Note that there is a cyclic hierarchy protection that will prevent you from creating parenting loops.
571615
572616
arguments:
573617
- name: "parent"

0 commit comments

Comments
 (0)