Jump to content
davidoss

how to smarter create this

Recommended Posts

Hi.

 

I want to ask you fellows how to do it better :

_handle_damage =

		[] spawn { 

			while {!isNull highjackveh} do { 
			
						if (({isPlayer _x AND (_x distance highjackveh) > 600} count (playableUnits + switchableUnits)) != 0) then { 
							
							highjackveh allowDamage false;
							
						} else {

							highjackveh allowDamage true;
						};
						
						sleep 20;
			};
		};

This is part of side mission which i create just now.

I wanna make this vehicle temporary indestructible because of AI driving skills.

In this way the code is repeat constantly until player shows up near.

There are no need to repeat the code if the vehicle is already indestructible.

Share this post


Link to post
Share on other sites

So the AI drives the car until the player steals it right? As soon as a player drives the car, the damage should be activated again? Or is the player supposed to ambush this car to destroy it?

Share this post


Link to post
Share on other sites

There will be ( i hope)  a convoy with 3 vehicles driving somewhere ( in reality around the map). In the middle of convoy a truck full witch civilians which are escorted by insurgents. Players need to release safely the civilians .
 
For now i have just a little bit of it done.

  Reveal hidden contents

Share this post


Link to post
Share on other sites

HI.
 
I am almost done with my script.

 

  Reveal hidden contents

 

Unfortunately i have no idea how to  define that the vehicle with civilians has no driver.

 

The problem is that the script need to wait  until the driver are not there ( killed or something).

Then is trigger created and defense units spawned.

I had used the condition :

waitUntil { sleep 1;({isPlayer _x AND (_x distance highjackveh) < 600} count (playableUnits + switchableUnits)) != 0 AND !(alive _unit14_driver)};

to define that the driver is dead but what if players not did that? How to find better way?

Share this post


Link to post
Share on other sites
isNull driver car;

Perhaps that helps.

Share this post


Link to post
Share on other sites

This is not changing anything i need there something like

waitUntil { sleep 1;({isPlayer _x AND (_x distance highjackveh) < 600} count (playableUnits + switchableUnits)) != 0 AND (!(alive _unit14_driver) OR !(alive _unit17) OR !(alive _unit19))};

Is that code alright?
 

Share this post


Link to post
Share on other sites

If it works, then it's probably alright ;)

Share this post


Link to post
Share on other sites

To reduce code and max out performance:

waitUntil {
	sleep 1;
	1 == {
		if (_x distance highjackveh < 600) exitWith {1}
	} count allPlayers
	&&	//equivalent to "and", used in almost all programming languages
	1 == {
		if (!alive _x) exitWith {1}
	} count [_unit14_driver, _unit17, _unit19];
};

Share this post


Link to post
Share on other sites
  On 9/11/2015 at 9:20 PM, Heeeere's johnny! said:

 

To reduce code and max out performance:

waitUntil {
	sleep 1;
	1 == {
		if (_x distance highjackveh < 600) exitWith {1}
	} count allPlayers
	&&	//equivalent to "and", used in almost all programming languages
	1 == {
		if (!alive _x) exitWith {1}
	} count [_unit14_driver, _unit17, _unit19];
};

there isnt any reason to run a loop inside an event handler at all.   if you want to make the vehicle not take damage or limit the damage use something like this.

_veh addEventHandler ["HandleDamage", {if (_this select 4 isEqualTo "" && (side (_this select 0)) isEqualTo resistance) then {(_this select 2)/100;};}];

all this basically says is that if no projectile hit the vehicle and a unit that is side of resistance is driving it, divide the damage by 100.   thats what i use in a mission of mine to keep the cars/trucks from being wrecked from the AI driving 30 seconds after mission starts...:)

 

 

for your varaible you want to set when a player takes the vehicle you are better off using the getIn/getOut event handlers for that.  

Share this post


Link to post
Share on other sites

Thanks noticed that. The mission is done.

If someone of you guys have any idea how to make it better please help.

  Reveal hidden contents

Share this post


Link to post
Share on other sites

Here's an updated version. I can still be optimized, especially regarding all the global variables, but my desire to sleep finally got bigger than my desire to script, so here it is:

 

  Reveal hidden contents

 

Just a few hints, please don't take it as smartass-dom. :)

 

Get a little more consistent with camelCase, not only on long words, but everywhere where it's reasonable, like in "setDir" or "configFile" or "_spawnOnRoad" ...

 

For reasons of distinction, use CAPITAL_LETTERS only for #define'ed values.

 

addEventHandler also takes code as second array member, so I'd very much recommend using that instead of a string.

 

getDammage is a very old and incorrectly spelled command which only exists for legacy reasons. Use damage instead, which does the exact same.

 

Don't be careless with indention. Indention should make the code more readable, but it won't if you indent arbitrarily as you did in some lines in that script.

 

Avoid unnecessary brackets. Logical commands (&&, ||, ==, !=, >, >=, <, <=, ...) always evaluate last, so "if ((damage _x) == 0)" is the same as "if (damage _x == 0)"

 

Make spaces between commas and words, because this:

 

[highjackveh,highjackescortveh1,highjackescortveh2]

 

just looks like a long snake of letters, while this:

 

[highjackveh, highjackescortveh1, highjackescortveh2]

 

at least gives you a chance.

 

Use line breaks if the line becomes too long. 200 characters in a single line is too long and has the potential to be worse readable than if you made line breaks where reasonable. I don't recommend the way I did it with your "... spawn BIS_fnc_taskCreate" line, but it's how I do it.

 

setMarkerSize is not needed for markers with icons, only if you setMarkerType and/or setMarkerBrush or you need the size for other reasons.

 

{_x = nil} forEach [hostages, defencegrp];

The above doesn't do what you'd expect, because _x is not the element itself, but rather a copy, just as if you'd copy a variable with:

"_copy = +_someVar".

 

And finally, keep things simple and reduce redundance where reasonable. Putting 35 "createMarker" statements, which do the exact same, means that you've got to change 35 lines if you wanted to change something about the markers. Keep it simple and let a loop do the redundant work.

  • Like 2

Share this post


Link to post
Share on other sites

Thank you very much Heeeere's johnny!.
Awesome knowledge.
 
PS.
 

{deleteVehicle _x} forEach crew groupveh; //What should (crew groupveh + [groupveh]) be doing?

In this way i got delete the group vehicle too. Deleting group units and crew  left behind the ofroad.

Share this post


Link to post
Share on other sites
  On 9/13/2015 at 12:44 AM, davidoss said:

Thank you very much Heeeere's johnny!.

Awesome knowledge.

 

PS.

 

{deleteVehicle _x} forEach crew groupveh; //What should (crew groupveh + [groupveh]) be doing?

In this way i got delete the group vehicle too. Deleting group units and crew  left behind the ofroad.

 

Oh, I see. Clever. ;)

Share this post


Link to post
Share on other sites

Could turn those spawned damage and fuel threads into eventHandlers. (This was my attempt at cleaning up your code last night)

  Reveal hidden contents

Share this post


Link to post
Share on other sites

Holy shit . You guys want to blew up my mind. I do not understand my script anymore. :blink:

 

Larrow   your script works but there is a main problem because the truck goes  after spawn ahead  escort vehicles, he should be between them.

 

I have no idea now  how to organize it. :D

 

 

There are also some EH errors :

Error in expression < {
0
}else{
_veh = _this select 0;
_veh removeEventHandler [ "HandleDamage", _ve>
16:14:10   Error position: <removeEventHandler [ "HandleDamage", _ve>
16:14:10   Error Type Any, needed Numeric

Anyway I do not knew how to thanks you .

I had to use global variables because i have no idea how to passing local variables into EH.

One more time - big thanks.
 

Share this post


Link to post
Share on other sites
  Quote

Larrow your script works but there is a main problem because the truck goes after spawn ahead escort vehicles, he should be between them.

Ah thats likely just the order they are sent to norrin's script in. They do actually spawn position wise escort, truck, escort.

change..

}forEach [
	[ "O_Truck_03_transport_F", 0 ],
	[ "B_MRAP_01_F", +40 ],
	[ "B_MRAP_01_F", -40 ]
];

to look like

}forEach [
	[ "B_MRAP_01_F", +40 ],
	[ "O_Truck_03_transport_F", 0 ],
	[ "B_MRAP_01_F", -40 ]
];

As this is the order they are placed in the _vehicles array which is passed to norrin's script.

Also change, just above that

if ( _forEachIndex isEqualTo 0 ) then {

to

if ( _forEachIndex isEqualTo 1 ) then {

To make sure the transport gets its extra man added as cargo and not to a non existent gunner position.

 

  Quote

There are also some EH errors :

There is a spelling error in my code

_veh removeEventHandler [ "HandleDamage", _veh getVariable "damageEH" ];

Should be..

_veh removeEventHandler [ "HandleDamage", _veh getVariable "damagedEH" ];

Notice the d in getvariable "damagedEH" to match the name of the setvariable above it.

 

  Quote

Holy shit . You guys want to blew up my mind. I do not understand my script anymore. :blink:

If your uncomfortable with any of the stuff shown do not use it, as it makes little sense using something you would be unable to fix.

Instead use the scripts to learn from and experiment with pieces until you get familiar with how/why they work.

Its always nice to have some examples to work/learn from, scripts do not necessarily need to be excessively optimised, if they work and you feel comfortable with what you have then all is good.

The only bits that i would recommend changing from your original are the spawned loops, the server has enough to cope with without throwing more scheduled code for it to look after every frame and eventHandlers are a better way to look after the fuel and damage.

Most of all have fun :D

Share this post


Link to post
Share on other sites

My code is a garbage. Spending a week to create it and test.

I am shock how in different  way you can get the same scenario.

I am also full of admiration to your knowledge.

Indeed i have lot to learn but not from bohemia wiki.

Share this post


Link to post
Share on other sites

Gosh, I look at length of the script and it stirs my hair up.

 

If .sqf ever was a line / expression efficient language, all of this could be done in < 50 lines. Now, it's way over 200.

 

Not looking to insult anybody, just sayin' for the 1k-th time - SQF is da shit :D.

 

tl;dr Just bitchin' things.

Share this post


Link to post
Share on other sites

SQF is at some point probably not the best thing when you come from programming languages like, C++ or Java.

 

But compared to other scripting languages, I see the aim to make it intuitive, so even people without programming knowledge can learn it. Just take the command "in" which you use to check if something is in an array, a vehicle or location. "in" also exists in JavaScript, but not to use on arrays, not even to use in if-checks, but rather to iterate over objects (sparing the details here).

 

And SQF is hell of a lot more intuitive than PHP, if you ask me. Or could any PHP developer reading this tell by heart what the difference between the PHP functions strpos, strrpos, stripos and strripos is? Some probably even can, but all of which I asked couldn't.

Share this post


Link to post
Share on other sites
  On 9/13/2015 at 5:10 PM, Larrow said:

scripts do not necessarily need to be excessively optimised, if they work and you feel comfortable with what you have then all is good.

 

I absolutely respect your knowledge, capabilities and effort you put into this forum, Larrow. So please excuse me that I dare to question the latter part of the above statement.

 

I've seen some scripts already and they all worked (more or less). But many of them used a lot of global variables, which of course made things a lot easier. But when I see global variables named "box" or "tks1", "tsk2"..., I begin to lose my hair.

 

I would even bet that a lot of addons are in conflict or do not work properly because of such "bad smells".

 

So to sum it up (TL;DR):

Global variables are a very easy way to make a script work and a lot of people feel comfortable with it. But they are certainly not the best solution if you can avoid them with just a little bit of patience and dedication for the sake of integrity and stability.

Share this post


Link to post
Share on other sites

Heeeere's johnny!

 

Most likely  everyone can put some commands together and make a script.

But if you think about MP environment arma you need different  point of view.

You cant easy  test your script you need MP environment and more players.

Stuff that working in SP in MP can not working at all. Saying nothing about JIP.

Global variables making coding easy for beginners i think.

 

I have changed global variables to local  and  marker position is not updated for players.

//create truck mobile marker
_markerhighjackveh = createMarker ["ICCVeh", getPos _highjackveh];
_markerhighjackveh setMarkerType "c_car";
_markerhighjackveh setMarkerColor "ColorGreen";
_markerhighjackveh setMarkerText "I.C.C";


_highjackvehpos = [_highjackveh] spawn {
_highjackveh = _this select 0;
while {!isNull _highjackveh} do {
"ICCVeh" setMarkerPos getPos _highjackveh;
sleep 2;
};
};

I just don't understand why.

Share this post


Link to post
Share on other sites

Wouh, don't get me wrong. I'm not saying, you should immediately change all your globals to locals, who knows what's gonna happen, plus I'm also a fan of "Never touch a running system".

 

You're right, globals are an easy way for starters and yes, thinking about "publicVariable", they are sometimes even needed and I'd be lying if I said I don't use them too, especially for testing purposes, because it's not always handy or even possible to setVariable on something to avoid a global variable. But if I use them, I'd rather have proper names and prefixes to minimize the risk of getting in conflict with any other script or addon.

 

But to your snippet, I tested it in the editor and everything works fine for me. Does it not work in MP?

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

×