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 2017-07-13 00:36:30

italkalotfromee
Member
Joined: 2015-05-08
Posts: 117

Getting username..

As of right now, I'm trying to catch the players username using a field parameter. As i know of the name is a string, yet I've tried converting it and it still don't work.. <(^.^)>

Any ideas?!?

m-_ZZy8TRHabBDLnl-3X-A.png


FluxBB bbcode test

Offline

#2 2017-07-13 00:39:44, last edited by hummerz5 (2017-07-13 00:55:45)

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

Re: Getting username..

variable assignments aren't quite like math. When you say "a == b" in math, "b == a" is also true. Commutative property, I think.

by the way, are you perhaps getting some sort of stack overflow because you're constructing an object in its very conception?

we need to talk.

so, public class Player works pretty well. But, if you make the member? static, then the information is constant throughout all instances. So, when you make more than one player, it will have the same "name." You can see how this breaks down for individual players. Using a static name, you only can have one name. So, TOOP, Jesse, and NVD would all end up being something like NVD.
tl;dr: set to nonstatic

also, objects have members. You're running a bit fast here. Slowing down, we see that saying a player... has a name player... has a name player... doesn't quite make sense in this situation. If the player "name" is a string, then you should say that it is when you declare the class. so "public string name" would be a bit more apt

Your line that you have indicated with "doesn't work" is a bit odd. You should reconsider how the equal sign works. You retrieved the latest data, so-to-speak, and placed it in playersname. using the assignment operator in this situation would then take that hard-earned data and erase it, overwriting with whatever old data you had in the player object.

also, we need to talk about how objects work. You're storing them in "players" which is cool, by ID which is cool, but you're not bothering to retrieve it for your chat command. In "add", create the player instance, write the important data down. Store it as you are. When you're retrieving, check to see if the user ID (usually m.GetInt(0)) is in the dictionary (players.ContainsKey()) and then work with that instance to customize to the username or whatever you need

Offline

Wooted by:

#3 2017-07-13 00:42:07, last edited by Vinyl Melody (2017-07-13 00:50:25)

Vinyl Melody
Formerly BananaMilkShake
Joined: 2016-06-19
Posts: 616

Re: Getting username..

public class player {
    public string username { get; set; }
}
//
public static Dictionary<int, player> players = new Dictionary<int, player>();
// add
if(!players.ContainsKey(e.GetInt(0)) {
    string playername = e.GetString(1);
    players.Add(e.GetInt(0), new player() {username = playername});
}
// say
if(e.GetString(1) == "t") {
    con.Send("say", "Hello, " + players[e.GetInt(0)].username);
}

:/

I think the problem in your code is that you're trying to get the username of a player without a key (say). Player class is wrong and that you're getting the player's username(?) before the player gets added to the Dictionary (add).
(Actually, if you only get the username in the Dictionary. Just do a Dictionary<int, string>)


cb0de83627.png
Thanks to: Ernesdo (Current Avatar), Zoey2070 (Signature)

Very inactive, maybe in the future, idk.

Offline

#4 2017-07-13 00:50:12

italkalotfromee
Member
Joined: 2015-05-08
Posts: 117

Re: Getting username..

iSo3PnuJRIiTBInkQi7PWQ.png

Not really.. Just recalls the class in which it's in and the name, which in this case is the user. But it doesn't refer to the user as "italkalot" or "usernamehere" <- if that make sense xD


FluxBB bbcode test

Offline

#5 2017-07-13 00:51:13

italkalotfromee
Member
Joined: 2015-05-08
Posts: 117

Re: Getting username..

Vinyl Melody wrote:

:/

I think the problem in your code is that you're trying to get the username of a player without a key (say). Player class is wrong and that you're getting the player's username(?) before the player gets added to the Dictionary (add).

The name is a field and can't be in the players dictionary as that needs a string or int value.


FluxBB bbcode test

Offline

#6 2017-07-13 00:53:44, last edited by Latif (2017-07-13 00:55:34)

Latif
Member
From: The Netherlands
Joined: 2015-03-13
Posts: 1,206

Re: Getting username..

italkalotfromee wrote:

As of right now, I'm trying to catch the players username using a field parameter. As i know of the name is a string, yet I've tried converting it and it still don't work.. <(^.^)>

Any ideas?!?

https://image.prntscr.com/image/m-_ZZy8 … l-3X-A.png

Player class:

public class player
{
    public string Name { get; set; }

    public player (string name)
    {
        Name = name;
    }
}

"Add" message

string playername = m.GetString(1);
players.Add(m.GetInt(0), new player(playername));

EDIT: Things happened when I wrote this lol

Offline

#7 2017-07-13 00:56:18

Xfrogman43
Member
From: need to find a new home
Joined: 2015-02-15
Posts: 4,174

Re: Getting username..

you can just use players[m.GetInt(0)] in add and thats their name


zsbu6Xm.png thanks zoey aaaaaaaaaaaand thanks latif for the avatar

Offline

#8 2017-07-13 00:57:15

italkalotfromee
Member
Joined: 2015-05-08
Posts: 117

Re: Getting username..

Latif wrote:
italkalotfromee wrote:

As of right now, I'm trying to catch the players username using a field parameter. As i know of the name is a string, yet I've tried converting it and it still don't work.. <(^.^)>

Any ideas?!?

https://image.prntscr.com/image/m-_ZZy8 … l-3X-A.png

Player class:

public class player
{
    public string name { get; set; }

    public player (string name)
    {
        Name = name;
    }
}

"Add" message

string playername = m.GetString(1);
players.Add(m.GetInt(0), new player(playername));

EDIT: Things happened when I wrote this lol

Well i know that works, but i want it a field parameter and not a string..


FluxBB bbcode test

Offline

#9 2017-07-13 00:59:08, last edited by Latif (2017-07-13 00:59:57)

Latif
Member
From: The Netherlands
Joined: 2015-03-13
Posts: 1,206

Re: Getting username..

Vinyl Melody wrote:
Latif wrote:
italkalotfromee wrote:

As of right now, I'm trying to catch the players username using a field parameter. As i know of the name is a string, yet I've tried converting it and it still don't work.. <(^.^)>

Any ideas?!?

https://image.prntscr.com/image/m-_ZZy8 … l-3X-A.png

Player class:

public class player
{
    public string name { get; set; }

    public player (string name)
    {
        Name = name;
    }
}

"Add" message

string playername = m.GetString(1);
players.Add(m.GetInt(0), new player(playername));

EDIT: Things happened when I wrote this lol

lol my code is bettur You're missing the "say" message. Look at his code on it.

It looks the same and this isn't really complicated. Btw I'm lazy and he should figure it out himself and google a bit. Everyone should know how to make a class.

EDIT: I hate my phone

Offline

Wooted by:

#10 2017-07-13 00:59:28, last edited by Xfrogman43 (2017-07-13 01:00:38)

Xfrogman43
Member
From: need to find a new home
Joined: 2015-02-15
Posts: 4,174

Re: Getting username..

MGAi8le.png
ja1dkv7.png
xxTq4Jl.png


zsbu6Xm.png thanks zoey aaaaaaaaaaaand thanks latif for the avatar

Offline

Wooted by:

#11 2017-07-13 01:01:44

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: Getting username..

I think you should have something like this for the player class:

public class player
{
    public string name;
    
    public player(string name)
    {
        this.name = name;
    }
    // first, this shouldn't be static because it is a property of the player, static means it should be shared between players (every player doesn't have the same name)
    // second, the name is a string, not a player (you need to specify the type of the data, not what the data is stored in)
    // third, although you can specify the name in the {} when you create a player, it's easier to have the constructor set it for you
}

This would mean you would create a new player like this:

players.Add(m.GetInt(0), new player(m.GetString(1));

// You can then later use players[id].name to get their name

Offline

Wooted by: (2)

#12 2017-07-13 01:03:08

Latif
Member
From: The Netherlands
Joined: 2015-03-13
Posts: 1,206

Re: Getting username..

destroyer123 wrote:

I think you should have something like this for the player class:

public class player
{
    public string name;
    
    public player(string name)
    {
        this.name = name;
    }
    // first, this shouldn't be static because it is a property of the player, static means it should be shared between players (every player doesn't have the same name)
    // second, the name is a string, not a player (you need to specify the type of the data, not what the data is stored in)
    // third, although you can specify the name in the {} when you create a player, it's easier to have the constructor set it for you
}

This would mean you would create a new player like this:

players.Add(m.GetInt(0), new player(m.GetString(1));

// You can then later use players[id].name to get their name

Exact same code! Such coincidence!

Offline

Wooted by:

#13 2017-07-13 01:08:27

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

Re: Getting username..

Gee, we all should teach a class on coding, 10 replies in half an hour... a little community tutorial of sorts, though that's neither here nor there

I think we covered all the peculiarities of the class structure and use, but our explanations might be a  tad lacking

Offline

Wooted by: (3)

#14 2017-07-13 01:08:38

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: Getting username..

Latif wrote:

Exact same code! Such coincidence!

It just took me ages to write because I'm on my phone //forums.everybodyedits.com/img/smilies/sad
When I started there were only 2 replies...

Offline

Wooted by: (3)

#15 2017-07-13 03:53:36

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: Getting username..

Why don't you include Id in your player classes
Player list is useless without ids

Offline

Wooted by:

#16 2017-07-13 10:47:53, last edited by LukeM (2017-07-13 10:48:29)

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: Getting username..

Gosha wrote:

Why don't you include Id in your player classes
Player list is useless without ids

Not if the id is the key, because then you wouldn't be able to get a random players id unless you had the id in the first place.

I usually have two seperate dictionaries, one with ids as the keys, and usernames as the values, and another with usernames as the key, and player objects as the value. This means you can convert the id to a username when you receive the message, then after that you don't need the id

Offline

#17 2017-07-13 11:44:55

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: Getting username..

Btw your problem Is much bigger than getting the usernames
You should learn basic programming because this code

Var playername = m. GetString(1);
playername = player.name.ToString();

Doesn't make sense

You get the the name and then trying to get a player in a player and convert it to string and override legit player's name
Like
Wtf lol

Offline

#18 2017-07-13 12:13:24, last edited by LukeM (2017-07-13 12:16:28)

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: Getting username..

Gosha wrote:

Btw your problem Is much bigger than getting the usernames
You should learn basic programming because this code

Var playername = m. GetString(1);
playername = player.name.ToString();

Doesn't make sense

You get the the name and then trying to get a player in a player and convert it to string and override legit player's name
Like
Wtf lol

You have to learn somehow, and just trying to do something you want to do, learning as you go isnt a particularly bad way... Alright it might be a bit slower than other ways, but it can sometimes help with understanding why you actually do things

Edit: Just incase italkalotfromee doesnt understand what is wrong with it, it should be the other way round:

string playername = m.GetString(1);
player.name = playername;

Offline

#19 2017-07-13 13:49:05

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: Getting username..

You should have class player with an Id, name and constructor.

some stuff
public class Player 
{
      public int Id;
      public string Name;
      public Player(int id, string name)
      {
            Id = id;
            Name = name; 
      }
}

Also in main class you should have a Dictionary of players  where Key Is an Id and Value is a player

public static Dictionary<int,Player> Players = new Dictionary<int,Player>();

Now you can add players into your "list of players" (dictionary)

if(m.Type == "add")
{
     var id = m.GetInt(0);   // id of the player 
     var name = m.GetString(1); // name of the player
     var player = new Player(id, name); //create new player with given id and name
     Players.Add(id,player); // add new player to player list at the position of id
}

Same thing with "left"

if(m.Type == "left")
{
    var id = m.GetInt(0); // id of the player who left
    Players.Remove(id); // remove player from player list

}

Now when you receive a message with Id in it (like "say") you can get the name of the player.


if(m.Type == "say")
{
    var id = m.GetInt(0); // id of the player who said something
    var player = Players[id]; // player who said something 
    var message = m.GetString(1); // message player wrote 
    // now you can output it to the console 
    Console.WriteLine(player.name + ": " + message);
}

Output will be something like this
Gosha: hi noobs

Warning!  Please note the you don't get information about yourself in "add" message. If your bot say something - you'll crash.
To fix it you need to do this:

if(m.Type == "init")
{
    var id = m.GetInt(5);   // your id
     var name = m.GetString(13); // your name 
     var player = new Player(id, name); 
     Players.Add(id,player);
     //please note that I didn't check Ee protocol, I am sure index of messages might be different but 5 and 13
}

This code will add you to the list of players


P.s. I wrote this all on my phone it took hell a lot of time , Idk why I wrote this guide but I hope it will help you.

Offline

Wooted by: (2)

#20 2017-07-13 15:41:49

Latif
Member
From: The Netherlands
Joined: 2015-03-13
Posts: 1,206

Re: Getting username..

It's funny how everyone want to post (including me) because we never get questions like this.

Offline

Wooted by:

#21 2017-07-13 23:42:50

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: Getting username..

Latif wrote:

It's funny how everyone want to post (including me) because we never get questions like this.

I decided to hold back since there were already a dozen posts xD

Offline

#22 2017-07-14 00:04:49, last edited by John (2017-07-14 00:06:31)

John
Member
Joined: 2019-01-11
Posts: 2,011

Re: Getting username..

den3107 wrote:

<snip>
I decided to hold back since there were already a dozen posts xD

I did too

EDIT:
I encourage everyone to place their code in images. Let's not run on copypaste pls


PW?scale=2

Offline

#23 2017-07-14 03:49:57

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: Getting username..

Emalton wrote:

I encourage everyone to place their code in images. Let's not run on copypaste pls

 

I think they have better chances of understanding things by copypasta with comments

Offline

#24 2017-07-14 11:45:51

den3107
Member
From: Netherlands
Joined: 2015-04-24
Posts: 1,025

Re: Getting username..

Emalton wrote:

I encourage everyone to place their code in images. Let's not run on copypaste pls

All he'd do is type it over blindly anyway. There might be a bigger chance of him thinking a bit more about the solution, but most of the time there'd be pretty much no difference.

Offline

#25 2017-07-14 11:51:53

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: Getting username..

den3107 wrote:
Emalton wrote:

I encourage everyone to place their code in images. Let's not run on copypaste pls

All he'd do is type it over blindly anyway. There might be a bigger chance of him thinking a bit more about the solution, but most of the time there'd be pretty much no difference.

I guess the best thing to do is not to give them any directly usable code at all, just to tell them what to do, and maybe a few lines for things that are hard to explain

Offline

Gosha1500030506667880

Board footer

Powered by FluxBB

[ Started around 1732705430.084 - Generated in 0.119 seconds, 12 queries executed - Memory usage: 1.87 MiB (Peak: 2.17 MiB) ]