struct_exists 함수 설명
struct_exists
함수는 주어진 구조체 내에 특정 변수가 존재하는지를 확인하는 기능을 제공합니다. 이 함수는 구조체 참조와 확인하고자 하는 변수의 이름(문자열 형태)을 인자로 받습니다. 만약 해당 이름을 가진 변수가 구조체에 존재하면 true
를 반환하고, 그렇지 않으면 false
를 반환합니다.
문법
struct_exists(struct, name);
인자 설명
인자 | 타입 | 설명 |
---|---|---|
struct | Struct | 확인할 구조체의 참조 |
name | String | 확인할 구조체 변수의 이름 (문자열 형태) |
반환값
- Boolean: 변수의 존재 여부에 따라
true
또는false
반환
예제
다음 코드는 "shields"라는 변수가 주어진 구조체에 존재하는지를 확인하고, 존재하지 않을 경우 해당 변수를 0으로 초기화합니다.
if !struct_exists(mystruct, "shields") {
mystruct.shields = 0;
}
활용 예제
1. 기본 구조체 생성 및 변수 확인
var mystruct = struct_create();
if !struct_exists(mystruct, "health") {
mystruct.health = 100;
}
2. 여러 변수 초기화
var player = struct_create();
var variables = ["score", "lives", "level"];
for (var i = 0; i < array_length(variables); i++) {
if !struct_exists(player, variables[i]) {
player[variables[i]] = 0;
}
}
3. 동적 변수 추가
var gameData = struct_create();
var newVariable = "highscore";
if !struct_exists(gameData, newVariable) {
gameData[newVariable] = 0;
}
4. 조건에 따른 변수 업데이트
var settings = struct_create();
if !struct_exists(settings, "volume") {
settings.volume = 50; // 기본 볼륨 설정
} else {
settings.volume += 10; // 기존 볼륨 증가
}
5. 구조체 내 변수 삭제 및 확인
var config = struct_create();
config.debugMode = true;
if struct_exists(config, "debugMode") {
struct_delete(config, "debugMode");
}
이와 같이 struct_exists
함수를 활용하면 구조체 내 변수의 존재 여부를 쉽게 확인하고, 필요한 경우 변수를 초기화하거나 수정하는 등의 작업을 할 수 있습니다.