Jump to content
Sign in to follow this  
clydefrog

adding +1 to a variable multiple times then counting not working

Recommended Posts

I have 5 vehicles, I have variable in my init.sqf that says vehicles_destroyed=0. Each time one of the vehicles is killed I have 2 triggers for each vehicle that does the following:

Trigger 1A

condition - !alive veh1 //checks that veh1 is destroyed

onAct - vehicles_destroyed=+1; //adds 1 to vehicles_destroyed so it is now vehicles_destroyed=1

Trigger 1B

condition - vehicles_destroyed==1 //checks that vehicles_destroyed number is 1

onAct - hint "vehicle destroyed, 4 vehicles remaining"; //tells player 4 vehicles remain

then for the 2nd vehicle:

Trigger 2A

condition - !alive veh2 //checks that veh2 is destroyed

onAct - vehicles_destroyed=+1; //adds 1 to vehicles_destroyed so it should now be vehicles_destroyed=2 if a vehicle has previously been destroyed (which it has when I test)

Trigger 2B

condition - vehicles_destroyed==2 //checks that vehicles_destroyed number is 2

onAct - hint "vehicle destroyed, 3 vehicles remaining"; //tells player 3 vehicles remain

etc.

But only the first pair of triggers work. The 2nd vehicles 2nd trigger (Trigger 2B) that checks to see if vehicles_destroyed==2 doesn't work, it is not counting the +1 that was added from the first vehicle being destroyed.

What is going wrong here? I need it to +1 to vehicles_destroyed every time a vehicle is destroyed but each trigger ignores the +1 added in the previous trigger.

Edited by clydefrog

Share this post


Link to post
Share on other sites

Sorry but what do I do with this? Do I replace the vehicles_destroyed=+1 with it?

Yep just tried it and it works as it's meant to, thanks a lot.

Edited by clydefrog

Share this post


Link to post
Share on other sites

I have another question to do with this. As said I have "vehicles_destroyed=0" in my init, then I add 1 to it each time a vehicle is destroyed using vehicles_destroyed = vehicles_destroyed + 1. Then another trigger checks the new count (e.g. vehicles_destroyed==1) then gives a message to say 4 vehicles remaining.

This all works but not when it comes to JIP players, I had destroyed 4 of the 5 then reconnected to the dedicated server, when I destroyed the next one it came up saying "4 vehicles remaining. When I reconnected it reset the variable to what it was in the init (vehicles_destroyed=0). How can I make this work for JIP players so that it remembers the current amount of vehicles destroyed?

Thanks

Share this post


Link to post
Share on other sites

You need to publicvariable vehicles_destroyed each time it's value changes. vehicles_destroyed = vehicles_destroyed +1; Publicvariable "vehicles_destroyed";

Also, use a nil check in the init.

init.sqf

If (IsNil "vehicles_destroyed") Then {vehicles_destroyed=0;};

This is saying, if the variable doesn't even exist yet then vehicles_destroyed = 0. Else, just keep the PV value. PV are sent to clients before the init runs.

Share this post


Link to post
Share on other sites

I thought it would be something to do with publicvariable. So which of my triggers in the original post would that go in, 1A or 1B?

Share this post


Link to post
Share on other sites

It doesn't matter. Each time the value changes you need to PV it. Weather it's in one trigger or another, or an external script.

Share this post


Link to post
Share on other sites

Ok so if I did this:

Trigger 1A

condition - !alive veh1

onAct - vehicles_destroyed = vehicles_destroyed + 1; publicVariable "vehicles_destroyed";

and put that line in my init, that should be fine?

Ah I see you edited the post and added that (unless I just missed it originally). Thanks again for your help.

Share this post


Link to post
Share on other sites

Yes, pV it. Also put this into your init.sqf.

init.sqf

If (IsNil "vehicles_destroyed") Then {vehicles_destroyed=0;};

Share this post


Link to post
Share on other sites

Oh one other thing, do I replace this line in my init:

vehicles_destroyed=0;

with this one:

If (IsNil "vehicles_destroyed") Then {vehicles_destroyed=0};

or do I have both?

Share this post


Link to post
Share on other sites

If that doesn't work then you'll need to use onplayerconnected & GetVariable

---------- Post added at 15:51 ---------- Previous post was at 15:50 ----------

Just

If (IsNil "vehicles_destroyed") Then {vehicles_destroyed=0;};

Share this post


Link to post
Share on other sites

Ok thanks, hopefully that will sort it out. If not, how would you do it using onplayerconnected and GetVariable if you don't mind explaining?

Share this post


Link to post
Share on other sites
Just see if that works :)

This doesn't work properly, now each time I destroy one the count goes up by 2 instead of 1, any ideas?

Share this post


Link to post
Share on other sites

A much more convenient approach would be to use the Killed-EventHandler for the vehicles.

Consider trying this in your init.sqf:

if (isServer) then {
vehicles_destroyed = 0;
publicVariable "vehicles_destroyed";


{
	_x addEventHandler ["Killed", 
		{
			vehicles_destroyed = vehicles_destroyed + 1;
			publicVariable "vehicles_destroyed";


			hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
		}
	];
} forEach [veh1, veh2, veh3, veh4, veh5];
};


waitUntil {!(isNil "vehicles_destroyed")};
"vehicles_destroyed" addPublicVariableEventHandler {
hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
};

Edited by qbt
Updated code.

Share this post


Link to post
Share on other sites
This doesn't work properly, now each time I destroy one the count goes up by 2 instead of 1, any ideas?

Triggers exist on all machines if placed in the editor. You could change the condition of the trigger to

!alive veh1 && isServer

That would make sure that only the server sends the publicVariable.

Share this post


Link to post
Share on other sites
Triggers exist on all machines if placed in the editor. You could change the condition of the trigger to

!alive veh1 && isServer

That would make sure that only the server sends the publicVariable.

Hmm, strange thing is though is that I am using this way of counting how many objects have been destroyed to give a message for 2 sets of things. One is for vehicles spawned through scripts which doesn't have the problem of adding more than 1 each time one is destroyed, and the other is for objects destroyed that are just placed in the editor, which is the one that has the count going up 2 each time one is destroyed (and also actually stopped working completely after I destroyed the 3rd object). Why would one work fine and the other not?

So you can see what I am doing, here are the triggers for both a vehicle being destroyed (no count issue) and one of the other objects being destroyed (count issue). Also with the vehicles because of how the script is I need to reference them by group name for each one and not unit name, so:

Vehicle destroyed Triggers:

Trigger 1A

Condition:

({alive _x} count units vehicle1) < 1

OnAct:

vehicle_count = vehicle_count + 1; publicVariable "vehicle_count";

Trigger 1B

Condition:

vehicle_count==1;

OnAct:

titletext ["vehicle neutralised, 4 vehicles remaining.","PLAIN DOWN"];

Object destroyed Triggers:

Trigger 2A

Condition:

!alive foc1

OnAct:

deletevehicle foc1; objects_destroyed = objects_destroyed + 1; publicVariable "objects_destroyed"; 

Trigger 2B

Condition:

objects_destroyed==1

OnAct:

titletext ["Object destroyed, 7 remaining.","PLAIN DOWN"];

---------- Post added at 11:22 ---------- Previous post was at 11:19 ----------

A much more convenient approach would be to use the Killed-EventHandler for the vehicles.

Consider trying this in your init.sqf:

if (isServer) then {
vehicles_destroyed = 0;
publicVariable "vehicles_destroyed";


{
	_x addEventHandler ["Killed", 
		{
			vehicles_destroyed = vehicles_destroyed + 1;
			publicVariable "vehicles_destroyed";


			hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
		}
	];
} forEach [veh1, veh2, veh3, veh4, veh5];
};


waitUntil {!(isNil "vehicles_destroyed")};
"vehicles_destroyed" addPublicVariableEventHandler {
hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
};

Thanks for this, if I used that though, since I'm using group names to refer to the vehicles and not unit names (because of the script I'm using, I'd rather be able to use unit names if I could), how would I amend that code so it checks if each group has 0 units left instead of checking if just a unit name is dead?

Edited by clydefrog

Share this post


Link to post
Share on other sites

I think you misunderstood my question, also the triggers that count the units in the vehicle groups don't seem to need the isServer bit for some reason, it already works fine. What I meant was, is in qbt's code there is the bit that says:

} forEach [veh1, veh2, veh3, veh4, veh5];

How would I change that script so it isn't doing it by unit names but by group names and is checking the groups have less than one unit in each?

Really I don't think I actually need to use that as the vehicle count thing seems to be working ok, and now so is the objects count after adding isServer to the trigger condition. It's strange though how the vehicles one doesn't have that problem, the only difference between the vehicles destroyed and the objects destroyed is the vehicles are spawned through a script and the objects are editor placed.

Edited by clydefrog

Share this post


Link to post
Share on other sites
I think you misunderstood my question, also the triggers that count the units in the vehicle groups don't seem to need the isServer bit for some reason, it already works fine. What I meant was, is in qbt's code there is the bit that says:

} forEach [veh1, veh2, veh3, veh4, veh5];

How would I change that script so it isn't doing it by unit names but by group names and is checking the groups have less than one unit in each?

Really I don't think I actually need to use that as the vehicle count thing seems to be working ok, and now so is the objects count after adding isServer to the trigger condition. It's strange though how the vehicles one doesn't have that problem, the only difference between the vehicles destroyed and the objects destroyed is the vehicles are spawned through a script and the objects are editor placed.

For specific groups:

{
   // If group has one unit
   if ((count (units _x)) == 1) then {
       // Do what you want
   };
} forEach [grpOne, grpTwo, grpThree];

For all groups from any side:

{
   // If group has one unit
   if ((count (units _x)) == 1) then {
       // Do what you want
   };
} forEach allGroups;

For groups from BLUFOR (west):

{
   // If group has one unit
   if (((count (units _x)) == 1) && ((side _x) == west)) then {
       // Do what you want
   };
} forEach allGroups;

:dance1:

Share this post


Link to post
Share on other sites
For specific groups:

{
   // If group has one unit
   if ((count (units _x)) == 1) then {
       // Do what you want
   };
} forEach [grpOne, grpTwo, grpThree];

For all groups from any side:

{
   // If group has one unit
   if ((count (units _x)) == 1) then {
       // Do what you want
   };
} forEach allGroups;

For groups from BLUFOR (west):

{
   // If group has one unit
   if (((count (units _x)) == 1) && ((side _x) == west)) then {
       // Do what you want
   };
} forEach allGroups;

:dance1:

Nice thanks, I want it for specific groups, so how would I put that into the rest of your code? Sorry I'm not too good with editing scripts really.

Share this post


Link to post
Share on other sites
Nice thanks, I want it for specific groups, so how would I put that into the rest of your code? Sorry I'm not too good with editing scripts really.

if (isServer) then {
   vehicles_destroyed = 0;
   publicVariable "vehicles_destroyed";


   {
       if ((count (units _x)) == 1) then {
           (vehicle (leader _x)) addEventHandler ["Killed", 
               {
                   vehicles_destroyed = vehicles_destroyed + 1;
                   publicVariable "vehicles_destroyed";


                   hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
               }
           ];
       };
   } forEach [grpOne, grpTwo, grpThree];
};


waitUntil {!(isNil "vehicles_destroyed")};
"vehicles_destroyed" addPublicVariableEventHandler {
   hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
};

Let me know how it works!

Share this post


Link to post
Share on other sites
if (isServer) then {
   vehicles_destroyed = 0;
   publicVariable "vehicles_destroyed";


   {
       if ((count (units _x)) == 1) then {
           (vehicle (leader _x)) addEventHandler ["Killed", 
               {
                   vehicles_destroyed = vehicles_destroyed + 1;
                   publicVariable "vehicles_destroyed";


                   hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
               }
           ];
       };
   } forEach [grpOne, grpTwo, grpThree];
};


waitUntil {!(isNil "vehicles_destroyed")};
"vehicles_destroyed" addPublicVariableEventHandler {
   hint format ["Vehicle destroyed, %1 vehicles remaining.", (5 - vehicles_destroyed)];
};

Let me know how it works!

It doesn't work, no hints show up.

Share this post


Link to post
Share on other sites
It doesn't work, no hints show up.

Are you running the game with the -showScriptErrors parameter? Give me a few minutes and I'll make test mission so I can debug it.

Edit:

It works for me, did you remember to name the groups to grpOne, grpTwo etc? You can do this with for example having...

grpOne = group this

...in the initialization of a unit.

Edited by qbt

Share this post


Link to post
Share on other sites
Are you running the game with the -showScriptErrors parameter? Give me a few minutes and I'll make test mission so I can debug it.

Edit:

It works for me, did you remember to name the groups to grpOne, grpTwo etc? You can do this with for example having...

grpOne = group this

...in the initialization of a unit.

I changed the group names to the names of the groups I'm using

so scud1, scud2, scud3, scud4, scud5

all the group names are defined in the scripts that spawns each of them and they work because the final trigger checks the number of alive units is 0 for each of those groups then gives a task completed hint.

But if it works for you I must be doing something wrong.

Edited by clydefrog

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  

×