The online racing simulator
Private Chat C#
(5 posts, started )
Quote from MariusMM :Still cant get it to work.

I tried something else today, but still isnt working the way I want it to...

Here is the current code:

case "!tr":
if (StrMsg.Length > 1)
{


int TrainerFound = 0;
foreach (clsConnection c in Connections)
{
if (c.IsTrainer == 1)
{
InSim.Send_MTC_MessageToConnection("Trainer Chat:", MSO.UCID, 0);
TrainerFound = 1;
InSim.Send_MTC_MessageToConnection(" " + MSO.Msg, MSO.UCID, 0);
}

if (TrainerFound == 0)
{
InSim.Send_MTC_MessageToConnection("No Permission :).", MSO.UCID, 0);
}
}
}
else
{
InSim.Send_MTC_MessageToConnection("No Permission :).", MSO.UCID, 0);
}
break;

Now when I use it, anyone who does !tr abc is able to see the message...

But I want it to show it to only people in the "Trainer group", and noone else.

And if you try to do this if your not in the group, you get the message "No Permission ."

But something is obviously very wrong.

Could someone please help me?

Marius.

Try re-arranging the braces around "if (c.IsTrainer == 1)", like above. As it is, it executes the "Trainer Chat:" message from the if statement correctly, and the curly braces after it will end the if statement, so it sends it regardless of if c.IsTrainer is true. Also using bool instead of int for TrainerFound would be slightly better practice, with either true or false. But that's just nit-picking
#2 - Stuff
Quote from MariusMM :
case "!tr":
if (StrMsg.Length > 1)
{
1. int TrainerFound = 0;
foreach (clsConnection c in Connections)
{
2. if (c.IsTrainer == 1)
3. InSim.Send_MTC_MessageToConnection("Trainer Chat:", MSO.UCID, 0);
4. {
TrainerFound = 1;
InSim.Send_MTC_MessageToConnection(" " + MSO.Msg, MSO.UCID, 0);
}

5. if (TrainerFound == 0)
{
InSim.Send_MTC_MessageToConnection("No Permission :).", MSO.UCID, 0);
}
}
}
else
{
6. InSim.Send_MTC_MessageToConnection("No Permission :).", MSO.UCID, 0);
}
break;


Well, you have some funky code in there..
First, item 1. What is TrainerFound supposed to do? You declared it outside the for loop so it seems like some kind of tracker for all items of the for loop. I think your intention was to set it when isTrainer is true but its not working like that. Just use isTrainer and an else block or something.
2. Using isTrainer is nice but it returns a boolean right? is* usually do. Its better practice to either check for true/false and not 1 or 0. Best to leave it off altogether. eg: if (c.isTrainer)
3. This is the only line that will be executed if c is a trainer. I think your intention was to execute the lines within the curly braces below but that will not happen.
4. The curly braces here have no effect. It is not part of the if statement. #3 is but since you left out the curly braces before that, only the next statement is executed and thats it.
5. TrainerFound will never be == 0. Because the curly braces on #4 have no effect, TrainerFound is always set to 1.
6. Why send "No permission ." when there is no message? Tell them no message not no permission..

Anyway, I think this is what you want.. Also, check the params for that Send_MTC_MessageToConnection function. I'm not sure of this but it seems like its all going to whoever sent the MSO, not everyone connected. Maybe something like: InSim.Send_MTC_MessageToConnection("Trainer Chat: " + MSO.Msg, c.UCID, 0);

case "!tr":
if (StrMsg.Length > 1)
{
foreach (clsConnection c in Connections)
{
if (c.IsTrainer)
InSim.Send_MTC_MessageToConnection("Trainer Chat: " + MSO.Msg, MSO.UCID, 0);
else
InSim.Send_MTC_MessageToConnection("No Permission :).", MSO.UCID, 0);
}
}
else
{
InSim.Send_MTC_MessageToConnection("What am I going to send?", MSO.UCID, 0);
}
break;

If you use Stuff's code but replace

InSim.Send_MTC_MessageToConnection("Trainer Chat: " + MSO.Msg, MSO.UCID, 0);


With what he also said


InSim.Send_MTC_MessageToConnection("Trainer Chat: " + MSO.Msg, c.UCID, 0);

it should work
-
(MariusMM) DELETED by MariusMM : Irrelevant
StrMsg is an array. By setting it to check if it's less than 16, it won't work if there's more than 16 words in the message. If you still have the rest of the MSO code from the cruise tutorial, try changing it to Msg. Msg is a normal string
-
(MariusMM) DELETED by MariusMM
You threw away modified string. Try with

Msg = Msg.Replace("tr", "");

and learn to use debugger

Private Chat C#
(5 posts, started )
FGED GREDG RDFGDR GSFDG