지난 실습의 마법 물약 코드로 돌아가겠습니다. 플레이어가 치유 물약을 마시면 치유 코드를 실행하고 대미지 물약을 마시면 피해 코드를 실행하는 것이 좋습니다.
우선 코드를 작성합니다 ...
-
물약 타입에 관한 질문에 답변해야 할 것 같습니다. 이를 추적하기 위해
PotionType이라는 string을 새로운 변수로 생성합니다.var PotionType: string = "heal" -
이제
PotionType변수가 다른 스트링과 동일한지 물어볼 수 있습니다.PotionType이"heal"과 동일한지 묻는다면 대답은 '예'일 겁니다. 그러면 치유 코드가 실행됩니다.if (PotionType = "heal"): set PlayerHealth = PlayerHealth + PotionHealAmount -
이제 피해 코드에 대해서도 유사한 if 표현식을 작성합니다.
if (PotionType = "damage"): set PlayerHealth = PlayerHealth - PotionDamageAmount근데 잠깐만요! 치유 코드에 문제가 있습니다!
플레이어가 물약으로 체력을 무제한으로 올리는 것을 막을 방법이 없습니다. 치유에 제한을 두는 것이 좋겠습니다.
if를 사용하면 가능합니다! -
우선
float타입의 새로운 상수를 선언합니다. 이름은MaxHealth로 정하고100.0으로 설정합니다.MaxHealth: float = 100.0플레이어가 치유 물약을 마셨을 때
PlayerHealth의 값이MaxHealth이상으로 올라가는 경우에는PlayerHealth를MaxHealth로 설정해야 합니다.# 플레이어가 치유 물약을 마셨을 때 실행할 코드 # 플레이어가 치유되었을 때 체력이 MaxHealth를 초과하지 않는 경우 # 최대량을 회복 if (PotionType = "heal" and (PlayerHealth + PotionHealAmount) < MaxHealth): set PlayerHealth = PlayerHealth + PotionHealAmount else: set PlayerHealth = MaxHealth위의 코드에는
and가 있어서PotionType이"heal"과 동일하면서도PlayerHealth + PotionAmount가MaxHealth보다 적은지 묻는 부분에 주목하세요. PlayerHealth가 PotionHealAmount만큼 증가하려면 두 가지 조건 모두 만족해야 합니다.이 코드를 읽는 사람은 PlayerHealth가 MaxHealth를 초과하지 않게 하기 위한 코드라는 점을 명확히 파악하기 어려울 수 있습니다. 바로 이러한 부분에서 코드 코멘트를 유용하게 사용할 수 있습니다. 실제 코드 위에 코멘트 3줄이 있는 것에 주목하세요.
-
피해 코드가 잘 작동하지만,
if…else if…else를 사용하면 개선할 수 있습니다. 플레이어가 대미지 물약을 마셨을 때 PlayerHealth가0.0이하로 떨어지는 경우에는 대신 PlayerHealth를1.0으로 설정하는 것이 좋습니다. PlayerHealth가 이미1.0인 경우0.0으로 설정합니다. 이렇게 하면 플레이어에게 너무 가혹한 불이익을 주지 않으면서 물약이 해롭다는 것을 알려줄 수 있습니다.# 플레이어가 대미지 물약을 마셨을 때 실행할 코드 # 플레이어의 체력이 MinHealth를 초과하지만 PotionDamageAmount 미만인 경우 플레이어를 처치하지 않음 # 플레이어의 체력이 이미 MinHealth 이하인 경우 플레이어를 처치함 if (PotionType = "damage" and PlayerHealth > PotionDamageAmount): set PlayerHealth = PlayerHealth - PotionDamageAmount else if (PlayerHealth > MinHealth): # 플레이어의 체력이 낮은 경우 한 번 더 기회를 부여합니다. set PlayerHealth = 1.0 else: set PlayerHealth = 0.0
... 버그 발견
아래에 이 실습에서 다룬 모든 코드가 있습니다. 테스트를 위한 몇 가지 Print() 함수도 있습니다. 이 코드를 실행해보세요. Print() 호출은 자유롭게 변경해 보세요. PotionType 변수를 "heal" 로 초기화했으니 치유 코드만 실행되리라고 예상할 수 있습니다.
하지만 버그가 더 있습니다!
아래 코드를 실행하고 버그를 찾아보세요.
MaxHealth: float = 100.0
MinHealth: float = 1.0
var PotionType: string = "heal"
# 플레이어가 치유 물약을 마셨을 때 실행할 코드
# 플레이어가 치유되었을 때 체력이 MaxHealth를 초과하지 않는 경우
# 최대량을 회복
if (PotionType = "heal" and (PlayerHealth + PotionHealAmount) < MaxHealth):
set PlayerHealth = PlayerHealth + PotionHealAmount
Print ("Full heal")
else:
# 그 외의 경우, PlayerHealth를 MaxHealth로 설정
set PlayerHealth = MaxHealth
Print("PlayerHealth too high for full heal")
# 플레이어가 대미지 물약을 마셨을 때 실행할 코드
# 플레이어의 체력이 MinHealth를 초과하지만 PotionDamageAmount 미만인 경우 플레이어를 처치하지 않음
# 플레이어의 체력이 이미 MinHealth 이하인 경우 플레이어를 처치함
if (PotionType = "damage" and PlayerHealth > PotionDamageAmount):
set PlayerHealth = PlayerHealth - PotionDamageAmount
Print("Full damage")
else if (PlayerHealth > MinHealth):
# 플레이어의 체력이 낮은 경우 한 번 더 기회를 부여합니다.
set PlayerHealth = 1.0
Print("PlayerHealth set to 1.0")
else:
# 플레이어를 처치합니다.
set PlayerHealth = 0.0
Print("Player eliminated!")
PotionType 이 치유로 설정되면 치유 코드만 실행되어야 합니다. 하지만 피해 코드의 if … else if … if 표현식도 여전히 실행됩니다. 즉, PlayerHealth 가 MinHealth 보다 높으면 PlayerHealth 가 1.0 으로 설정됩니다. 이건 원하는 결과가 아닙니다. PotionType 만을 체크하는 if 표현식 내에서 다른 체크를 중첩하면 문제를 해결할 수 있습니다.
# 플레이어가 치유 물약을 마셨을 때 실행할 코드
# 플레이어가 치유되었을 때 체력이 MaxHealth를 초과하지 않는 경우
# 최대량을 회복
if (PotionType = "heal"):
if ((PlayerHealth + PotionHealAmount) < MaxHealth):
set PlayerHealth = PlayerHealth + PotionHealAmount
Print ("Full heal")
else:
# PlayerHealth를 MaxHealth로 설정
set PlayerHealth = MaxHealth
Print("PlayerHealth too high for full heal")
# 플레이어가 대미지 물약을 마셨을 때 실행할 코드
# 플레이어의 체력이 MinHealth를 초과하지만 PotionDamageAmount 미만인 경우 플레이어를 처치하지 않음
# 플레이어의 체력이 이미 MinHealth 이하인 경우 플레이어를 처치함
if (PotionType = "damage"):
if ((PlayerHealth > PotionDamageAmount)):
set PlayerHealth = PlayerHealth - PotionDamageAmount
Print("Full damage")
else if (PlayerHealth > MinHealth):
# 플레이어의 체력이 낮은 경우 한 번 더 기회를 부여합니다.
set PlayerHealth = 1.0
Print("PlayerHealth set to 1.0")
else:
# 플레이어를 처치합니다.
set PlayerHealth = 0.0
Print("Player eliminated!")
이제 PotionType 을 체크하는 if 표현식 아래에 들여쓰기된 코드 블록만 실행됩니다. PotionType 을 "damage" 로도 설정하고 이 코드를 실행해 보세요.
휴! 지금까지 변경할 코드가 꽤 많았지만, 잘 해냈습니다!
잠시 휴식을 취하고 다음 수업을 위한 준비가 되면 돌아오세요.
전체 스크립트
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
hello_world_device := class(creative_device):
# 실행 중인 게임에서 장치가 시작되면 실행됩니다.
OnBegin<override>()<suspends> : void =
MaxHealth : float = 100.0
MinHealth : float = 1.0
var PotionType: string = "heal"
set PlayerHealth = 80.0
# 플레이어가 치유 물약을 마셨을 때 실행할 코드
# 플레이어가 치유되었을 때 체력이 MaxHealth를 초과하지 않는 경우
# 최대량을 회복
if (PotionType = "heal"):
if ((PlayerHealth + PotionHealAmount) < MaxHealth):
set PlayerHealth = PlayerHealth + PotionHealAmount
Print ("Full heal")
else:
# 그 외의 경우, PlayerHealth를 MaxHealth로 설정
set PlayerHealth = MaxHealth
Print("PlayerHealth too high for full heal")
# 플레이어가 대미지 물약을 마셨을 때 실행할 코드
# 플레이어의 체력이 MinHealth를 초과하지만 PotionDamageAmount 미만인 경우 플레이어를 처치하지 않음
# 플레이어의 체력이 이미 MinHealth 이하인 경우 플레이어를 처치함
if (PotionType = "damage"):
if ((PlayerHealth > PotionDamageAmount)):
set PlayerHealth = PlayerHealth - PotionDamageAmount
Print("Full damage")
else if (PlayerHealth > MinHealth):
# 플레이어의 체력이 낮은 경우 한 번 더 기회를 부여합니다.
set PlayerHealth = 1.0
Print("PlayerHealth set to 1.0")
else:
set PlayerHealth = 0.0
Print("Player eliminated!")
Print("PlayerHealth now {PlayerHealth}")