Jump to content
Sign in to follow this  
Adesso

The simple syntax for variable testing

Recommended Posts

Greetings.

I am breaking my head here, and have tried a million variations, but can't seem to get simple variable addition and testing to work. Can someone help me please.

The Idea.

A soldier is unarmed, and gets a task to move to a location. Working 100%

A Trigger on the way assigns a gun to the soldier and new task. Working 100%

The new task is to kill 3 people. Not working

Now I have tried so many flavours I am almost crying. The 3 people he has to kill is not in a group, nor are they the only 3 people in a area. So what I have done is to add on there init box

this addEventHandler ["killed", {_this exec "kill_task2.sqf"}];

in my kill_task2.sqf file I have

_this removeAllEventHandlers "killed";
_bodies = player getVariable "num_task2";
if ( _bodies > 2) then 
{
hint format['Guards Killed %1',_bodies];
task2 setTaskState "SUCCEEDED";
[task2] call mk_fTaskHint;

} else 
{
_bodies = _bodies + 1;
player setVariable ["num_task2", _bodies];
hint format['Guards Killed %1',_bodies];
};

The idea is to simply call the script on the kill, incrament the kill count, and when all 3 guards are dead, finish the task...

Right now The scripts is called twice for every kill, resulting in double numbers on the hint, and both the if and else is executing???

What am I doing wrong here...

---------- Post added at 03:35 PM ---------- Previous post was at 02:00 PM ----------

So I have simpliefied the script even more...

_obj = _this select 0;
_obj removeEventHandler ["killed", 0];
num_task2 = num_task2 + 1;
hint format['Guards Killed %1',num_task2];

but still get 2 Guards killed if I kill only 1

another version

_obj = _this select 0;
_obj removeEventHandler ["killed", 0];
sleep 10;
num_task2 = num_task2+1;
hint format['Guards Killed %1',num_task2];
sleep 10;
if (num_task2==3) then 
{
task2 setTaskState "SUCCEEDED";
[task2] call mk_fTaskHint;
}

This one starts to count better, but sometime I get a 2 count and sometime a 1 ???

Share this post


Link to post
Share on other sites

I haven't even looked at the script but are you suffering from what seems to be the 1.03 multikill/multideath bug? Sorry, I am in the middle of cooking.

Share this post


Link to post
Share on other sites

Thanks for the heads-up on the bug, but I think I am getting somewhere.

I have now tried naming the guards... g1 g2 g3..

on my Task init script I then assign the EventHandlers via the name

and changed my script to this

_obj = _this select 0;
_obj removeEventHandler ["killed", 0];
sleep 10;
?!(alive g1) : hint 'Guards 1 Killed ';
?!(alive g2) : hint 'Guards 2 Killed ';
?!(alive g3) : hint 'Guards 3 Killed ';
?(!(alive g1)&&!(alive g2)&&!(alive g3)) : hint 'ALL Guards Killed';

It seems it's counting right now... but I am so confused as to what the right syntax should be.. :confused:

I'll post more results as I make progress...

---------- Post added at 04:15 PM ---------- Previous post was at 04:01 PM ----------

Finally I got it to work :yay:

_obj = _this select 0;
_obj removeEventHandler ["killed", 0];
sleep 10;
?!(alive g1) : hint 'Guards 1 Killed ';
?!(alive g2) : hint 'Guards 2 Killed ';
?!(alive g3) : hint 'Guards 3 Killed ';
?(!(alive g1)&&!(alive g2)&&!(alive g3)) : hint 'ALL Guards Killed'; task2 setTaskState "SUCCEEDED"; [task2] call mk_fTaskHint;

What is REALLY puzzeling me is that I have to use this if syntax ?():; and not the if() then{}else{};

So far learning this scripting language has got to be THE MOST confusing experience in my coding life...

It took me 6 hours to solve this simple problem.... phew

Share this post


Link to post
Share on other sites

What is REALLY puzzeling me is that I have to use this if syntax ?():; and not the if() then{}else{};

Because you exec (for SQS files), which don't support the latter syntax (which is used in SQF files, and executed with execVM command). Forget SQS and learn SQF and it becomes much simpler (if you got even a little experience from any programming language). And if you don't need to show the hints (which I personally don't like in missions), you could just make a trigger with !alive g1 && !alive g2 && !alive g3

Share this post


Link to post
Share on other sites

just some thoughts:

1)

this addEventHandler ["killed", {_this exec[b]VM[/b] "kill_task2.sqf"}];

should be correct. basically a somewhat minor one but "kill_task2.sqf" would have been treated as sqs afaik, that means your first sript wont run as it consists of line brakes which aren't supported in sqs syntax

2) why trying to delete any eventhandlers before all three are executed ? dosent make much sense to me, I'd just keep them and delete all eventhandlers after "they have done their job"

3) the 3rd script (!alive..) may always work nice, but include the downside that dead units not necessarily are killed ones - they may have fallen off a ladder :)

so eventhandlers seem the way to go here, i would stick with them.

So why don't enjoy their full comfort and check what they pass to the script they call ?

http://community.bistudio.com/wiki/Armed_Assault:_EventHandlers_List

_this select 1;

f.e. always returns the killer in your case.

if you have your three eventhandlers added to your three guards to kill, you just need to increment a variable or add the killer to an array (that you count) if the killer (_this select 1) is the required unit.

but, besides all that technical stuff,

what if another unit kills one of those 3 guards ?

then you'll never get the three kills maybe required....

Share this post


Link to post
Share on other sites

I don't see the point in not using a trigger if all your doing is checking for dead units.

anyway I also found away around it, you have to set up some variables placed in an init and name your men.

state1=0;state2=0;state3=0 ;num_task2 =0

and the eventhandler to call the code

this addEventHandler["killed", {[_this select 0, _this select 1] execVM "kill_task2.sqf"}];

_obj = _this select 0;
_obj removeEventHandler ["killed", 0];
_killer = _this select 1;
hint format["Killer: %1, victim: %2, NameOfKiller: %3", _killer, _obj, name _killer];
sleep 2;

if ((_obj==g1) and (state1==0)) then 
{
num_task2 = num_task2+1;
state1=1;
};
if ((_obj==g2)and (state2==0)) then 
{
num_task2 = num_task2+1;
state2=1;
};
if ((_obj==g3) and (state3==0))then 
{
num_task2 = num_task2+1;
state3=1;
};

hint format["Guards Killed %1",num_task2];
sleep 1;

if (num_task2==3) then 
{
task2 setTaskState "SUCCEEDED";
[task2] call mk_fTaskHint;
};

It just sets a flag so it can't add more than one for each unit.

but id !alive is working then that's fine.

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  

×