Finding a specific name from a list using a Foreach check
I've been working on a new event idea which involves 2 people being paired up and then parts of each char being checked against the other, I have everything working fine apart from 1 of the checks that selects the players opponent from the list by checking the names in the list, It only selects the first person added to the list and then returns a fail rather than checking the whole list and returning the correct match if they are still there, I have checked and players are being added fine and all player names are in the list when it reaches the "foreach" check, & matchopponent name is correct & in the list.
Anyone know why it only selects the first person who was added to the list rather than searching the list for the correct player and selecting them?
Code:
public static List<Game.Entity> SignedUp = new List<Game.Entity>()
if (client.Entity.ItemEquipped == true)
{
foreach (var c in Kernel.SignedUp) //All players needed are in the list but Only pulls the 1st player who was added to the list rather than the player it should, Opponent confirmed as in list
{
if (c.Name == client.Entity.MatchOpponent) //Selects first person added to the list & Calls a fail even tho the opponents name is in the list
{
It's very difficult for us to say why it only iterates once without seeing the actual code.
When doing a foreach loop (and if you've verified that the collection DOES contain more than one entry) then whats inside that loop controls its logic.
It's possible you break out or return from the loop prematurely.
It would help us if you actually explained what it is you wanted to do. How are you wanting to match up players? Finding two signed up players who are the closest in level/bp/same class/etc?
You're best using a LINQ query that fits your parameters and pulling the two closest matches from that collection to start a match (and if it fails you wait for more people to enter. You could even slowly make it less demanding if no one new signs up)
Below is the current code im using which contains the foreach loop, Its currently running it from an npc, I can confirm all characters needed are in the Collection, The opponent is already determined before the below code is activated & "client.Entity.MatchOpponent" contains the clients opponents name which the foreach loop is supposed to search for, The names here are also checked and definatly correct.
Code:
Kernel.CardMatch.Add(client.Entity); //players added to the collection prior to the next code
case 1:
{
if (client.Entity.CardEquipped == true)
{
try
{
foreach (var c in Kernel.CardMatch)
{
if (c.Name == client.Entity.CardMatchOpponent) //This is where its failing and returns "Your opponent has resigned.", Client names needed are def in the list, & the clients opponents name is def fine n correct
{
if (c.CardEquipped == true)
{
dialog.Text("Choose your move.");
dialog.Option("1.", 1);
dialog.Option("2.", 2);
dialog.Option("3.", 3);
dialog.Option("4.", 4);
dialog.Option("5.", 5);
dialog.Send();
break;
}
else
{
dialog.Text("Your opponent hasent selected a card.");
dialog.Option("Ok.", 255);
dialog.Send();
break;
}
}
else
{
dialog.Text("Your opponent has resigned.");
dialog.Option("Okies.", 255);
dialog.Send();
break;
}
}
}
catch { }
}
else
{
dialog.Text("You havent equipped your battle card.");
dialog.Option("Okies.", 255);
dialog.Send();
break;
}
}
break;
Well your code gets the first name in the collection, if it doesnt match then it returns resigned and breaks from the loop.
You need to insert your opponent has resigned AFTER the for each loop, if the name was not found.
So you could insert a success boolean, and when the name matches set it to true.
After the foreach loop, check if success is true, if it isnt, then you would execute the opponent has resigned.
Well, like they said, in the npc dialogue. You're using foreach and it will only check out the first character only. This is because if the name does does match, it will give you the controls and so on, and break out. Ending the npc dialogue. but this is happening even if the first character conditions is not matching the name. Which is where the resigned opponent begins. And you also had a break there. That itself is stopping the whole loop so it will only check the very first character of your list.
What you could've done is make a boolean, and if name matches, just 1 is enough. Then set bool to true, and add the dialogues afterwards using if (bool) //true
Code:
Kernel.CardMatch.Add(client.Entity); //players added to the collection prior to the next code
case 1:
{
if (client.Entity.CardEquipped == true)
{
try
{
//change the object to whatever var c is
object cc = null;
foreach (var c in Kernel.CardMatch)
{
if (c.Name == client.Entity.CardMatchOpponent)
{
cc = c;
}
}
if (cc != null)
{
if (cc.Name == client.Entity.CardMatchOpponent)
{
if (cc.CardEquipped == true)
{
dialog.Text("Choose your move.");
dialog.Option("1.", 1);
dialog.Option("2.", 2);
dialog.Option("3.", 3);
dialog.Option("4.", 4);
dialog.Option("5.", 5);
dialog.Send();
break;
}
else
{
dialog.Text("Your opponent hasent selected a card.");
dialog.Option("Ok.", 255);
dialog.Send();
break;
}
}
else
{
dialog.Text("Your opponent has resigned.");
dialog.Option("Okies.", 255);
dialog.Send();
break;
}
}
else
{
dialog.Text("Your opponent has logged off.");
dialog.Option("Okies.", 255);
dialog.Send();
break;
}
}
catch { }
}
else
{
dialog.Text("You havent equipped your battle card.");
dialog.Option("Okies.", 255);
dialog.Send();
break;
}
}
break;
Mit Foreach mehrere Daten in MySQL eintragen^ 07/12/2012 - Web Development - 4 Replies Hallöchen community,
komme gerade nicht weiter, steh irgendwie aufm Schlauch..
Ich habe folgendes:
$xml = @simplexml_load_file($xmllink);
foreach($xml->newsitems->newsitem as $news) {
$title = $news->title; // Titel der News
$url = $news->url; // Link zu den News
EU2 WTB list check plz 01/13/2011 - Archlord Trading - 11 Replies ok so i want to play on EU2 again
here is what i need
Ranger cf :
~ cuirass whit extra sb
~tights whit xtra resistance thanx to gelford
~bracers extra att speed if possible
~boots xtra block or mov speed if possible
uniques : 2 blodes
Twelvesky 2 - Aeria - Aion - Huge List Check it out 06/19/2010 - Trading - 3 Replies Hey guys i quit twelvesky , this is the list of everything i have lying around , i accept paypal ,
owl 0%
falc 100pct
8 Luxuary Chests
M33 RARE JIN ARMOUR 99PCT CS 12
M33 RARE SPEAR 120PCT M33
M33 RARE BOOTS 93PCT CS12
M33 RARE CAPE CS10 96 PCT
1.6 BIL SILVERS.
Direct a foreach at Char in Map? 04/01/2010 - CO2 Private Server - 8 Replies I've been working at a hunt quest that teleports chars to a map where they must kill a monster get a key find the treasure box, open it then teleport back to TC.
This is what I have but it directs the tele to all characters in game
foreach (Game.Character C in Game.World.H_Chars.Values)
{
Game.World.SendMsgToAll("SYSTEM", "The Treasure has been found. Better luck next time.", 2011, 0);
...