본문 바로가기
대학생활/수업

리얼타임엔진 11주차 - Verse 튜토리얼2

by se.jeon 2023. 11. 14.
728x90
반응형

https://youtu.be/L9ndlyr8bjw

 

필요한 셋업

1. Spawn 설정

2. 오버랩 설정

3. 태그 설정

 

Item Spawner 설치 후 셋업 진행

- Spawn Item on Timer 해제

- Respawn Item on Timer 해제

 

오버랩 설정

- Run Over Pickup 체크

- Allow Spawning when blocked 체크

 

태그에 VerseTagMarkup 컴포넌트 추가

 

using { /Verse.org/Simulation/Tags } # 추가 후
coin_spawner := class(tag){} # 추가를 통해 태그를 제작할 수 있다.

 

태그 제작 후 Verse Build를 수행하면 태그가 에디터에 추가된다.

 

 

여러개를 관리하기 위해 array를 선언 후 오브젝트 연결

    @editable
    CoinSpawnerDevices : [] item_spawner_device = array {}

 

 

subscibe를 하면 구독과 같이 이벤트를 감지할 수 있다.

item_spawner_device을 검색 해 보면 base_item_spawner_device를 받고있는 것을 확인할 수 있다.

우리가 찾고있는 PickedUp 이벤트를 찾기 위해서는 base_item_spawner_device로 이동해야 한다.

 

 

소스코드 추가

using { /Verse.org/Random }

    HandleCoinPickedUp(Agent : agent) : void=
        SpawnCoin()

    # 랜덤하게 스폰되게끔 구성
    SpawnCoin() : void=
        if:
            SpawnDeviceIndex:int = GetRandomInt(0, CoinSpawnerDevices.Length - 1)
            NextCoinSpawner := CoinSpawnerDevices[SpawnDeviceIndex]
        then:
            NextCoinSpawner.SpawnItem()

 

 

 

최종 코드

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Simulation/Tags }
using { /Verse.org/Random }

coin_spawner := class(tag){}

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.

# A Verse-authored creative device that can be placed in a level
CoinCollectDevice := class(creative_device):

    # 맵에 배치된 타이머 디바이스를 받아온다.
    # 에디터에서 배치한 애들을 연결시키기 위해 editable 속성을 지정 해 준다.
    @editable
    TimerDevice : timer_device = timer_device {}

    @editable
    EndgameDevice : end_game_device = end_game_device {}

    @editable
    CoinSpawnerDevices : [] item_spawner_device = array {}

    @editable
    ScoreManagerDevice : score_manager_device = score_manager_device {}


    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        # TimerDevice에 연결 해 준다.
        TimerDevice.SuccessEvent.Subscribe(HandleCollectTimerFinished)
        SetupCoinDevices()

        for(CoinSpawnerDevice:CoinSpawnerDevices):
            CoinSpawnerDevice.ItemPickedUpEvent.Subscribe(HandleCoinPickedUp)
        SpawnCoin()


    # agent = user, 인자로 받는 것. ?는 있을 수도 있고 없을 수도 있음을 의미함.
    # TimerDevice에는 ?agent 인자가 있고, 이런 종류의 함수만 만들 수 있기 때문에 추가 해 주어야 한다.
    # Verse에서 타입은 소문자, 변수는 대문자를 사용한다.
    HandleCollectTimerFinished(MaybeAgent:?agent) : void=
        if(Agent := MaybeAgent?):
            EndgameDevice.Activate(Agent)
    # verse에서 모든 표현식에는 결과가 있다.


    HandleCoinPickedUp(Agent : agent) : void=
        ScoreManagerDevice.Activate(Agent)
        ScoreManagerDevice.SetScoreAward(1)
        SpawnCoin()

    
    # 랜덤하게 스폰되게끔 구성
    SpawnCoin() : void=
        if:
            SpawnDeviceIndex:int = GetRandomInt(0, CoinSpawnerDevices.Length - 1)
            NextCoinSpawner := CoinSpawnerDevices[SpawnDeviceIndex]
        then:
            NextCoinSpawner.SpawnItem()
        
    SetupCoinDevices():void=
        TaggedActors := GetCreativeObjectsWithTag(coin_spawner{})
728x90
반응형