Thread: Objectives
View Single Post
Brad
AMX Mod X Team Member
Join Date: Jun 2004
Old 12-15-2005 , 12:17  
Reply With Quote #6

Quote:
Originally Posted by Brad
There's no need to use cs_get_user_team and I'm pretty sure that as a rule, you shouldn't. Instead just use get_user_team.
Quote:
Originally Posted by Lord Saddler
For this part, what would I use in the if? Would it just be TEAM_T or would it continue to be CS_TEAM_T?
You should define TEAM_T as 1 and TEAM_CT as 2 at the start of your script. Then you can use TEAM_T and TEAM_CT as needed throughout your script.

Quote:
Originally Posted by Brad
Finally, use if else instead of a bunch of if statements. You'll be able to reduce the duplication of return PLUGIN_HANDLED all over the place. For that matter, get rid of return PLUGIN_HANDLED altogether as you're not using the return value for anything and show_objectives isn't a forward.
Quote:
Originally Posted by Lord Saddler
I don't know if it's sad that I don't really know how to do that. Please explain a little more.
You are using consecutive if statements.

if ...
if ...
if ...

And you are returning PLUGIN_HANDLED from each, essentially stopping any further code from running. If instead you used if else you wouldn't need to explicitly stop further code from running with your returns.

if ...
else if ...
else if ...

Compare your original code (shown first) to my code (shown second). Let me know if you have any questions. I've left a couple of my suggestions out as an exercise for you.

Your initial code:
Code:
//Thanks to v3x for help with various things #include <amxmodx> #include <amxmisc> #include <cstrike> public plugin_init() {     register_plugin("Objectives","1.0","Lord Saddler")     register_logevent("NewRound", 2, "0=World triggered", "1=Round_Start") } public NewRound() {     new players[32], num, i     get_players(players, num, "ac")     for(i = 0; i <= num; i++)     {         showobjectives(players[i])     } } public showobjectives(id) {     new map[32]     get_mapname(map,31)         if(containi(map,"cs_")!=-1)     {         new CsTeams:team = cs_get_user_team(id)                 switch(team)         {             case CS_TEAM_CT:             {                 client_print(id,print_center,"Your objective: Infiltrate the Terrorist hideout, save the hostages!")             }             case CS_TEAM_T:             {                 client_print(id,print_center,"Your objective: Protect your hostages from the Counter-Terrorists!")             }         }           return PLUGIN_HANDLED     }         if(containi(map,"de_")!=-1)     {         new CsTeams:team = cs_get_user_team(id)                 switch(team)         {             case CS_TEAM_CT:             {                 client_print(id,print_center,"Your objective: Stop the Terrorists from planting their C4!")             }             case CS_TEAM_T:             {                 client_print(id,print_center,"Your objective: Plant the C4 and guard it!")             }         }         return PLUGIN_HANDLED     }         if(containi(map,"fy_")!=-1)     {         client_print(id,print_center,"Your objective: Kill the opposing team at any cost!")                 return PLUGIN_HANDLED     }         if(containi(map,"aim_")!=-1)     {         client_print(id,print_center,"Your objective: Test your aim!")                 return PLUGIN_HANDLED     }         if(containi(map,"awp_")!=-1)     {         client_print(id,print_center,"Your objective: Snipe your way to victory with the AWP!")                 return PLUGIN_HANDLED     }         if(containi(map,"ka_")!=-1)     {         client_print(id,print_center,"Your objective: Stab, slash and cut your way to victory!")                 return PLUGIN_HANDLED     }         if(containi(map,"as_")!=-1)     {         new CsTeams:team = cs_get_user_team(id)                 switch(team)         {             case CS_TEAM_CT:             {                 client_print(id,print_center,"Your objective: Protect the VIP!")             }         case CS_TEAM_T:             {                 client_print(id,print_center,"Your objective: Kill the VIP!")             }         }         return PLUGIN_HANDLED     }         if(containi(map,"dm_")!=-1)     {         client_print(id,print_center,"Your objective: No real objective here, just kill EVERYONE!")                 return PLUGIN_HANDLED     }         if(containi(map,"he_")!=-1)     {         client_print(id,print_center,"Your objective: Blow shit up!")                 return PLUGIN_HANDLED     }         if(containi(map,"scout")!=-1)     {         client_print(id,print_center,"Your objective: Scout your way to victory!")                 return PLUGIN_HANDLED     }         if(containi(map,"kz_")!=-1)     {         client_print(id,print_center,"Your objective: CLIMB!")                 return PLUGIN_HANDLED     }         return PLUGIN_HANDLED }
My first stab at it:
Code:
//Thanks to v3x for help with various things #include <amxmodx> #include <amxmisc> #define TEAM_T  1 #define TEAM_CT 2 public plugin_init() {     register_plugin("Objectives","1.0","Lord Saddler")     register_logevent("NewRound", 2, "0=World triggered", "1=Round_Start") } public NewRound() {     new players[32], num, i     get_players(players, num, "ac")     for(i = 0; i < num; i++)     {         showobjectives(players[i])     } } public showobjectives(id) {     new teamID = get_user_team(id)     new objective[64] = "Unknown."     new map[32]     get_mapname(map, 31)     if (containi(map, "cs_") != -1)     {         switch(teamID)         {             case TEAM_CT: copy(objective, 63, "Infiltrate the Terrorist hideout, save the hostages!")             case TEAM_T:  copy(objective, 63, "Protect your hostages from the Counter-Terrorists!")         }       }     else if (containi(map,"de_") != -1)     {         switch(teamID)         {             case CS_TEAM_CT: copy(objective, 63, "Stop the Terrorists from planting their C4!")             case CS_TEAM_T:  copy(objective, 63, "Plant the C4 and guard it!")         }     }     else if (containi(map,"fy_") != -1)  copy(objective, 63, "Kill the opposing team at any cost!")     else if (containi(map,"aim_") != -1) copy(objective, 63, "Test your aim!")     else if (containi(map,"awp_") != -1) copy(objective, 63, "Snipe your way to victory with the AWP!")     else if (containi(map,"ka_") != -1)  copy(objective, 63, "Stab, slash and cut your way to victory!")     else if (containi(map,"as_") != -1)     {         switch(teamID)         {             case CS_TEAM_CT: copy(objective, 63, "Protect the VIP!")             case CS_TEAM_T:  copy(objective, 63, "Kill the VIP!")         }     }     else if (containi(map,"dm_") != -1)   copy(objective, 63, "No real objective here, just kill EVERYONE!")     else if (containi(map,"he_") != -1)   copy(objective, 63, "Blow shit up!")     else if (containi(map,"scout") != -1) copy(objective, 63, "Scout your way to victory!")     else if (containi(map,"kz_") != -1)   copy(objective, 63, "CLIMB!")     client_print(id, print_center, "Your Objective: %s", objective) }
Note how I avoided the consecutive if statements.

What I left out of my code was:
  • multilingual support
  • ability to handle maps with multiple objectives
  • perhaps an aesthetic reordering of the conditionals
Quote:
Originally Posted by Lord Saddler
EDIT: PS - I haven't looked at these forums for a while (computer issues), and just noticed you are a moderator. Good job, I think it's well deserved.
Thank you.
Brad is offline