loadConfig: Difference between revisions
Jump to navigation
Jump to search
m (modify example) |
(Fix example to be lowercase) |
||
Line 23: | Line 23: | ||
params ["_path"]; | params ["_path"]; | ||
//initialize cache if not initialized yet | //initialize cache if not initialized yet | ||
if (isNil " | if (isNil "TAG_configCache") then { | ||
TAG_configCache = createHashMap; | |||
}; | }; | ||
private _cfg = | private _cfg = TAG_configCache getOrDefault [_path, configNull]; | ||
//if path doesn't exist in the cache or cfg is null, load the config | //if path doesn't exist in the cache or cfg is null, load the config | ||
if (isNull _cfg) then { | if (isNull _cfg) then { | ||
_cfg = loadConfig _path; | _cfg = loadConfig _path; | ||
TAG_configCache set [_path, _cfg]; | |||
}; | }; | ||
_cfg; | _cfg; | ||
Line 37: | Line 37: | ||
["a3\data_f\default_super.rvmat"] call _fnc_loadConfig;</sqf> | ["a3\data_f\default_super.rvmat"] call _fnc_loadConfig;</sqf> | ||
|x3=<sqf>// | |x3=<sqf>//Converting a config into hashmap | ||
_fnc_convertClass = { | _fnc_convertClass = { | ||
params ["_cfgClass"]; | params ["_cfgClass"]; | ||
private _result = createHashMap; | private _result = createHashMap; | ||
private _props = configProperties [_cfgClass, "true", true]; | private _props = configProperties [_cfgClass, "true", true]; | ||
//Note: Hashmaps are case-sensitive. So configName cases have to be consistent (e.g. all lowercase) | |||
{ | { | ||
if (isNumber _x) then {_result set [configName _x, getNumber _x]; continue; }; | if (isNumber _x) then {_result set [toLowerANSI configName _x, getNumber _x]; continue; }; | ||
if (isText _x) then {_result set [configName _x, getText _x]; continue; }; | if (isText _x) then {_result set [toLowerANSI configName _x, getText _x]; continue; }; | ||
if (isArray _x) then {_result set [configName _x, getArray _x]; continue; }; | if (isArray _x) then {_result set [toLowerANSI configName _x, getArray _x]; continue; }; | ||
} forEach _props; | } forEach _props; | ||
private _classes = "true" configClasses _cfgClass; | private _classes = "true" configClasses _cfgClass; | ||
{ | { | ||
_result set [configName _x, _x call _fnc_convertClass]; | _result set [toLowerANSI configName _x, _x call _fnc_convertClass]; | ||
} forEach _classes; | } forEach _classes; | ||
_result | _result | ||
Line 57: | Line 58: | ||
private _cfg = loadConfig "mission.sqm"; | private _cfg = loadConfig "mission.sqm"; | ||
private _cfgMap = _cfg call _fnc_convertClass; | private _cfgMap = _cfg call _fnc_convertClass; | ||
//The following expression is similar to getNumber(_cfg >> "EditorData" >> "moveGridStep") | |||
//Notice that all strings are lowercase (which is how they were stored in hashmap) | |||
_cfgMap get "editordata" get "movegridstep"; </sqf> | |||
|seealso= [[config greater greater name|>>]] [[configFile]] [[configClasses]] [[isText]] [[isNumber]] [[isArray]] [[isClass]] [[configName]] [[getText]] [[getNumber]] [[getArray]] | |seealso= [[config greater greater name|>>]] [[configFile]] [[configClasses]] [[isText]] [[isNumber]] [[isArray]] [[isClass]] [[configName]] [[getText]] [[getNumber]] [[getArray]] | ||
}} | }} |
Revision as of 10:13, 17 March 2022
Description
- Description:
- Loads the given file as a Config. It can load .rvmat, .bisurf, .cpp, .bin, .sqm, and description.ext files (both binarized and unbinarized configs are supported).
- Groups:
- Config
Syntax
- Syntax:
- loadConfig path
- Parameters:
- path: String - Path to the file, which should be in a valid config format.
- Return Value:
- Config - Loaded config. Returns configNull if the file doesn't exist.
Examples
- Example 1:
- Example 2:
- //Using a cache to store loaded configs, since loading configs is very slow and should be done once. _fnc_loadConfig = { params ["_path"]; //initialize cache if not initialized yet if (isNil "TAG_configCache") then { TAG_configCache = createHashMap; }; private _cfg = TAG_configCache getOrDefault [_path, configNull]; //if path doesn't exist in the cache or cfg is null, load the config if (isNull _cfg) then { _cfg = loadConfig _path; TAG_configCache set [_path, _cfg]; }; _cfg; }; //the first call will be slow (~0.1200 ms), but subsequent calls are fast (~0.0050 ms) ["a3\data_f\default_super.rvmat"] call _fnc_loadConfig;
- Example 3:
- //Converting a config into hashmap _fnc_convertClass = { params ["_cfgClass"]; private _result = createHashMap; private _props = configProperties [_cfgClass, "true", true]; //Note: Hashmaps are case-sensitive. So configName cases have to be consistent (e.g. all lowercase) { if (isNumber _x) then {_result set [toLowerANSI configName _x, getNumber _x]; continue; }; if (isText _x) then {_result set [toLowerANSI configName _x, getText _x]; continue; }; if (isArray _x) then {_result set [toLowerANSI configName _x, getArray _x]; continue; }; } forEach _props; private _classes = "true" configClasses _cfgClass; { _result set [toLowerANSI configName _x, _x call _fnc_convertClass]; } forEach _classes; _result }; private _cfg = loadConfig "mission.sqm"; private _cfgMap = _cfg call _fnc_convertClass; //The following expression is similar to getNumber(_cfg >> "EditorData" >> "moveGridStep") //Notice that all strings are lowercase (which is how they were stored in hashmap) _cfgMap get "editordata" get "movegridstep";
Additional Information
- See also:
- >> configFile configClasses isText isNumber isArray isClass configName getText getNumber getArray
Notes
-
Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note