jaynic 1 Posted April 13, 2015 Hey guys, I've got the following script: //FATIGUE MULTIPLIER //JayNic 2015-04-12 //This script is used to change the rate at which fatigue applies to the player. //The changes will only impact the INCREASE in fatigue: meaning that it will NOT // change the rate at which players RECOVER from fatigue. //It is designed to be run on the client - so each time a player OHTC_AdjustFatigue = { player enableFatigue true; _l = 0; //The last fatigue we had while {true} do { if(!(isNil "OHTC_FatigueMultiplier") && typeName OHTC_FatigueMultiplier == "SCALAR" && OHTC_FatigueMultiplier > 0 && OHTC_FatigueMultiplier <= 1) then { //The current fatigue of the player _c = getfatigue player; //The default increase: ie: difference between what we have now, and what we had last time we ran _di = _c - _l; //The NEW increase in fatigue would be the percentage we request of the default increase _ni = _di - (_di * OHTC_FatigueMultiplier); //Assuming we've gone up: let's set the fatigue to the new value; if(_ni > 0) then { player setFatigue _c - _ni; }; systemChat format ["Last: %1, Current: %2, Default Inc: %3, Multiplier: %4, New Inc: %5, Final Fatigue: %6", _l, _c, _di, OHTC_FatigueMultiplier, _ni, getfatigue player]; //Now we set our last historical value to our modified value _l = getfatigue player; }; sleep 1; } }; player addEventHandler ["Respawn",{ _null = [] call OHTC_AdjustFatigue; }]; _null = [] call OHTC_AdjustFatigue; It works fine - but after respawn: the game hangs for a second and then I get the "generic error in expression" error @ line 33. Line 33 being: sleep 1; I'm sure it's some wierd syntax thing I'm doing. Any help would be great. Thanks Share this post Link to post Share on other sites
tpw 2315 Posted April 13, 2015 You get the error because you are trying to use sleep inside a called function: _null = [] call OHTC_AdjustFatigue; If you spawn the function you won't get the error: _null = [] spawn OHTC_AdjustFatigue; Share this post Link to post Share on other sites
jaynic 1 Posted April 13, 2015 Thanks dude. Works like a charm. I just read the documentation on functions in the wiki and followed the sample code. Looks like I need to read some more. Don't quite see the difference between the two - or why there should be a difference... Share this post Link to post Share on other sites
jshock 513 Posted April 13, 2015 Call is unscheduled and in a sense can/will suspend the code at that point until a value is returned. So when in a scheduled environment call will cause an error as you have gotten above. Spawn is scheduled, in the sense that whatever code is within the braces gets injected into the game scheduler and will be executed the next time the "main game loop" iterates, essentially not suspending the code as it doesn't wait for a return value. That's how I understand it, could be a bit of misinformation, but I know when and where to use them on that understanding so.... Share this post Link to post Share on other sites
st_dux 26 Posted April 13, 2015 Call is unscheduled and in a sense can/will suspend the code at that point until a value is returned. So when in a scheduled environment call will cause an error as you have gotten above.Spawn is scheduled, in the sense that whatever code is within the braces gets injected into the game scheduler and will be executed the next time the "main game loop" iterates, essentially not suspending the code as it doesn't wait for a return value. That's how I understand it, could be a bit of misinformation, but I know when and where to use them on that understanding so.... Kind of. You can think of call as introducing new code directly into the current thread. If that thread is scheduled (e.g., if call is used within a script that was executed via execVM or spawn), then the normal rules for a scheduled environment will apply, i.e., you can use sleeps. If that thread is unscheduled, though, then attempting to use sleep or waitUntil within code that is called will produce an error. Using spawn will create an entirely new thread that is always scheduled, so sleeps will never cause an error there. Here's an example to demonstrate what I mean: fn_waitFiveSeconds = { sleep 5; }; [] spawn { player sideChat "A new thread has begun!"; call fn_waitFiveSeconds; player sideChat "Five seconds have passed!"; }; Despite the fact that code containing a sleep is executed using call in the above code, this will never produce an error because call is being invoked in the scheduled environment of a thread that was spawned. Share this post Link to post Share on other sites
jshock 513 Posted April 13, 2015 Better explaination, I was trying to keep it in more basic terms and may have over simplified :p. Share this post Link to post Share on other sites