is_undefined 함수 설명
is_undefined
함수는 주어진 변수가 정의되어 있는지 여부를 반환합니다. 특정 상황에서는 GameMaker에서 변수의 데이터 타입을 확인하고 싶을 때 이 함수를 사용합니다. 이 함수는 값이 정의되어 있는지에 따라 true
또는 false
를 반환합니다. 그러나 이 함수는 변수의 존재 여부를 확인하는 데 사용할 수 없습니다. 대신 variable_instance_exists()
또는 variable_global_exists()
함수를 사용해야 합니다.
문법
is_undefined(n);
인수
인수 | 타입 | 설명 |
---|---|---|
n | Any | 확인할 인수 |
반환값
반환값 | 타입 | 설명 |
---|---|---|
Boolean | true 또는 false | 변수의 정의 여부 |
예제
다음은 is_undefined
함수를 사용하는 예제입니다.
var val = ds_map_find_value(map, 13);
if (is_undefined(val)) {
show_debug_message("Map entry does not exist!");
}
위 코드는 변수 val
이 정의되어 있는지 확인하고, 정의되지 않은 경우 디버그 메시지를 표시합니다.
활용 예제
- 변수 초기화 확인
gml var score; if (is_undefined(score)) { score = 0; // 점수가 정의되지 않았다면 초기화 }
- 배열 요소 확인
gml var myArray = [1, 2, 3]; var element = myArray[5]; // 존재하지 않는 인덱스 접근 if (is_undefined(element)) { show_debug_message("Element does not exist in the array!"); }
- 객체 속성 확인
gml if (is_undefined(myObject.someProperty)) { myObject.someProperty = "default value"; // 속성이 정의되지 않았다면 기본값 설정 }
- 함수 반환값 확인
gml var result = myFunction(); if (is_undefined(result)) { show_debug_message("Function returned an undefined value!"); }
- 데이터 구조 확인
gml var myMap = ds_map_create(); var value = ds_map_find_value(myMap, "key"); if (is_undefined(value)) { show_debug_message("Key does not exist in the map!"); }