Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2015-08-25 02:31:09

ParadoxInTheVoid
Member
From: ̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺
Joined: 2015-02-18
Posts: 341

[Help] List problem

public class player
        {
            public int id { get; set; }
            public string username { get; set; }
            public string uid { get; set; }
            public int smileyid { get; set; }
            public int posX { get; set; }
            public int posY { get; set; }
            public bool isGod { get; set; }
            public bool isAdmin { get; set; }
            public bool canChat { get; set; }
            public int goldCoins { get; set; }
            public int blueCoins { get; set; }
            public bool isFriend { get; set; }
            public bool isClubMember { get; set; }
            public bool isMod { get; set; }
            public int team { get; set; }
            public int aura { get; set; }
            public uint chatColor { get; set; }
            public string badge { get; set; }
            public bool isCrewMember { get; set; }
            public int points { get; set; }
            public int wins { get; set; }
        }

       public static List<player> players = new List<player>();

That is the code I have, now for the add message:

try { players.Add(new player() { id = m.GetInt(0), username = m.GetString(1), uid = m.GetString(2), smileyid = m.GetInt(3), posX = m.GetInt(4) / 16, posY = m.GetInt(5) / 16, isGod = m.GetBoolean(6), isAdmin = m.GetBoolean(7), canChat = m.GetBoolean(8), goldCoins = m.GetInt(9), blueCoins = m.GetInt(10), isFriend = m.GetBoolean(11), isClubMember = m.GetBoolean(12), isMod = m.GetBoolean(13), team = m.GetInt(14), aura = m.GetInt(15), chatColor = m.GetUInt(16), badge = m.GetString(17), isCrewMember = m.GetBoolean(18), points = 0, wins = 0 }); } catch { }
                try { PlayerList.Items.Add(players[id].username); } catch { }

and left

try
                {
                    PlayerList.Items.Remove(players[id].username);
                    players.Remove(players[id]);
                }
                catch { }

very similar code with "c" and "m"...
Also 'id' is defined. Just not included here.

Now to explain the problem, when the bot connects it crashes because of the add message. The error message says:

System.ArgumentOutOfRangeException occured in mscorelib.dll but was not handled by user code.

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

When it comes to the add message, the error comes from PlayerList.Items.Add(players[id].username);


signature.png

Offline

#2 2015-08-25 04:07:37, last edited by hummerz5 (2015-08-25 04:08:43)

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: [Help] List problem

Your problem is likely wherever "id" is defined -- you deftly omitted that relevant line.

"Index was out of range" is rather straightforward. Somewhere, you're indexing a "word" that doesn't exist. So, let's look at indexes.
The line you raise as the error is

PlayerList.Items.Add(players[id].username);

And the only indexing here is by ID. So, you determined it incorrectly.

In this case, I should point out that you're assuming a vague "ID" as the variable. With no more information to base this guess from, I question if you have mixed up your definitions of EE room user ID and list element (array) ID.
(Simply put, EE gives 1020, 1021, 1022) but your list starts with (0, 1, 2)...

coding pro tip: please break that long lien into multiple. You can put line breaks in that initializing block...
I admit I have that bad practice, too, but that's somewhat bothersome to look at -- at first I questioned if you had accidentally gotten braces mixed up somehow.

Offline

#3 2015-08-25 04:09:51, last edited by capasha (2015-08-25 04:24:53)

capasha
Member
Joined: 2015-02-21
Posts: 4,066

Re: [Help] List problem

What you need is to loop through the players and check if the id exists. Then do something.

public static List<player> players = new List<player>();

static int getUserID(int id)
{
for (int i = 0; i < players.Count; i++)
    {
        if (players[i].id == id)
            {
                return i;
            }
    }
    return -1;
}
		
switch (m.Type) 
{
	case "add":
	players.Add(new players() { id = m.GetInt(0), username = m.GetString(1) });
	break;
	case "left":
	players.Remove(players[getUserID(m.GetInt(0))]);
	break;
	case "say":
	var id = getUserID(m.GetInt(0));
	Console.WriteLine("PLayer " + players[id].username + " said something");
	break;
}



Adding ID to a dictionary makes it easier without need of a loop.

public static Dictionary<int,player> players = new Dictionary<int,player>();

switch (m.Type) 
{
	case "add":
	players.Add(m.GetInt(0),new player() { username = m.GetString(1) });
	break;
	case "left":
	players.Remove(m.GetInt(0));
	break;
	case "say":
	Console.WriteLine("PLayer " + players[m.GetInt(0)].username + " said something");
	break;
}

Offline

#4 2015-08-25 04:28:15

ParadoxInTheVoid
Member
From: ̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺̺
Joined: 2015-02-18
Posts: 341

Re: [Help] List problem

capasha wrote:

-snip-

Thanks. I haven't work enough with dictionaries. Will storing isGod and stuff like that still work fine or do I have to fiddle with that? It seems to work so far but I want this to be scrubbed clean.


signature.png

Offline

#5 2015-08-25 04:31:16

capasha
Member
Joined: 2015-02-21
Posts: 4,066

Re: [Help] List problem

ParadoxInTheVoid wrote:
capasha wrote:

-snip-

Thanks. I haven't work enough with dictionaries. Will storing isGod and stuff like that still work fine or do I have to fiddle with that? It seems to work so far but I want this to be scrubbed clean.

I updated my post. Look again how you use a list.

Offline

capasha1440473476534193

Board footer

Powered by FluxBB

[ Started around 1732466938.0163 - Generated in 0.078 seconds, 13 queries executed - Memory usage: 1.44 MiB (Peak: 1.57 MiB) ]