Reputation: 33
I am coding a rcon message tool for a game server in C#. However I've run across a error:
"The name 'm' does not exist in the current context"
By now you're shouting at your screen NOOB! and yes I admit I am; I have little real coding experience.
I've played with MFC C++ and OpenGL and I'm a fairly respected cod modder "script is gsc loosely based on c++" so I hope I can learn quickly, basically I tried to access an instance of b. outside of the main loop but it gave me the error:
The name b does not exist in the current context
so I made a new messages function that started a new connection in a new instance. Then I tried the access that in another function stopmessages()
but I still get the error.
Sorry for the newb question. I've googled long and hard about this and I just don't understand.
Here's my code - it uses Nini.dll
for config file access and BattleNET.dll
for access to rcon for the game -
#region
using System;
using System.Net;
using System.Text;
using BattleNET;
using Nini.Config;
#endregion
namespace BattleNET_client
{
internal class Program
{
private static void Main(string[] args)
{
bool isit_ok = true;
Console.OutputEncoding = Encoding.UTF8;
Console.Title = "rotceh_dnih's DayZ servermessages";
BattlEyeLoginCredentials loginCredentials = GetLoginCredentials();
Console.Title += string.Format(" - {0}:{1}", loginCredentials.Host, loginCredentials.Port);
IBattleNET b = new BattlEyeClient(loginCredentials);
b.MessageReceivedEvent += DumpMessage;
b.DisconnectEvent += Disconnected;
b.ReconnectOnPacketLoss(true);
b.Connect();
while (true)
{
startmessages();
string cmd = Console.ReadLine();
if (cmd == "exit" || cmd == "logout" || cmd == "quit")
{
Environment.Exit(0);
}
if (cmd == "restart")
{
stopmessages();
}
if (cmd == "startstuff")
{
startmessages();
}
if (b.IsConnected())
{
if (isit_ok)
{
}
isit_ok = false;
b.SendCommandPacket(cmd);
}
else
{
Console.WriteLine("Not connected!");
}
}
}
private static BattlEyeLoginCredentials GetLoginCredentials()
{
IConfigSource source = new IniConfigSource("server/admindets.ini");
string serverip = source.Configs["rconlogin"].Get("ip");
int serverport = source.Configs["rconlogin"].GetInt("port");
string password = source.Configs["rconlogin"].Get("rconpwd");
var loginCredentials = new BattlEyeLoginCredentials
{
Host = serverip,
Port = serverport,
Password = password,
};
return loginCredentials;
}
public static void startmessages()
{
BattlEyeLoginCredentials loginCredentials = GetLoginCredentials();
IBattleNET m = new BattlEyeClient(loginCredentials);
m.MessageReceivedEvent += DumpMessage;
m.DisconnectEvent += Disconnected;
m.ReconnectOnPacketLoss(true);
m.Connect();
IConfigSource messagesource = new IniConfigSource("messages/servermessages.ini");
int messagewait = messagesource.Configs["timesettings"].GetInt("delay");
string[] messages = messagesource.Configs["rconmessages"].Get("messages1").Split('|');
// for (;;)
// {
foreach (string message in messages)
{
Console.WriteLine(message);
m.SendCommandPacket(EBattlEyeCommand.Say,message);
System.Threading.Thread.Sleep(messagewait * 60 * 1000);
}
// }
}
public static void stopmessages()
{
m.Disconnect();
}
private static void Disconnected(BattlEyeDisconnectEventArgs args)
{
Console.WriteLine(args.Message);
}
private static void DumpMessage(BattlEyeMessageEventArgs args)
{
Console.WriteLine(args.Message);
}
}
}
Upvotes: 3
Views: 1747
Reputation: 29243
I'm assuming m should refer to this:
IBattleNET m = new BattlEyeClient(loginCredentials);
in the method startmessages()
. What you need to do is declare IBattleNET m outside the method body:
static IBattleNET m;
public static void startmessages()
{
//etc
Upvotes: 0
Reputation: 1079
what you could do is after you declared your class, so bevore the static void main declare your m value
internal class Program
{
IBattleNET m;
then in the startMessages method add
m = new BattlEyeClient(loginCredentials);
this will make the m value available to all the methods inside your class
Upvotes: 0
Reputation: 117
The stopmessages() method won't be able to access m as the variable m only exists within the startmessages() method
Upvotes: 1
Reputation: 107357
m
is declared in scope of static method startmessages
but then you are trying to use it in stopmessages
, where it is not in scope. You should move the variable to class scope, and define it as static (since your methods are static).
Hopefully your client app is single-threaded, otherwise you will need to consider thread safety issues as well.
Upvotes: 0
Reputation: 42363
Move the declaration of IBattleNET m
To outside the main
function and make it static:
static IBattleNet b;
Then in your main
you just do m = new BattlEyeClient(loginCredentials);
Upvotes: 0
Reputation: 176239
You need to put the declaration of m
into the class scope:
internal class Program
{
// declare m as field at class level
private static IBattleNET m;
private static void Main(string[] args)
{
....
}
public static void startmessages()
{
BattlEyeLoginCredentials loginCredentials = GetLoginCredentials();
// JUST SET THE VALUE HERE
m = new BattlEyeClient(loginCredentials);
m.MessageReceivedEvent += DumpMessage;
m.DisconnectEvent += Disconnected;
m.ReconnectOnPacketLoss(true);
m.Connect();
IConfigSource messagesource = new IniConfigSource("messages/servermessages.ini");
int messagewait = messagesource.Configs["timesettings"].GetInt("delay");
string[] messages = messagesource.Configs["rconmessages"].Get("messages1").Split('|');
// for (;;)
// {
foreach (string message in messages)
{
Console.WriteLine(message);
m.SendCommandPacket(EBattlEyeCommand.Say,message);
System.Threading.Thread.Sleep(messagewait * 60 * 1000);
}
// }
}
Upvotes: 4