Switch Action 라이브러리
Switch Action 라이브러리는 게임 액션 코드에서 스위치 문을 만들기 위해 필요한 동작들을 포함하고 있습니다. 많은 상황에서 특정 값에 따라 인스턴스가 어떤 액션을 완료하게 하고자 할 때가 있습니다. 여러 개의 "if" 문을 사용하는 것도 가능하지만, 선택지가 두 개나 세 개를 넘으면 코드가 복잡해질 수 있습니다. 이런 경우에 "switch" 동작을 사용하는 것이 더 좋습니다.
Switch 동작의 작동 방식
- 값 A(일반적으로 어떤 표현식이나 변수에서 나옵니다)를 제공합니다.
- 이 값은 여러 "case" 문에 할당된 값과 비교됩니다.
- 값이 해당 case의 값과 같으면 그 case의 내용이 수행됩니다. 그렇지 않으면 다음 case가 평가됩니다.
- 하나의 case가 수행되거나 모든 case가 평가되어 실패하면, 코드는 switch 문의 끝에서 계속 진행됩니다.
- 모든 case가 실패했지만 기본 case가 있다면, 그 기본 case가 수행된 후 코드가 계속 진행됩니다.
사용 가능한 Switch Actions
| 액션 | 설명 |
|---|---|
| Switch | switch 문을 생성합니다. |
| Case | 특정 조건을 확인하는 case를 정의합니다. |
| Default | 모든 case가 실패했을 때 실행되는 default입니다. |
| Back | switch 문을 이전 상태로 되돌립니다. |
| Next | 다음 case를 평가합니다. |
활용 및 응용 예제
예제 1: 점수 기반 행동 결정
var score = 85;
switch (score) {
case 90:
show_message("Excellent!");
break;
case 80:
show_message("Well done!");
break;
case 70:
show_message("Good effort!");
break;
default:
show_message("Keep trying!");
}
예제 2: 플레이어의 직업에 따른 행동
var playerClass = "mage";
switch (playerClass) {
case "warrior":
show_message("You are a warrior with great strength!");
break;
case "mage":
show_message("You are a mage with powerful spells!");
break;
case "rogue":
show_message("You are a rogue with stealth abilities!");
break;
default:
show_message("Unknown class!");
}
예제 3: 요일에 따른 메시지 출력
var day = "Monday";
switch (day) {
case "Monday":
show_message("Start of the week!");
break;
case "Wednesday":
show_message("Midweek already!");
break;
case "Friday":
show_message("Almost the weekend!");
break;
default:
show_message("Just another day!");
}
이와 같은 방식으로 Switch Action 라이브러리를 사용하면, 여러 조건에 따른 코드 작성이 더 간결하고 이해하기 쉬워집니다.