Jump to content
Sign in to follow this  
pyro05x

cleaning sqf code

Recommended Posts

any ideas for cleaning code that looks like this?:

from my "onPlayerKilled.sqf"

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">...

if (_perpSide == west) then

{

if (wOwned) then

{

if (local server) then

{

_perp addScore _conDPoint;

_perp sideChat "I killed a BLUFOR attacker!";

};

if (player == _perp) then

{

if (_conDPoint==1) then

{

hint "You earned a flag defense point!";

}

else

{

if (_conDPoint>0) then

{

hint format ["You earned %1 flag defense points!",_conDPoint];

};

};

};

}

else

{

if (eOwned) then

{

if (local server) then

{

_perp addScore _conAPoint;

_perp sideChat "I killed an OPFOR defender!";

};

if (player == _perp) then

{

if (_conAPoint==1) then

{

hint "You earned a flag assault point!";

}

else

{

if (_conAPoint>0) then

{

hint format ["You earned %1 flag assault points!",_conAPoint];

};

};

};

};

};

}

else //_perpSide == east

{

if (eOwned) then

{

if (local server) then

{

_perp addScore _conDPoint;

_perp sideChat "I killed an OPFOR attacker!";

};

if (player == _perp) then

{

if (_conDPoint==1) then

{

hint "You earned a flag defense point!";

}

else

{

if (_conDPoint>0) then

{

hint format ["You earned %1 flag defense points!",_conDPoint];

};

};

};

}

else

{

if (wOwned) then

{

if (local server) then

{

_perp addScore _conAPoint;

_perp sideChat "I killed a BLUFOR defender!";

};

if (player == _perp) then

{

if (_conAPoint==1) then

{

hint "You earned a flag assault point!";

}

else

{

if (_conAPoint>0) then

{

hint format ["You earned %1 flag assault points!",_conAPoint];

};

};

};

};

};

};

...

it looks pretty nasty, imo.

i thought about testing various conditions beforehand and creating a string of binary numbers then using <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">switch {_s} do

{

case "0000":{blahblah;};

case "0001":{blehbleh;};

}; but it turns out that would really just be nasty and ultimately just more code. confused_o.gif

Share this post


Link to post
Share on other sites

Might I suggest using properties/variables and filling in using Format command. You are using alot of reoccuring commands/text. Best to convert it to a function and supply the appropriate parameters. I am short on time or I would write the code for you.. but basically supply text parameters and use a single function to :

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">hint format["You killed a %1 %2",_this select 0, _this select 1];

etc...

etc..

If I get a chance to come back later, I will elaborate.

Share this post


Link to post
Share on other sites

ok. i put some work into it and here's what i came up with

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

if (_perpSide == west) then

{

if (wOwned) then

{

_pType = "defense";

}

else

{

if (eOwned) then

{

_pType = "assault";

};

};

}

else //_perpSide == east

{

if (eOwned) then

{

_pType = "defense";

}

else

{

if (wOwned) then

{

_pType = "assault";

};

};

};

_pPoint = switch (_pType) do

{

case "assault":{_conAPoint};

case "defense":{_conDPoint};

};

_conAPoint=nil;

_conDPoint=nil;

if (local server) then

{

_perp addScore _pPoint;

_perp sideChat format ["I killed a%1 %2er!",

switch (_perpSide) do {

case west:{"n OPFOR"};

case east:{" BLUFOR"};},

switch (_pType) do {

case "assault":{"attack"};

case "defense":{"defend"};}];

};

if (player == _perp) then

{

if (_pPoint == 1) then

{

hint format ["You earned a flag %1 point!", _pType];

}

else

{

if (_pPoint > 0) then

{

hint format ["You earned %1 flag %2 points!", _pPoint, _pType];

};

};

};

Share this post


Link to post
Share on other sites

You can also shorten:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">if (_perpSide == west) then

{

if (wOwned) then

{

 _pType = "defense";

}

else

{

 if (eOwned) then

 {

  _pType = "assault";

 };

};

}

else //_perpSide == east

{

if (eOwned) then

{

 _pType = "defense";

}

else

{

 if (wOwned) then

 {

  _pType = "assault";

 };

};

};

to <table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_ptype = "assault";

if (   ((_perpSide == west) && (wOwned))   ||  ((_perpSide == east) && (eOwned))  ) then

{

  _pType = "defense";

}

It would be even smaller if we had an Xor operator.

e.g.

if (_perpSide == West) Xor (eOwned)

Share this post


Link to post
Share on other sites

sweet! thanks for the insight. messy code or overly complex code is something i absolutely hate.

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  

×