i’ve asked a question before but sounds that i didn’t explain it in good way so here is the codes of the buggest part
class packet handler (receive information from packet and store them at x,y and there is struct which accept 2 ushort ref so once i define an object it change the value of the variables i send)
public class PacketHandler : GUI
{
GameUser role;
public PacketHandler(GameUser who)
{
role = who;
}
ushort Actualx, Actualy;
public PacketHandler(ref ushort x ,ref ushort y)
{
x = Actualx; y = Actualy;
}
public unsafe void HandleServer(byte[] data)
{
.
.
.
case 10010:
{
if (BitConverter.ToUInt16(data, 8) == 1002)
{
Actualx = BitConverter.ToUInt16(data, 24);
Actualy = BitConverter.ToUInt16(data, 26);
}
break;
}
this get the value from packets , store them at actualx , actualy preparing to give them to any ushort ref come at the defining object parameter
here is other class
public class ClientBase
{
GameUser role2;
public ClientBase(GameUser role)
{
role2 = role;
Thread T = new Thread(HuntThread) { Name = "Hunt Thread" };
T.Start(this);
}
.
.
.
public void HuntThread(object Sender)
{
ClientBase Client = Sender as ClientBase;
while (true)
{
Monster Target = GetNextkill();
if (Target != null)
{
Thread.Sleep(1000);
ProxyParadise.Network.Packets.PacketHandler getxandy = new ProxyParadise.Network.Packets.PacketHandler(ref X, ref Y);
ProxyParadise.Network.Packets.PacketStructure ps = new ProxyParadise.Network.Packets.PacketStructure();
.
.
.
and the real problem is i find zero at x and y , when i trace it i find zero at actualx,actualy however im sure they got a value , so i think im doing something stupid
so all in all , if u can’t help me at this code or figure out what i mean then please tell me a proper way to get values from another class while using threading , thanks everyone , please some mod delete my old question , i rephrase it now in better way
Here we go:
You are assigning the values from the fields (which are initially zero) to the parameters, rather than the values from the parameters to the fields:
Note I removed the
reftoo; that would have had the nasty side-effect (in your original code) of wiping the by-ref variables you passed in, since the fields are zero initially.However, what you can’t do is store a “reference to a
ushort” as a field. If you need that, the best thing to do is to drop the value onto a class, and refer to the object:then any number of callers can have references to the same
Fooobject, and therefore updating one is updating all. Frankly, yourPacketHandlerwould already serve in place of thisFoo; the trick would be for multiple pieces of code to have a reference to the handler, and get their.ActualX/.ActualYfrom the object.