Dynamically create and destroy maps

# Course Introduction

Learn how to dynamically create and destroy maps.

##### Reference Guide

  * [World Instance](/docs?postId=984%7B%22target%22:%22_blank%22%7D)
  * [Create Instance Map](/docs/?postId=540%7B%22target%22:%22_blank%22%7D)
  * [DynamicMapService](/apiReference/Services/DynamicMapService%7B%22target%22:%22_blank%22%7D)
  * [DynamicMapResultCode](/apiReference/Enums/DynamicMapResultCode%7B%22target%22:%22_blank%22%7D)



# Source Map

Source maps are maps created by the creator in Maker, which are automatically created when entering the world without any script calls. The maps that can be used as source maps when creating dynamic maps depend on the type of room. In static rooms, only static maps can be used as source maps, and in instance rooms, only instance maps can be used as source maps.

#### Source Map Initial State

The initial state of the source map refers to the state of the map at the earliest point in time when it is automatically created upon entering the world. Dynamic maps are always created with the same initial state as the source map, regardless of the state of the source map at the time the map is dynamically created.

# Dynamic Map

Dynamic maps are maps that are dynamically created while playing the world. For example, depending on the world you're creating, you may need the same type of map with only certain people allowed on a map. You could copy the maps created by the creator in Maker and create as many maps as you need in advance. However, as the number of maps increases, it can put a strain on the world server and slow down world loading. This is where the DynamicMapService can help you create worlds efficiently by only creating them when you need them.

# Create Dynamic Map

When creating maps dynamically, use the `CreateDynamicMap()` function. When you create a map with this function, it returns a [DynamicMapResultCode](/apiReference/Enums/DynamicMapResultCode) as the result after `OnBeginPlay()` is called on all components belonging to the source map [png](1757313592097179ef701cdbc4f06847445e16dbe93dd) For maps that use dynamic maps, you should be careful not to perform operations in `OnBeginPlay()` that take a long time to execute, as this can adversely affect server performance. You should also be careful not to create dynamic maps in `OnBeginPlay()` when you create them. If you create a dynamic map in `OnBeginPlay()`, the return call can cause the dynamic map to be created infinitely repeatedly, resulting in an error

[png](17573136093876287b8a065484a47ae8ebb646ea7a3f7)

> Tip. When creating a dynamic map, you cannot change the properties of the map. Static maps of static rooms are created only as static maps, and instance maps are created dynamically only as instance maps.

#### Notes When Creating Dynamic Maps

When creating a dynamic map with the `DynamicMapService`, the basis is the initial state of the source map. If the map created in Maker looks like the one below, the dynamically created map will always be created in the state below. Even if the data in the source map that was first created in the world has changed, the changed data will not be reflected in the newly created dynamic map, but will be created in the initial state.

[{"index":"Initial State","0":"[png](1756980791490973aeecd2496440faba844a80585270c)"},{"index":"Source Map in Play","0":"[png](1756980818537570eca05f30a4376ba796d157114aeb7)"}]

When you create a dynamic map, it is created with the initial state as shown below. [png](1756980791490973aeecd2496440faba844a80585270c)

# Dynamic Maps and Property Types

When you create a dynamic map, if the property of the type that uses a path is in the initial state, you must use a relative path instead of an absolute path. The following property types must use relative paths.

  * Entity, Component, EntityRef, ComponentRef



For example, imagine that in the source map, as shown below, the destination of Portal A is Portal B and the destination of Portal B is Portal A. [png](1756980939504b01e26564c99479094d726cf011279f4)

Because the nature of dynamic maps is to replicate the initial data in the source map, this creates a problem when creating a dynamic map. The path remains the same absolute path in the newly created dynamic map, so if you navigate to Portal A in the newly created dynamic map, you will be directed to Portal B in DynamicMap1 (the source map).

[png](175730580015799115a0e1bf842aa88bf70fb01616fe8)

You can counteract this problem by directly specifying a reference destination at a specific point in time, such as `OnBeginPlay()`. For example, you can Extend the `PortalComponent` to specify the destination of the portal as a relative path.
    
    
    @Component
    script PortalConnector extends Component
        property string PortalEntityPath = nil
    
        method void OnBeginPlay()
        local fullPath = self.Entity.CurrentMap.Path .. "/" .. self.PortalEntityPath
        local targetEntity = _EntityService:GetEntityByPath(fullPath)
    
        if targetEntity == nil or targetEntity.PortalComponent == nil then
         log_warning("Target entity is invalid. path : " .. fullPath)
        	return
        end
        self.PortalEntityRef = targetEntity
        end
    end
    

# Removing a Dynamic Map

Use the `DestroyDynamicMap()` function to remove a dynamic map. It can only be removed if there are no users in the dynamic map. When you run the remove dynamic map function, it returns the result as [DynamicMapResultCode](/apiReference/Enums/DynamicMapResultCode).