array_first 함수 설명
array_first
함수는 배열의 첫 번째 요소를 반환하는 함수입니다. 즉, 인덱스 0에 있는 요소를 가져옵니다. 이 함수를 사용할 때는 array_shift
를 사용하여 배열의 첫 번째 요소를 제거할 수도 있습니다.
문법
array_first(array);
인수
인수 | 유형 | 설명 |
---|---|---|
array | Array | 첫 번째 값을 가져올 배열입니다. |
반환값
- 배열이 비어 있지 않으면 배열이 보유할 수 있는 유효한 데이터 유형의 값
- 배열이 비어 있으면
undefined
를 반환합니다.
예제
var _first_added_enemy = array_first(enemies);
with (_first_added_enemy) {
show_debug_message(string(id) + " is the first enemy in the array.");
}
위의 코드는 enemies
배열에 추가된 첫 번째 적 인스턴스를 가져와서 _first_added_enemy
라는 임시 변수에 저장합니다. 이후 인스턴스는 자신의 ID를 포함한 디버그 메시지를 표시합니다.
활용 예제
예제 1: 첫 번째 요소 출력
var first_item = array_first(my_array);
show_debug_message("The first item is: " + string(first_item));
예제 2: 배열에서 첫 번째 적 제거
var first_enemy = array_first(enemies);
array_shift(enemies); // 첫 번째 적 제거
show_debug_message("Removed enemy with ID: " + string(first_enemy.id));
예제 3: 비어 있는 배열 처리
if (array_length(my_array) > 0) {
var first_value = array_first(my_array);
show_debug_message("First value: " + string(first_value));
} else {
show_debug_message("The array is empty.");
}
예제 4: 여러 배열에서 첫 번째 요소 가져오기
var first_enemy = array_first(enemies);
var first_item = array_first(items);
show_debug_message("First enemy ID: " + string(first_enemy.id) + ", First item: " + string(first_item));
이와 같이 array_first
함수를 활용하여 배열의 첫 번째 요소를 쉽게 가져오고, 다양한 상황에서 활용할 수 있습니다.