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.
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?!?
Offline
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
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>)
Thanks to: Ernesdo (Current Avatar), Zoey2070 (Signature)
Very inactive, maybe in the future, idk.
Offline
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
Offline
:/
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.
Offline
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?!?
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
you can just use players[m.GetInt(0)] in add and thats their name
thanks zoey aaaaaaaaaaaand thanks latif for the avatar
Offline
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?!?
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..
Offline
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?!?
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
thanks zoey aaaaaaaaaaaand thanks latif for the avatar
Offline
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
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
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
Why don't you include Id in your player classes
Player list is useless without ids
Offline
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
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
Btw your problem Is much bigger than getting the usernames
You should learn basic programming because this codeVar 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
You should have class player with an Id, name and constructor.
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
It's funny how everyone want to post (including me) because we never get questions like this.
Offline
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
<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
Offline
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
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
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
[ Started around 1732385053.8792 - Generated in 0.227 seconds, 12 queries executed - Memory usage: 1.87 MiB (Peak: 2.17 MiB) ]