How to do this?

How to do this?

Postby 94ge7j998 » 02 Aug 2013, 18:56

Spoiler:


Okay, so I just wanted to know what to put in the 'else if (message == "")' part if I want any other responce you put in to come out as this. One example of this is the help command. How if you type /help randomcommand it will say unknown command. Anyone know how I would do this?
94ge7j998
 
Posts: 64
Joined: 26 Oct 2011, 20:49

Re: How to do this?

Postby Warren1001 » 02 Aug 2013, 19:54

if (message == ">command<")
{
Help(p);
return;
// or
Player.SendMessage(p, "Unknown command.");
}
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: How to do this?

Postby joppiesaus » 02 Aug 2013, 20:13

you can do it like this, with the switch. in the default block is going to be the help message.
for example:
Code: Select all
switch (message)
{
   case "command1":
      //magic here
      break;
   case "command2":
      break;
   default:
      //help / error message here
      break;
}


of course you can do everything with if else if else if else, but this is more orgranized.
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: How to do this?

Postby lucasds12 » 03 Aug 2013, 03:45

It would be recognized like this, correct if I get this code wrong, I am a #C beginner.
Code: Select all
if (message == "unknowncommand")
{
Help(p);
Player.SendMessage("Invalid command");
return;

-Lucas
There is only one thing I do in life, that's contributing here.
lucasds12
 
Posts: 334
Joined: 17 Apr 2013, 16:17
Location: In the deep caves.

Re: How to do this?

Postby Conor » 03 Aug 2013, 04:02

Lucasds - you are missing a '}' - please check your code before giving advice :)

@94ge7j998 The switch statement is worth learning as joppiesaus has stated.

For just an if/elseif/else statement, here is an example of its structure, if it may at all be at help to you.

Code: Select all
public void DoIfStatement(string commandName)
{
   if (commandName == "kick")
   {
        Command.all.Find("kick").Use(null, "");
   }
   else if (commandName == "rules")
   {
        Command.all.Find("rules").Use(Player.Find("conanza121"), "");
   }
   else
   {
        Player.GlobalMessage("An incorrect command was given.");
   }
}
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: How to do this?

Postby joppiesaus » 03 Aug 2013, 08:19

lucasds12 wrote:It would be recognized like this, correct if I get this code wrong, I am a #C beginner.
Code: Select all
if (message == "unknowncommand")
{
Help(p);
Player.SendMessage("Invalid command");
return;

-Lucas


This wont work. you are missing a } and the help message will only appear if you type in unknowncommand.
if you want everythinging one if block 94ge7j998, you can also do this:

Code: Select all
if (message != "command1" && message != "command2")
{
   //help / error message here
}

the != sign will return true if it isn't true.
the && sign will say the computer only executes the if block if multiple things are true.

If you want more commands after it, do this:
Code: Select all
&& message != "command3"
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: How to do this?

Postby 94ge7j998 » 03 Aug 2013, 20:50

Thanks guys for the help, this part was all I needed to make it work:

Code: Select all
   else
   {
        Player.GlobalMessage("An incorrect command was given.");
   }
94ge7j998
 
Posts: 64
Joined: 26 Oct 2011, 20:49


Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 2 guests

cron