Factory - kuimoani/defold GitHub Wiki

Factory components

νŒ©ν† λ¦¬ μ»΄ν¬λ„ŒνŠΈλŠ” 였브젝트 ν’€(pool of objects)μ—μ„œ 싀행쀑인 κ²Œμž„μœΌλ‘œ κ²Œμž„μ˜€λΈŒμ νŠΈλ₯Ό λ™μ μœΌλ‘œ μŠ€ν°ν•˜λŠ”λ° μ‚¬μš©λ©λ‹ˆλ‹€. νŽ™ν† λ¦¬ μ»΄ν¬λ„ŒνŠΈλ₯Ό κ²Œμž„ μ˜€λΈŒμ νŠΈμ— μΆ”κ°€ν•˜κ³ , Prototype 속성에 νŠΉμ • κ²Œμž„ 였브젝트 νŒŒμΌμ„ μ§€μ •ν•˜λ©΄ 섀계도면 찍어내듯이 μƒˆ κ²Œμž„ 였브젝트λ₯Ό 찍어낼 수 μžˆμŠ΅λ‹ˆλ‹€.

Factory component

Factory component

κ²Œμž„ 였브젝트 생성을 트리거 ν•˜κΈ° μœ„ν•΄μ„œλŠ” factory.create()λ₯Ό ν˜ΈμΆœν•˜λ©΄ λ©λ‹ˆλ‹€.

factory.script

local p = go.get_position()
p.y = vmath.lerp(math.random(), min_y, max_y)
local component = "#star_factory"
factory.create(component, p)

Spawned game object

factory.create() ν•¨μˆ˜λŠ” 5개의 νŒŒλΌλ©”ν„°λ₯Ό κ°€μ§‘λ‹ˆλ‹€.

url

μƒˆ κ²Œμž„μ˜€λΈŒμ νŠΈλ₯Ό μŠ€ν°ν•˜λŠ” νŽ™ν† λ¦¬ μ»΄ν¬λ„ŒνŠΈμ˜ 아이디

[position]

(선택) μƒˆ κ²Œμž„ 였브젝트의 μ›”λ“œ ν¬μ§€μ…˜(world position). vector3 ν˜•μ‹μ΄λ©°, ν¬μ§€μ…˜μ„ μ •ν•˜μ§€ μ•ŠμœΌλ©΄ κ²Œμž„μ˜€λΈŒμ νŠΈκ°€ νŽ™ν† λ¦¬ μ»΄ν¬λ„ŒνŠΈμ˜ μœ„μΉ˜μ— 슀폰됨

[rotation]

μƒˆ κ²Œμž„ 였브젝트의 μ›”λ“œ λ‘œν…Œμ΄μ…˜(world rotation). quat(μΏΌν„°λ‹ˆμ˜¨) ν˜•μ‹μž„

[properties]

κ²Œμž„ μ˜€λΈŒμ νŠΈμ™€ μ΄ˆκΈ°ν™” λ˜λŠ” 슀크립트 μ†μ„±κ°’μ˜ Lua ν…Œμ΄λΈ”. μžμ„Έν•œ λ‚΄μš©μ€ Script properties 맀뉴얼 μ°Έκ³ 

[scale]

슀폰된 κ²Œμž„μ˜€λΈŒμ νŠΈμ˜ μŠ€μΌ€μΌ(scale). 0보닀 큰 숫자둜 μž…λ ₯ν•˜λ©΄ λͺ¨λ“  μΆ•(axis)에 λ™μΌν•œ μ‚¬μ΄μ¦ˆλ‘œ λ³€κ²½λ˜λ©°, vector3둜 μž…λ ₯ν•˜λ©΄ μΆ•λ§ˆλ‹€ λ‹€λ₯Έ 크기둜 μŠ€μΌ€μΌλ§ ν•  수 있음

예제:

factory.script

local p = go.get_position()
p.y = vmath.lerp(math.random(), min_y, max_y)
local component = "#star_factory"
-- νšŒμ „ 없이 λ‘λ°°μ˜ μŠ€μΌ€μΌλ‘œ μŠ€ν°ν•¨. star의 scoreλ₯Ό 10으둜 섀정함
factory.create(component, p, nil, { score = 10 }, 2.0)                           [1]

[1]. star κ²Œμž„ 였브젝트의 "score" 속성을 섀정함

star.script

go.property("score", 1)                                                                         [1]

local speed = -240

function update(self, dt)
    local p = go.get_position()
    p.x = p.x + speed * dt
    if p.x < -32 then
        go.delete()
    end
    go.set_position(p)
end

function on_message(self, message_id, message, sender)
    if message_id == hash("collision_response") then
        msg.post("main#gui", "add_score", {amount = self.score})         [2]
        go.delete()
    end
end

[1]. "score" 슀크립트 속성이 κΈ°λ³Έκ°’κ³Ό ν•¨κ»˜ μ •μ˜λ¨

[2]. "score" 슀크립트 속성을 "self"에 μ €μž₯된 κ°’μ²˜λŸΌ 참쑰함

Spawned game object with property and scaling

ν˜„μž¬ DefoldλŠ” 좩돌 λͺ¨μ–‘(collision shape)의 λΉ„κ·œκ²© μŠ€μΌ€μΌλ§(non uniform scaling)을 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. vmath.vector3(1.0, 2.0, 1.0) 와 같은 λΉ„κ·œκ²© μŠ€μΌ€μΌλ§ 값을 λ„˜κΈ°λ©΄ μŠ€ν”„λΌμ΄νŠΈλŠ” μ œλŒ€λ‘œ λ³€ν˜•λ˜μ§€λ§Œ 좩돌 λͺ¨μ–‘은 μ œλŒ€λ‘œ λ³€ν˜•λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

Instance limits

Project Settings에 μžˆλŠ” max_instances 속성은 κ²Œμž„ μ›”λ“œ(κ²Œμž„ μ‹œμž‘μ‹œ λ‘œλ“œλœ main.collection을 λΉ„λ‘―ν•΄ collection proxyλ₯Ό 톡해 λ‘œλ“œλœ λͺ¨λ“  μ›”λ“œ)에 μ‘΄μž¬ν•  수 μžˆλŠ” κ²Œμž„ 였브젝트 μΈμŠ€ν„΄μŠ€μ˜ 총 μˆ˜λŸ‰μ„ μ œν•œν•©λ‹ˆλ‹€. κ²Œμž„ μ›”λ“œμ— μ‘΄μž¬ν•˜λŠ” λͺ¨λ“  κ²Œμž„ μ˜€λΈŒμ νŠΈλŠ” μ—λ””ν„°μ—μ„œ 직접 λ°°μΉ˜ν•˜λ˜μ§€ 슀크립트둜 μŠ€ν°ν•˜λ˜μ§€μ™€ 상관없이 μ œν•œλœ μˆ˜λŸ‰ 만큼 카운트 λ©λ‹ˆλ‹€.

Max instances

κ·ΈλŸ¬λ―€λ‘œ λ§Œμ•½ max_instancesλ₯Ό 1024둜 μ„€μ •ν•˜κ³  메인 μ»¬λ ‰μ…˜μ— 24개의 κ²Œμž„μ˜€λΈŒμ νŠΈλ₯Ό μˆ˜λ™μœΌλ‘œ λ°°μΉ˜ν–ˆλ‹€λ©΄, 1000개의 κ²Œμž„μ˜€λΈŒμ νŠΈλ₯Ό μΆ”κ°€λ‘œ μŠ€ν°ν•  수 있게 λ©λ‹ˆλ‹€. 그리고 κ²Œμž„μ˜€λΈŒμ νŠΈλ₯Ό μ‚­μ œν•˜λ©΄, μ‚­μ œν•œ 만큼의 μΈμŠ€ν„΄μŠ€λ₯Ό μΆ”κ°€ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

Pooling of game objects

μŠ€ν°ν•œ κ²Œμž„ 였브젝트λ₯Ό ν’€(pool)에 μ €μž₯ν–ˆλ‹€κ°€ μž¬μ‚¬μš©ν•˜λŠ” 것은 쒋은 방법인 λ“― ν•˜μ§€λ§Œ μ„±λŠ₯μƒμ˜ 이유둜 이λ₯Ό 자제 ν•˜λŠ”κ²ƒμ΄ μ’‹μŠ΅λ‹ˆλ‹€. κ²Œμž„μ˜€λΈŒμ νŠΈλ₯Ό μ‚­μ œν•˜κ³  μƒˆλ‘œ μŠ€ν°ν•˜λŠ” 것이 풀링을 ν•˜λŠ” 것보닀 더 λΉ λ¦…λ‹ˆλ‹€(λ•Œλ‘œλŠ” 더 깔끔함).