이제 함수의 구조를 배웠으니 함수를 직접 작성해 볼 시간입니다. 작성할 함수는 두 개입니다.
지난 실습에서는 포트나이트 플레이어 캐릭터에게 피해를 입히는 데 필요한 코드 줄을 학습했습니다. 코드가 필요할 때마다 같은 줄을 입력할 필요가 없으면 더욱 바람직하겠죠?
-
HurtPlayer()로 명명된 함수를 선언합니다. 이 함수의 반환 타입은void로, 값을 반환하지 않는다는 의미입니다. Verse 파일 가장 마지막에 함수 선언을 넣습니다. 함수의 바디는 5강: 실습 시간의 코드와 동일하므로 필요하다면 다시 돌아가서 살펴보세요.HurtPlayer() : void = Playspace: fort_playspace = GetPlayspace() AllPlayers: []player = Playspace.GetPlayers() if (FirstPlayer : player = AllPlayers[0]): if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]): FortniteCharacter.Damage(50.0)다른 함수 안에 함수를 정의할 수는 없으므로 모든 함수가
OnBegin()과 동일한 수준으로 들여쓰기되었는지 확인하세요. -
이제 캐릭터에게 피해를 입히고 싶을 때 언제든지 사용할 수 있는 함수를 완성했습니다. 하지만 함수를 선언해도 함수 바디에서 코드가 실행되지는 않습니다. 이를 위해서는 함수를 호출 해야 합니다. 반드시
OnBegin()내에서 이 함수를 호출하세요.HurtPlayer() Print("Player Hurt") -
이 시점이면 캐릭터를 치유하는 메서드도 있다는 것을 유추하셨을 겁니다. 해당 메서드는
Heal()이라고 명명했으며,Damage()를 호출할 때와 완전히 같은 방법으로 호출할 수 있습니다. 이 코드를 사용하여 Verse 파일 마지막에HealPlayer()함수를 만들어 보세요.HealPlayer() : void = Playspace: fort_playspace = GetPlayspace() AllPlayers: []player = Playspace.GetPlayers() if (FirstPlayer : player = AllPlayers[0]): if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]): FortniteCharacter.Heal(20.0) -
이제
OnBegin()바디 내의 함수 호출에HealPlayer()호출을 추가합니다.HurtPlayer() Print("Player Hurt") Sleep(5.0) HealPlayer() Print("Player Healed")
이제 Sleep() 함수에 새로운 호출이 있는 것이 보일 겁니다. 기본적으로 Sleep() 을 사용하면 프로그램이 다음에 오는 코드를 계속 실행하기 전에 대기 상태가 됩니다. 대기 시간은 Sleep() 으로 전달하는 파라미터에 의해 결정되며, 단위는 초입니다. (파라미터에 관해서는 이후 수업에서 자세히 알아보겠습니다.) 여기에 Sleep() 을 사용하지 않으면 HealPlayer() 가 HurtPlayer()` 직후에 실행되므로 두 함수 호출의 이펙트를 인지할 수 없습니다.
전체 스크립트
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Playspaces }
using { /Verse.org/Simulation }
using { /Verse.org/Verse }
hello_world_device := class(creative_device):
# 실행 중인 게임에서 장치가 시작되면 실행됩니다.
OnBegin<override>()<suspends>:void=
HurtPlayer()
Print("Player Hurt")
Sleep(5.0)
HealPlayer()
Print("Player Healed")
HurtPlayer() : void =
Playspace: fort_playspace = GetPlayspace()
AllPlayers: []player = Playspace.GetPlayers()
if (FirstPlayer : player = AllPlayers[0]):
if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]):
FortniteCharacter.Damage(50.0)
HealPlayer() : void =
Playspace: fort_playspace = GetPlayspace()
AllPlayers: []player = Playspace.GetPlayers()
if (FirstPlayer : player = AllPlayers[0]):
if (FortniteCharacter : fort_character = FirstPlayer.GetFortCharacter[]):
FortniteCharacter.Heal(20.0)
다음 수업
%learn-code-basics-7-specifying-the-result-of-a-function-in-verse:topic%