ソースを参照

Update factory.md

Björn Ritzl 5 年 前
コミット
a0d8b8e077
1 ファイル変更33 行追加0 行削除
  1. 33 0
      docs/en/manuals/factory.md

+ 33 - 0
docs/en/manuals/factory.md

@@ -86,6 +86,39 @@ end
 Defold does not currently support non uniform scaling of collision shapes. If you provide a non uniform scale value, for instance `vmath.vector3(1.0, 2.0, 1.0)` the sprite will scale correctly but the collision shapes won't.
 :::
 
+
+## Addressing of factory created objects
+
+Defold's addressing mechanism makes it possible to access every object and component in a running game. The [Addressing manual](/manuals/addressing/) goes into quite a bit of detail how the system works. It is possible to use the same addressing mechanism for spawned game objects and their components. It is quite often enough to use the id of the spawned object, for instance when sending a message:
+
+```lua
+local function create_hunter(target_id)
+    local id = factory.create("#hunterfactory")
+    msg.post(id, "hunt", { target = target_id })
+    return id
+end
+```
+
+::: sidenote
+Message passing to the game object itself instead of a specific component will in fact send the message to all components. This is usually not a problem but it's good to keep in mind if the object has a lot of components.
+:::
+
+But what if you need to access a specific component on a spawned game object, for instance to disable a collision object or change a sprite image? The solution is to construct a URL from the game object id and the id of the component.
+
+```lua
+local function create_guard(unarmed)
+    local id = factory.create("#guardfactory")
+    if unarmed then
+        local weapon_sprite_url = msg.url(nil, id, "weapon")
+        msg.post(weapon_sprite_url, "disable")
+
+        local body_sprite_url = msg.url(nil, id, "body")
+        sprite.play_flipbook(body_sprite_url, hash("red_guard"))
+    end
+end
+```
+
+
 ## Tracking spawned and parent objects
 
 When you call `factory.create()` you get back the id of the new game object, allowing you to store the id for future reference. One common use is to spawn objects and add their id's to a table so you can delete them all at a later point, for instance when resetting a level layout: