
Vous voulez optimisez vos scripts ? C'est ici !
Ce tutoriel n'est pas complet et je ne vais qu'aborder les "angles" de la grande machine qu'est l'optimisation.
Pour commencer, il faut savoir que bien optimiser un serveur cela commence par utilisé correctement le SQF pour ne pas faire des choses inutiles et ou avec des erreurs.
Voici une exemple :
Code:
if (_vendor in [trait_coco,trait_mari,trait_hero,trait_heroincoupe,trait_heroinpur]) then {
_hasLicense = true;
} else {
if (_type in ["ura_1","ura_2","ura_3","ura_4"]) then {
_hasLicense = LICENSE_VALUE("Uranium","civ");
} else {
if (_type in ["hen_bis","rooster_bis",""]) then {
_hasLicense = LICENSE_VALUE("acm","civ");
} else {
if (_type isEqualTo "biere_duriff") then {
_hasLicense = LICENSE_VALUE("duriff","civ");
} else {
_hasLicense = LICENSE_VALUE(_type,"civ");
};
};
};
};
Le code du dessus sera plus lents que celui d'en bas :
Code:
switch (_type) do {
case "ura_1": { _hasLicense = LICENSE_VALUE("Uranium","civ"); };
case "ura_2": { _hasLicense = LICENSE_VALUE("Uranium","civ"); };
case "ura_3": { _hasLicense = LICENSE_VALUE("Uranium","civ"); };
case "ura_4": { _hasLicense = LICENSE_VALUE("Uranium","civ"); };
case "hen_bis": { _hasLicense = LICENSE_VALUE("acm","civ"); };
case "rooster_bis": { _hasLicense = LICENSE_VALUE("acm","civ"); };
case "sheep_bis": { _hasLicense = LICENSE_VALUE("acm","civ"); };
case "goat_bis": { _hasLicense = LICENSE_VALUE("acm","civ"); };
case "gazole": { _hasLicense = LICENSE_VALUE("fuel","civ"); };
case "sp95": { _hasLicense = LICENSE_VALUE("fuel","civ"); };
case "sp98": { _hasLicense = LICENSE_VALUE("fuel","civ"); };
case "gpl": { _hasLicense = LICENSE_VALUE("fuel","civ"); };
case "bio": { _hasLicense = LICENSE_VALUE("fuel","civ"); };
case "kerosene": { _hasLicense = LICENSE_VALUE("fuel","civ"); };
case "marijuana": { _hasLicense = true; };
case default { _hasLicense = LICENSE_VALUE(_type,"civ"); };
[CENTER]};
Il faut aussi savoir que d'avoir trop de dossier différents dans un fichier mission peut ralentir les recherches, prenons l'exemples des serveurs KOTH qui tiennent tout leur codes dans le fichier ROOT et chacun de c'est fichier tiennent sur une lignes.
Il existe pour ce faire des sites qu'on appelle des converter sqf qui permettent de convertir un code en voici un : https://sqf.fini.dev/convert
Le site à plusieurs options mais celle qui nous intéresse c'est "Minifier" qui permet de faire tenir ce code :
[/CENTER]
Code:
#include "..\..\script_macros.hpp"
#define ctrlSelData(ctrl) (lbData[##ctrl,(lbCurSel ##ctrl)])
/*
File: fn_vehTakeItem.sqf
Author: Bryan "Tonic" Boardwine
Description:
Used in the vehicle trunk menu, takes the selected item and puts it in the players virtual inventory
if the player has room.
*/
private ["_ctrl","_num","_index","_data","_old","_value","_weight","_diff"];
disableSerialization;
if (isNull life_trunk_vehicle || !alive life_trunk_vehicle) exitWith {hint localize "STR_MISC_VehDoesntExist"};
if (!alive player) exitWith {closeDialog 0;};
if ((life_trunk_vehicle getVariable ["trunk_in_use_by",player]) != player) exitWith { closeDialog 0; hint localize "STR_MISC_VehInvUse"; };
if ((lbCurSel 3502) isEqualTo -1) exitWith {hint localize "STR_Global_NoSelection";};
_ctrl = ctrlSelData(3502);
_num = ctrlText 3505;
if (!([_num] call TON_fnc_isnumber)) exitWith {hint localize "STR_MISC_WrongNumFormat";};
_num = parseNumber(_num);
if (_num < 1) exitWith {hint localize "STR_MISC_Under1";};
_index = [_ctrl,((life_trunk_vehicle getVariable "Trunk") select 0)] call TON_fnc_index;
_data = (life_trunk_vehicle getVariable "Trunk") select 0;
_old = life_trunk_vehicle getVariable "Trunk";
if (_index isEqualTo -1) exitWith {};
_value = _data select _index select 1;
if (_num > _value) exitWith {hint localize "STR_MISC_NotEnough"};
_num = [_ctrl,_num,life_carryWeight,life_maxWeight] call life_fnc_calWeightDiff;
if (_num isEqualTo 0) exitWith {hint localize "STR_NOTF_InvFull"};
_weight = ([_ctrl] call life_fnc_itemWeight) * _num;
if (_ctrl == "money") then {
if (_num == _value) then {
_data deleteAt _index;
} else {
_data set[_index,[_ctrl,(_value - _num)]];
};
CASH = CASH + _num;
[0] call SOCK_fnc_updatePartial;
life_trunk_vehicle setVariable ["Trunk",[_data,(_old select 1) - _weight],true];
[life_trunk_vehicle] call life_fnc_vehInventory;
} else {
if ([true,_ctrl,_num] call life_fnc_handleInv) then {
if (_num == _value) then {
_data deleteAt _index;
} else {
_data set[_index,[_ctrl,(_value - _num)]];
};
life_trunk_vehicle setVariable ["Trunk",[_data,(_old select 1) - _weight],true];
[life_trunk_vehicle] call life_fnc_vehInventory;
} else {
hint localize "STR_NOTF_InvFull";
};
};
[CENTER]
Sur ça :
Code:
#include "..\..\script_macros.hpp"#define ctrlSelData(ctrl)(lbData[##ctrl,(lbCurSel ##ctrl)])private["_ctrl","_num","_index","_data","_old","_value","_weight","_diff"];disableSerialization;if(isNull life_trunk_vehicle || !alive life_trunk_vehicle)exitWith{hint localize "STR_MISC_VehDoesntExist"};if(!alive player)exitWith{closeDialog 0;};if((life_trunk_vehicle getVariable["trunk_in_use_by",player])!=player)exitWith{closeDialog 0;hint localize "STR_MISC_VehInvUse";};if((lbCurSel 3502)isEqualTo-1)exitWith{hint localize "STR_Global_NoSelection";};_ctrl=ctrlSelData(3502);_num=ctrlText 3505;if(!([_num]call TON_fnc_isnumber))exitWith{hint localize "STR_MISC_WrongNumFormat";};_num=parseNumber(_num);if(_num<1)exitWith{hint localize "STR_MISC_Under1";};_index=[_ctrl,((life_trunk_vehicle getVariable "Trunk")select 0)]call TON_fnc_index;_data=(life_trunk_vehicle getVariable "Trunk")select 0;_old=life_trunk_vehicle getVariable "Trunk";if(_index isEqualTo-1)exitWith{};_value=_data select _index select 1;if(_num>_value)exitWith{hint localize "STR_MISC_NotEnough"};_num=[_ctrl,_num,life_carryWeight,life_maxWeight]call life_fnc_calWeightDiff;if(_num isEqualTo 0)exitWith{hint localize "STR_NOTF_InvFull"};_weight=([_ctrl]call life_fnc_itemWeight)*_num;if(_ctrl=="money")then{if(_num==_value)then{_data deleteAt _index;}else{_data set[_index,[_ctrl,(_value-_num)]];};CASH=CASH+_num;[0]call SOCK_fnc_updatePartial;life_trunk_vehicle setVariable["Trunk",[_data,(_old select 1)-_weight],true];[life_trunk_vehicle]call life_fnc_vehInventory;}else{if([true,_ctrl,_num]call life_fnc_handleInv)then{if(_num==_value)then{_data deleteAt _index;}else{_data set[_index,[_ctrl,(_value-_num)]];};life_trunk_vehicle setVariable["Trunk",[_data,(_old select 1)-_weight],true];[life_trunk_vehicle]call life_fnc_vehInventory;}else{hint localize "STR_NOTF_InvFull";};};
Vous comprenez donc que moins c'est lisibles mieux ça l'est pour la machine.. (Ah... la différence entre le rebot et l'homme !) ce minifier supprime toutes les informations illisibles pour la machine rendant le code plus long et plus lourd, la machine le lit donc plus rapidement.
Ce n'est qu'un petit dans l'optimisation des scripts, il existe aussi l'optimisation de la mission mais je n'ai pas encore attaquer ce point pour en savoir plus !
Zaros[/CENTER]