Raised This Month: $ Target: $400
 0% 

"if" or "else if"?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GoRRageBoy
BANNED
Join Date: Jul 2011
Old 09-25-2011 , 01:45   "if" or "else if"?
Reply With Quote #1

So I've given up on finishing my Talk Like Bill plugin (it would've changed a Soldier wearing Bill's Hat's voice clips to those of Bill from Left 4 Dead)

Instead, I've decided to use the coding I already did to make a different plugin.

However, I've hit a brick wall in the coding and I require a little assistance.

Here's where I got stuck:

PHP Code:
public Action:Event_HorsemannKilled(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    if (
IsValidClient(client))
    {
        new 
TFClassType:class = TF2_GetPlayerClass(client);
        if (class == 
TFClass_Soldier)
        {
            
EmitSoundToAll(SOLDIER_HHHKILLED[GetRandomInt(03)], client___1.0);
        }
    }

I'm going to code in a separate EmitSoundToAll for all 9 classes. But I don't know whether to do it like this:

PHP Code:
public Action:Event_HorsemannKilled(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    if (
IsValidClient(client))
    {
        new 
TFClassType:class = TF2_GetPlayerClass(client);
        if (class == 
TFClass_Soldier)
        {
            
EmitSoundToAll(SOLDIER_HHHKILLED[GetRandomInt(03)], client___1.0);
        }
        
        if (class == 
TFClass_Scout)
        {
            
EmitSoundToAll(SCOUT_HHHKILLED[GetRandomInt(03)], client___1.0);
        }
    }

Or like this:

PHP Code:
public Action:Event_HorsemannKilled(Handle:event, const String:name[], bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    if (
IsValidClient(client))
    {
        new 
TFClassType:class = TF2_GetPlayerClass(client);
        if (class == 
TFClass_Soldier)
        {
            
EmitSoundToAll(SOLDIER_HHHKILLED[GetRandomInt(03)], client___1.0);
        }
        
        else if (class == 
TFClass_Scout)
        {
            
EmitSoundToAll(SCOUT_HHHKILLED[GetRandomInt(03)], client___1.0);
        }
    }

Basically, when the event happens, I want the emitsoundtoall function for each class to trigger on every player in the server, and I'm confused about if I'm supposed to use "if" or "else if" for each subsequent "class == TFClass_<class>" thing.

Then again, I'm probably and idiot and overthinking this, and I should just use "if"....but I don't know.

Last edited by GoRRageBoy; 09-27-2011 at 10:59.
GoRRageBoy is offline
napalm00
Veteran Member
Join Date: Jun 2011
Location: Italy, sadly
Old 09-25-2011 , 02:31   Re: "if" or "else if"?
Reply With Quote #2

I'd use a switch
PHP Code:
switch(class)
{
case 
TFClass_Soldier:
{
//do stuff for solly
}
case 
TFClass_Scout:
{
//do stuff for scout
}

Can't really indentate it with the phone, I think you got the idea btw.
If you use the "if" example, the game will check ALL the cases and see what's false and what's true.
With the "else if" example, it will skip the false ones and move directly onto the true ones.
It's really all about efficiency, I suggest the "else if" or, better, the switch (much clearer to read).
Hope I helped, correct me if I'm wrong
__________________

Last edited by napalm00; 09-25-2011 at 03:11.
napalm00 is offline
GoRRageBoy
BANNED
Join Date: Jul 2011
Old 09-25-2011 , 03:13   Re: "if" or "else if"?
Reply With Quote #3

Quote:
Originally Posted by napalm00 View Post
I'd use a switch
PHP Code:
switch(class)
{
case 
TFClass_Soldier:
{
//do stuff for solly
}
case 
TFClass_Scout:
{
//do stuff for scout
}

Can't really indentate it with the phone, I think you got the idea btw.
If you use the "if" example, the game will check ALL the cases and see what's false and what's true.
With the "else if" example, it will skip the false ones and move directly onto the true ones.
It's really all about efficiency, I suggest the "else if" or, better, the switch (much clearer to read).
Hope I helped, correct me if I'm wrong
Why did I not think about that before? Splendid idea!

Last edited by GoRRageBoy; 09-27-2011 at 10:59.
GoRRageBoy is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 09-25-2011 , 05:32   Re: "if" or "else if"?
Reply With Quote #4

Use else if. If the first if met its conditions, the else if afterwards won't be checked.
__________________
hleV is offline
GoRRageBoy
BANNED
Join Date: Jul 2011
Old 09-25-2011 , 05:54   Re: "if" or "else if"?
Reply With Quote #5

Quote:
Originally Posted by hleV View Post
Use else if. If the first if met its conditions, the else if afterwards won't be checked.
I don't want the plugin to not check the next thing.

When the event happens, in this case, the Horsemann spawning, I want all 9 classes to play their respective "HHHSPAWNED" voice clips.

I just made a test version using the switch method, and spawned the Horsemann with the plugin, but the voice clips didn't play.

I think I have to test it on Mann Manor with an actual Horsemann spawning event.

Last edited by GoRRageBoy; 09-27-2011 at 10:59.
GoRRageBoy is offline
napalm00
Veteran Member
Join Date: Jun 2011
Location: Italy, sadly
Old 09-25-2011 , 06:04   Re: "if" or "else if"?
Reply With Quote #6

Quote:
Originally Posted by GoRRageBoy View Post
When the event happens, in this case, the Horsemann spawning, I want all 9 classes to play their respective "HHHSPAWNED" voice clips.
I believe the event you have there is fired when the HHH is killed o0
__________________
napalm00 is offline
DarthNinja
SourceMod Plugin Approver
Join Date: Mar 2009
Location: PreThinkHook()
Old 09-25-2011 , 21:53   Re: "if" or "else if"?
Reply With Quote #7

In this case you would use a switch, but to answer if vs else if:
A player can't be both a scout and a soldier, so:

PHP Code:
if (== true)
{
code;
}
else if (
== false// x can't be both true and false, so we use "else"
{
code;

__________________
DarthNinja is offline
GoRRageBoy
BANNED
Join Date: Jul 2011
Old 09-26-2011 , 04:09   Re: "if" or "else if"?
Reply With Quote #8

Quote:
Originally Posted by DarthNinja View Post
In this case you would use a switch, but to answer if vs else if:
A player can't be both a scout and a soldier, so:

PHP Code:
if (== true)
{
code;
}
else if (
== false// x can't be both true and false, so we use "else"
{
code;

So an else if for each class it is!

Last edited by GoRRageBoy; 09-27-2011 at 10:59.
GoRRageBoy is offline
DarthNinja
SourceMod Plugin Approver
Join Date: Mar 2009
Location: PreThinkHook()
Old 09-26-2011 , 17:18   Re: "if" or "else if"?
Reply With Quote #9

Quote:
Originally Posted by GoRRageBoy View Post
So an else if for each class it is!
No.

Quote:
Originally Posted by DarthNinja View Post
In this case you would use a switch
__________________
DarthNinja is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 09-27-2011 , 09:31   Re: "if" or "else if"?
Reply With Quote #10

Quote:
Originally Posted by DarthNinja View Post
No.
I could have sworn there was somewhere on the Wiki that told you to use switch instead of if/else if, but I can't seem to find it at the moment.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:14.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode