Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Private Server
You last visited: Today at 00:16

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Finding a specific name from a list using a Foreach check

Discussion on Finding a specific name from a list using a Foreach check within the CO2 Private Server forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Feb 2006
Posts: 41
Received Thanks: 2
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
         {
hacksaw is offline  
Old 10/05/2013, 01:51   #2
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,376
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)
pro4never is offline  
Old 10/05/2013, 03:07   #3
 
elite*gold: 0
Join Date: Feb 2006
Posts: 41
Received Thanks: 2
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;
hacksaw is offline  
Old 10/05/2013, 03:16   #4
 
_Emme_'s Avatar
 
elite*gold: 1142
Join Date: Aug 2006
Posts: 2,464
Received Thanks: 1,161
The client.Entity.CardMatchOpponent is NOT the first value in the list Kernel.CardMatch.

Whenever it falls to the else, it breaks the loop and thus only iterates once.
_Emme_ is offline  
Thanks
1 User
Old 10/05/2013, 03:17   #5
 
elite*gold: 0
Join Date: Feb 2006
Posts: 726
Received Thanks: 271
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.

Hope you can understand that.
Aceking is offline  
Thanks
1 User
Old 10/05/2013, 06:57   #6
 
-Shunsui-'s Avatar
 
elite*gold: 0
Join Date: Apr 2008
Posts: 1,152
Received Thanks: 321
LINQ
-Shunsui- is offline  
Thanks
2 Users
Old 10/05/2013, 11:15   #7
 
shadowman123's Avatar
 
elite*gold: 0
Join Date: Aug 2007
Posts: 1,525
Received Thanks: 230
i understood Nothing to be Honest
shadowman123 is offline  
Old 10/05/2013, 13:52   #8
 
drakejoe67's Avatar
 
elite*gold: 0
Join Date: Feb 2008
Posts: 322
Received Thanks: 121
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;
drakejoe67 is offline  
Thanks
1 User
Old 10/05/2013, 15:02   #9
 
elite*gold: 0
Join Date: Feb 2006
Posts: 41
Received Thanks: 2
Thumbs up

Thanks for all the info and suggestions guys, Can see where i was going wrong now, All sorted
hacksaw is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
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); ...



All times are GMT +2. The time now is 00:16.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.