aimgame 5 Posted April 19, 2017 Hi all! I have this simple script of fuel consumption working in EDEN editor pretty well: _veh = vehicle player; _rate = 0.003 ; _loadMultiplier = 0.003 ; _tooSlowRate = 0.004 ; _tooFastRate = 0.008 ; _speedMult = 0; while {true} do { if ((alive _veh) and (_veh != player) and (isengineon _veh) and ((fuel _veh) > 0)) then { _crew = count( crew _veh); _load = _crew * _loadMultiplier; _speed = speed _veh; if(_speed < 30 ) then { _speedMult = _tooSlowRate; }; if(_speed > 90 ) then { _speedMult = _tooFastRate; }; _realLoad = _rate + _load + _speedMult; _veh setFuel ( (fuel _veh) - _realLoad); }; sleep 5; }; But it's not work on dedi server(( Can anyone help with this? P.S. Server mod: AltisLife 4.4r4, if it mean something... Tryed both - server and client sides... Share this post Link to post Share on other sites
BlacKnightBK 47 Posted April 19, 2017 52 minutes ago, aimgame said: Hi all! I have this simple script of fuel consumption working in EDEN editor pretty well: _veh = vehicle player; _rate = 0.003 ; _loadMultiplier = 0.003 ; _tooSlowRate = 0.004 ; _tooFastRate = 0.008 ; _speedMult = 0; while {true} do { if ((alive _veh) and (_veh != player) and (isengineon _veh) and ((fuel _veh) > 0)) then { _crew = count( crew _veh); _load = _crew * _loadMultiplier; _speed = speed _veh; if(_speed < 30 ) then { _speedMult = _tooSlowRate; }; if(_speed > 90 ) then { _speedMult = _tooFastRate; }; _realLoad = _rate + _load + _speedMult; _veh setFuel ( (fuel _veh) - _realLoad); }; sleep 5; }; But it's not work on dedi server(( Can anyone help with this? P.S. Server mod: AltisLife 4.4r4, if it mean something... Tryed both - server and client sides... If it works in eden then there is no issue with it. the issue is with how are you trying to use it in the dedicated server Cheers Share this post Link to post Share on other sites
aimgame 5 Posted April 19, 2017 BlacKnightBK, well can you tell how to use it properly in the server? Share this post Link to post Share on other sites
BlacKnightBK 47 Posted April 19, 2017 turn it into a function and call it at the points when a vehicle is spawned. Since it is a roleplay server it gets called when vehicle is bought or taken out of garage AIMGAME_fnc_fuelConsumption = { params ["_veh"]; _rate = 0.003 ; _loadMultiplier = 0.003 ; _tooSlowRate = 0.004 ; _tooFastRate = 0.008 ; _speedMult = 0; while {true} do { if ((alive _veh) and (_veh != player) and (isengineon _veh) and ((fuel _veh) > 0)) then { _crew = count( crew _veh); _load = _crew * _loadMultiplier; _speed = speed _veh; if(_speed < 30 ) then { _speedMult = _tooSlowRate; }; if(_speed > 90 ) then { _speedMult = _tooFastRate; }; _realLoad = _rate + _load + _speedMult; _veh setFuel ( (fuel _veh) - _realLoad); }; sleep 5; }; }; Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 19, 2017 26 minutes ago, BlacKnightBK said: turn it into a function and call it at the points when a vehicle is spawned. Since it is a roleplay server it gets called when vehicle is bought or taken out of garage AIMGAME_fnc_fuelConsumption = { params ["_veh"]; _rate = 0.003 ; _loadMultiplier = 0.003 ; _tooSlowRate = 0.004 ; _tooFastRate = 0.008 ; _speedMult = 0; while {true} do { if ((alive _veh) and (_veh != player) and (isengineon _veh) and ((fuel _veh) > 0)) then { _crew = count( crew _veh); _load = _crew * _loadMultiplier; _speed = speed _veh; if(_speed < 30 ) then { _speedMult = _tooSlowRate; }; if(_speed > 90 ) then { _speedMult = _tooFastRate; }; _realLoad = _rate + _load + _speedMult; _veh setFuel ( (fuel _veh) - _realLoad); }; sleep 5; }; }; Sorry BlacKnightBK but i think it is not such easy. The problem is setFuel which needs to be executed on the machine where a vehicle is local. But the locality of a vehicle changes if someone enters it. What is neded here is remote execution of setFuel. [_veh, ((fuel _veh) -_realLoad)] remoteExec ["setFuel", _veh]; Share this post Link to post Share on other sites
aimgame 5 Posted April 19, 2017 6 minutes ago, sarogahtyp said: Sorry BlacKnightBK but i think it is not such easy. The problem ist setFuel which needs to be executed on the machine where a vehicle is local. But the locality of a vehicle changes if someone enters it. Yes yes, that is the problem i've faced here... and i just cant figure out how to execute this script on every vehicle that players use... i mean veh is created at server side when it pulled out from garage, then player enters in and this veh becomes player's? or what? And where should i exec the script, if setFuel is only for local vehs? Sorry, im dumb)) Share this post Link to post Share on other sites
BlacKnightBK 47 Posted April 19, 2017 Just now, aimgame said: Yes yes, that is the problem i've faced here... and i just cant figure out how to execute this script on every vehicle that players use... i mean veh is created at server side when it pulled out from garage, then player enters in and this veh becomes player's? or what? And where should i exec the script, if setFuel is only for local vehs? Sorry, im dumb)) The reason I am about to say this is because I have never had to deal with locality issues (haven't reached that stage yet). Since I see it is an issue for you as well to me there is a workaround. Keep the function server side, however, add a wait until command that detects when a player is inside the vehicle. And if the player leaves the vehicle then the loop will repeat anyway and will wait until it gets occupied again.S Cheers Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 19, 2017 (edited) I think u should execute that script server side for every vehicle (any conditions with player make no sense in that case). Wit the above given remoteExec line you can execute the setFuel command on the machine which owns the vehicle. EDIT: yeah, nearly the same as BlacKnightBK told above Edited April 19, 2017 by sarogahtyp Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 19, 2017 (edited) AIMGAME_fnc_fuelConsumption = { _rate = 0.003 ; _loadMultiplier = 0.003 ; _tooSlowRate = 0.004 ; _tooFastRate = 0.008 ; while {true} do { { _speedMult = 0; _crew = count (crew _x); _load = _crew * _loadMultiplier; _speed = speed _x; if(_speed < 30 ) then { _speedMult = _tooSlowRate; }; if(_speed > 90 ) then { _speedMult = _tooFastRate; }; _realLoad = _rate + _load + _speedMult; [_x, ((fuel _x) -_realLoad)] remoteExec ["setFuel", _x]; true } count (vehicles select {(alive _x) && (fuel _x > 0) && (count crew _x > 0) && (isEngineOn _x)}); sleep 5; }; }; }; try it and give feedback! Edited April 19, 2017 by sarogahtyp moved _speedMult Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 19, 2017 _speedMult has to be right before first if statement. I ll fix it when back on desktop Gesendet von meinem SUNNY mit Tapatalk EDIT: Updated script Share this post Link to post Share on other sites
Devastator_cm 434 Posted April 19, 2017 KillzoneKid has a better solution for setfuel. Check his page for it. http://killzonekid.com/arma-scripting-tutorials-locality/ If you do simple remote exec you can kill the network if you have many people connected to your server and many vehicles which runs such script Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 19, 2017 I m not sure that KKs method is better. But I agree that it could cause a network traffic peak if there are many cars with fullfilled conditions to do setFuel. Therefore I added an algorithm to prevent network lags by spreading the network requests over a time period of 3-4 seconds. These time is an additional delay for the script and has to be in mind by op if he uses the script. AIMGAME_fnc_fuelConsumption = { _lag_delay = 3; //time period for executing network traffic - thought to prevent traffic peaks _rate = 0.003 ; _loadMultiplier = 0.003 ; _tooSlowRate = 0.004 ; _tooFastRate = 0.008 ; _vec_array = []; while {true} do { _vec_array = vehicles select {(alive _x) && (fuel _x > 0) && (count crew _x > 0) && (isEngineOn _x)}; _vec_num = count _vec_array; if (_vec_num > 0) then { _delay = (_lag_delay + random 1) / _vec_num; { _speedMult = 0; _crew = count (crew _x); _load = _crew * _loadMultiplier; _speed = speed _x; if(_speed < 30 ) then { _speedMult = _tooSlowRate; }; if(_speed > 90 ) then { _speedMult = _tooFastRate; }; _realLoad = _rate + _load + _speedMult; [_x, ((fuel _x) -_realLoad)] remoteExec ["setFuel", _x]; sleep _delay; true } count (vehicles select {(alive _x) && (fuel _x > 0) && (count crew _x > 0) && (isEngineOn _x)}); sleep 5; }; //_vec_num end }; // while end }; // fnc end Share this post Link to post Share on other sites
Lucullus 71 Posted April 19, 2017 I would add an EventHandler ("GetInMan") to player and run the fnc local on Clients. In fnc ask for example: if !(local _vehicle) exitWith {}; edit: In initPlayerLocal.sqf: player addEventHandler ["GetInMan",{_this spawn AIMGAME_fnc_fuelConsumption}]; function: AIMGAME_fnc_fuelConsumption = { params ["","","_vehicle",""]; if !(local _vehicle) exitWith {}; ... your code here ... }; Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 19, 2017 I would add an EventHandler ("GetInMan") to player and run the fnc local on Clients. In fnc ask for example:if !(local _vehicle) exitWith {}; edit: In initPlayerLocal.sqf:player addEventHandler ["GetInMan",{_this spawn AIMGAME_fnc_fuelConsumption}]; function:AIMGAME_fnc_fuelConsumption = { params ["","","_vehicle",""];if !(local _vehicle) exitWith {};...your code here...}; This is a good idea but not as easy as u describe it.One has to care about stopping the loop if it's locality changes and the other thing is if it is ensured if the locality has already changed when the eh fires ...And what happens if the locality changes because the crew changes the seats without leaving or entering the car?sent from mobile using Tapatalk Share this post Link to post Share on other sites
Lucullus 71 Posted April 19, 2017 Ask for (local _vehicle) in the loop, and there is an EventHandler "SeatSwitchedMan". Edit: OK, another try. In initPlayerLocal.sqf: player addEventHandler ["GetInMan",{_this spawn Luc_fnc_VehicleFuel}]; player addEventHandler ["SeatSwitchedMan",{_this spawn Luc_fnc_VehicleFuel}]; Function: Luc_fnc_VehicleFuel = { params ["","","_vehicle",""]; if !(isNil {_vehicle getVariable "Luc_fuel"}) exitWith {}; _vehicle setVariable ["Luc_fuel", 0]; _type = typeOf _vehicle; _mass = getMass _vehicle; _fuelCapacity = getNumber(configFile >> "CfgVehicles" >> _type >> "fuelCapacity"); _maxSpeed = getNumber(configFile >> "CfgVehicles" >> _type >> "maxSpeed"); _enginePower = getNumber(configFile >> "CfgVehicles" >> _type >> "enginePower"); private _correction = 0.002; if (_vehicle isKindOf "Car_F") then {_correction = 0.0035}; if (_vehicle isKindOf "Wheeled_APC_F") then {_correction = 0.0015}; if (_vehicle isKindOf "Tank_F") then {_correction = 0.0065}; waitUntil {sleep 0.5; isEngineOn _vehicle}; while {(alive _vehicle) and (local _vehicle)} do { if (isEngineOn _vehicle) then { _tank_content = 1 / _fuelCapacity; _consumption = ((_mass/_enginePower)*(speed _vehicle/_maxSpeed) * _tank_content * _correction) max 0.0001; _vehicle setFuel ((fuel _vehicle) - _consumption); }; sleep 1; systemChat format ["%1 - %2",_vehicle,fuel _vehicle]; //only for Test }; _vehicle setVariable ["Luc_fuel", nil]; }; tested only local! Edit2: "Wheeled_APC_F" only 45 liters??? 1 Share this post Link to post Share on other sites
aimgame 5 Posted April 19, 2017 Thank you all! Now i got this one working well on dedi server: KK_fnc_setFuel = { private ["_veh","_fuel"]; _veh = _this select 0; _fuel = _this select 1; if (local _veh) then { _veh setFuel _fuel; } else { PVsetFuel = _this; publicVariable "PVsetFuel"; }; }; "PVsetFuel" addPublicVariableEventHandler { _veh = _this select 1 select 0; _fuel = _this select 1 select 1; if (local _veh) then { _veh setFuel _fuel; }; }; /////////////////////////////////////////////// _rate = 0.001 ; _loadMultiplier = 0.001 ; _tooStopRate = 0.001 ; _tooSlowRate = 0.003 ; _tooFastRate = 0.005 ; _speedMult = 0; while {true} do { _veh = vehicle player; if (_veh != player) then { if ((alive _veh) and (count (crew _veh) > 0) and (isengineon _veh) and ((fuel _veh) > 0)) then { _crew = count( crew _veh); _load = _crew * _loadMultiplier; _speed = speed _veh; _fuel = fuel _veh; if (_veh isKindOf "Air") then { _speedMult = _tooSlowRate; } else { if(_speed == 0 ) then { _speedMult = _tooStopRate; }; if(_speed < 60 ) then { _speedMult = _tooSlowRate; }; if(_speed > 60 ) then { _speedMult = _tooFastRate; }; }; _realLoad = _rate + _load + _speedMult; _fuelc = (fuel _veh) - _realLoad; [vehicles select 0, _fuelc] call KK_fnc_setFuel; }; sleep 10; }; }; Have some small fps lags there... but still not sure about the reason... I'll do some more checks with and without this script, and give a feedback. Any way im realy glad its even working now! 1 1 Share this post Link to post Share on other sites
sarogahtyp 1108 Posted April 20, 2017 After reading this:https://community.bistudio.com/wiki/Locality_in_MultiplayerI think u should do a check if player is driver of the vehicle to reduce network traffic.And u can delete the fuel check because if engine is on then there is fuel in the vehicle as wellsent from mobile using Tapatalk Share this post Link to post Share on other sites
simicsko 4 Posted April 19, 2022 On 4/19/2017 at 10:45 PM, aimgame said: Thank you all! Now i got this one working well on dedi server: KK_fnc_setFuel = { private ["_veh","_fuel"]; _veh = _this select 0; _fuel = _this select 1; if (local _veh) then { _veh setFuel _fuel; } else { PVsetFuel = _this; publicVariable "PVsetFuel"; }; }; "PVsetFuel" addPublicVariableEventHandler { _veh = _this select 1 select 0; _fuel = _this select 1 select 1; if (local _veh) then { _veh setFuel _fuel; }; }; /////////////////////////////////////////////// _rate = 0.001 ; _loadMultiplier = 0.001 ; _tooStopRate = 0.001 ; _tooSlowRate = 0.003 ; _tooFastRate = 0.005 ; _speedMult = 0; while {true} do { _veh = vehicle player; if (_veh != player) then { if ((alive _veh) and (count (crew _veh) > 0) and (isengineon _veh) and ((fuel _veh) > 0)) then { _crew = count( crew _veh); _load = _crew * _loadMultiplier; _speed = speed _veh; _fuel = fuel _veh; if (_veh isKindOf "Air") then { _speedMult = _tooSlowRate; } else { if(_speed == 0 ) then { _speedMult = _tooStopRate; }; if(_speed < 60 ) then { _speedMult = _tooSlowRate; }; if(_speed > 60 ) then { _speedMult = _tooFastRate; }; }; _realLoad = _rate + _load + _speedMult; _fuelc = (fuel _veh) - _realLoad; [vehicles select 0, _fuelc] call KK_fnc_setFuel; }; sleep 10; }; }; Have some small fps lags there... but still not sure about the reason... I'll do some more checks with and without this script, and give a feedback. Any way im realy glad its even working now! Hello. Where do you install this script? What do you call it? Share this post Link to post Share on other sites
rowdied 44 Posted April 21, 2022 On 4/19/2022 at 3:18 AM, simicsko said: Hello. Where do you install this script? What do you call it? In the mission folder. What I did is copy the info from above and pasted it into a text file and renamed it fuel.sqf (you can name it anything you want) Make sure you rename the file form .txt to .sqf and then place it into a folder inside the mission folder called scripts (the folder can be named anything you want) Next you have to reference it in the init.sqf which is also in the mission folder. If not you will have to create it like you did the fuel one. Next place this code in the init.sqf; [] execVM "scripts\fuel.sqf"; and thats it it should now work in SP or MP and even on dedicated servers. Make sense? If not pm me Share this post Link to post Share on other sites
simicsko 4 Posted August 27, 2022 On 4/21/2022 at 5:57 AM, rowdied said: In the mission folder. What I did is copy the info from above and pasted it into a text file and renamed it fuel.sqf (you can name it anything you want) Make sure you rename the file form .txt to .sqf and then place it into a folder inside the mission folder called scripts (the folder can be named anything you want) Next you have to reference it in the init.sqf which is also in the mission folder. If not you will have to create it like you did the fuel one. Next place this code in the init.sqf; [] execVM "scripts\fuel.sqf"; and thats it it should now work in SP or MP and even on dedicated servers. Make sense? If not pm me It works, thank you very much. 😉 Share this post Link to post Share on other sites