Jump to content
Sign in to follow this  
Stubbinz

How to subtract variables given to units?

Recommended Posts

I have my guy with setvariable ["hunger",90]

i have a script thats calls this variable (so i can see how hungry my guy is via hint. That works, however my hunger does not increase, nor decrease with anything im doing. Telling me that the variable is staying constant. How does one work around this?

my hunger script.

#hungerscript
_target = _this select 0;
_caller = _this select 1;
_id = _this select 2;
~0.125
_hunger = _caller getvariable "hunger";
~0.125
if (_hunger < 100)THEN {_hunger = _hunger+1} ELSE {player setdamage 1};
~0.125
if (_hunger >= 90)THEN {hint "You are Starving!} ELSE {};
~5
_caller setvariable ["hunger",_hunger];
goto "hungerscript";
Exit;

Share this post


Link to post
Share on other sites

Lol I'm surprised people still use SQS.

Let me suggest an SQF alternative.

_target = _this select 0;
_caller = _this select 1;
_id = _this select 2;

while{alive player}do{
  _hunger = _caller getVaraible "Hunger";
  if(_hunger < 100)then{_hunger = _hunger + 1}else{player setDamage 1};
  if(_hunger >= 90)then{hint "You are starving!"};
  _caller setVariable ["Hunger",_hunger];
  sleep 5;
}

Having cleaned it up now it will probably work. If not come back and we'll try to figure it out.

Share this post


Link to post
Share on other sites

Thank you, and yeah, im old school. ha.

I don't really know the difference between sqf and sqs very much to be effective with it.

---------- Post added at 11:22 PM ---------- Previous post was at 11:17 PM ----------

And its still not working. ha

Share this post


Link to post
Share on other sites

one of the major differences of sqs and sqf is the way they are executed:

_null = [something] exec "scriptname.sqs";

_null = [something] execVM "scriptname.sqf";

also there is a misspelling in the script Big Dawg KS posted, should be:

 _hunger = _caller [b]getVariable [/b]"Hunger"; 

also im not sure if it matters, but here Hunger is spelled with capital H, not h as you had, maybe its important.

but yeah, i highly recomend you switch to using sqf, the changes are not that big once you start converting.

Share this post


Link to post
Share on other sites

Still is not working.

Funny thing is i have a money variable attached to the unit, and a test script that subtracts its value which seems to be working. Yet, i'm not registering any increase to my hunger value at all as that is supposed to be perpetual in nature. Example, gain +1 hunger every minute or whatever time frame i wanted (going for a minute at the moment). When hunger reaches 100, unit dies. I need it to be compatible for multiplayer, so i cant just use player variable i believe. Its actually really annoying.

Share this post


Link to post
Share on other sites

Thanks weirdo for the information, however it doesn't really address the problem i'm having trying to fix my hunger issue ha

Share this post


Link to post
Share on other sites

Add -showScriptErrors in your shortcut parameters to see the errors.

The sqf script had typos, and should probably work when they're fixed:

_target = _this select 0;
_caller = _this select 1;
_id = _this select 2;

while {alive player} do {
_hunger = _caller getVariable "Hunger";
if (_hunger < 100) then {_hunger = _hunger + 1} else {player setDamage 1};
if (_hunger >= 90) then {hint "You are starving!"};
_caller setVariable ["Hunger",_hunger];
sleep 5;
};

Share this post


Link to post
Share on other sites

maybe the issue is that you try to getVariable the "Hunger" variable to fast, before it is already set, causing script to stall or bug/error?

there is the option of using getVariable with a default return value.

try having before the while loop:

waitUntil {(player getVariable ["Hunger", 666]) != 666};

Edit: above is ofc asuming you have set the variable in init.sqf or somewhere else like a initline or similar, else your issue is that you try to collect a variable wich does not exist, and you shoudl just setVariable to 0 before the while loop.

FYI: there was a missing ; after the last } in Big Dawg KS´s sqf script.

for sqf its important to have these, just incase you did not straight copy celerys script wich had this fixed.

these small typos have caused myself alot of headaches in the past, squint is a nice tool to check for errors as well btw.

Edited by Demonized

Share this post


Link to post
Share on other sites

Lol sorry for the typos it's been a while since I wrote SQF. Demonized has a good point I overlooked, the variable may not be initialized.

Revised (and corrected) script:

_target = _this select 0;
_caller = _this select 1;
_id = _this select 2;

while {alive player} do {
_hunger = _caller getVariable ["Hunger",0]; // < give hunger a default value of 0
if (_hunger < 100) then {_hunger = _hunger + 1} else {player setDamage 1};
if (_hunger >= 90) then {hint "You are starving!"};
_caller setVariable ["Hunger",_hunger];
sleep 5;
};

---------- Post added at 11:02 AM ---------- Previous post was at 10:59 AM ----------

also im not sure if it matters, but here Hunger is spelled with capital H, not h as you had, maybe its important.

I'm pretty sure that "hunger" and "Hunger" are the same variable. Most variables are case insensitive in the game.

Share this post


Link to post
Share on other sites

I got it. Thank you guys for the support. :D I've just been su used to using sqs since flashpoint days, so sqf isnt my strong point, but that's why i'm on these forums to begin with. thank you.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×