FirstGearGames
FirstGearGames
CC#
Created by FirstGearGames on 3/26/2023 in #help
❔ Socket.Connected for listening sockets?
The Socket class contains a Connected property, but not Listening. I'm trying to check for a break in connectivity when a socket is listening. EG: if a client connects to a server then Connected is true, and will become false when the client disconnects. I'm not finding an equivalent for when the server is listening, or if the socket is somehow dropped.
6 replies
CC#
Created by FirstGearGames on 3/11/2023 in #help
❔ Passing around thread to perform work on?
I'd like to call a method that runs on the main thread but offloads a portion of it's work onto a passed in thread. Here's a stripped down version of what I'm looking to accomplish...
private BackgroundWorker _worker = new BackgroundWorker();
private ConcurrentBag<AResult> _results = new ConcurrentBag<AResult>();
public enum AResult
{
Passed,
Failed,
Error
}
public void DoThing(BackgroundWorker worker, ConcurrentBag<AResult> bag)
{
//Nevermind this could all be done on worker thread, pretend some must be done on main.
//Main thread...
float a = 10f;
float b = 20f;

//Do this in worker....
if (a == b)
bag.Add(AResult.Passed);
else if (a > b)
bag.Add(AResult.Failed);
else
bag.Add(AResult.Error);
}

//This is called on the main thread.
private void DoCalculations()
{
int count = _collection.Count;
for (int i = 0; i < count; i++)
_collection[i].DoThing(_worker, _results);
}
//Called regularly on the main thread.
private void HandleResults()
{
while (_results.TryTake(out AResult result))
{ }
}
private BackgroundWorker _worker = new BackgroundWorker();
private ConcurrentBag<AResult> _results = new ConcurrentBag<AResult>();
public enum AResult
{
Passed,
Failed,
Error
}
public void DoThing(BackgroundWorker worker, ConcurrentBag<AResult> bag)
{
//Nevermind this could all be done on worker thread, pretend some must be done on main.
//Main thread...
float a = 10f;
float b = 20f;

//Do this in worker....
if (a == b)
bag.Add(AResult.Passed);
else if (a > b)
bag.Add(AResult.Failed);
else
bag.Add(AResult.Error);
}

//This is called on the main thread.
private void DoCalculations()
{
int count = _collection.Count;
for (int i = 0; i < count; i++)
_collection[i].DoThing(_worker, _results);
}
//Called regularly on the main thread.
private void HandleResults()
{
while (_results.TryTake(out AResult result))
{ }
}
I'm not 100% sure on the best way to go about this.
5 replies
CC#
Created by FirstGearGames on 2/11/2023 in #help
❔ Optimization thoughts on a loop.
Looking for potential ways to speed up this operation. Any thoughts? The entries of each HashSet will change regularly and this is iterated frequently.
public class MyClass { }
//This will aways have 4 entries.
private List<HashSet<MyClass>> _regions = new List<HashSet<MyClass>>();
private void Test()
{

for (int i = 0; i < 2000; i++)
{
CheckIndex(0);
if (i % 2 == 0)
CheckIndex(1);
if (i % 3 == 0)
CheckIndex(2);
if (i % 4 == 0)
CheckIndex(3);
}

//Logic
void CheckIndex(int index)
{
HashSet<MyClass> classes = _regions[index];
//do things to classes.
}
}
public class MyClass { }
//This will aways have 4 entries.
private List<HashSet<MyClass>> _regions = new List<HashSet<MyClass>>();
private void Test()
{

for (int i = 0; i < 2000; i++)
{
CheckIndex(0);
if (i % 2 == 0)
CheckIndex(1);
if (i % 3 == 0)
CheckIndex(2);
if (i % 4 == 0)
CheckIndex(3);
}

//Logic
void CheckIndex(int index)
{
HashSet<MyClass> classes = _regions[index];
//do things to classes.
}
}
8 replies
CC#
Created by FirstGearGames on 9/21/2022 in #help
Multiple types in a params without casting?
Feel like I'm hoping for an unrealistic expectation, but is it possible to have multiple types in a params without casting to avoid GC? eg...
public void DoSomething(params object[] objs)
{
for (int i = 0; i < objs.Length; i++)
{
if (i % 2 == 0)
Debug.Log((int)objs[i]);
else
Debug.Log((string)objs[i]);
}
}
public void DoSomething(params object[] objs)
{
for (int i = 0; i < objs.Length; i++)
{
if (i % 2 == 0)
Debug.Log((int)objs[i]);
else
Debug.Log((string)objs[i]);
}
}
7 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
I've never in my career of coding have seen the behavior I'm about to describe so I'm hoping to learn something new... this is Unity3D btw... In my code I am iterating a collection built on creation. I've confirmed that the objects are not being rebuilt at any time, and also the GetHashCode shown in this segment returns the same every time. The code below does not really matter a whole lot, just showing my work flow.
for (int i = 0; i < _rigidbodyDatas.Count; i++)
{
Debug.Log("Changing kinematic on " + _rigidbodyDatas[i].GetHashCode() + " to " + isKinematic);
if (!_rigidbodyDatas[i].SetIsKinematic(isKinematic))
{
_rigidbodyDatas.RemoveAt(i);
i--;
}
}
for (int i = 0; i < _rigidbodyDatas.Count; i++)
{
Debug.Log("Changing kinematic on " + _rigidbodyDatas[i].GetHashCode() + " to " + isKinematic);
if (!_rigidbodyDatas[i].SetIsKinematic(isKinematic))
{
_rigidbodyDatas.RemoveAt(i);
i--;
}
}
This is the SetIsKinematic method....
public bool SetIsKinematic(bool isKinematic)
{
if (_rigidbody == null)
return false;

if (isKinematic)
{
_velocity = _rigidbody.velocity;
Debug.Log("Cached velocity as " + _velocity + " on " + GetHashCode());
_angularVelocity = _rigidbody.angularVelocity;
_rigidbody.isKinematic = true;
}
else
{
Debug.Log("Read velocity as " + _velocity + " on " + GetHashCode());
_rigidbody.isKinematic = false;
_rigidbody.AddForce(_velocity, ForceMode.Impulse);
_rigidbody.AddTorque(_angularVelocity, ForceMode.Impulse);
}

return true;
}
public bool SetIsKinematic(bool isKinematic)
{
if (_rigidbody == null)
return false;

if (isKinematic)
{
_velocity = _rigidbody.velocity;
Debug.Log("Cached velocity as " + _velocity + " on " + GetHashCode());
_angularVelocity = _rigidbody.angularVelocity;
_rigidbody.isKinematic = true;
}
else
{
Debug.Log("Read velocity as " + _velocity + " on " + GetHashCode());
_rigidbody.isKinematic = false;
_rigidbody.AddForce(_velocity, ForceMode.Impulse);
_rigidbody.AddTorque(_angularVelocity, ForceMode.Impulse);
}

return true;
}
What's strange is whenever _rigidbody.Velocity (a structure) contains value the GetHashCode returns different. EG: if _rigidbody.velocity is Vector3.zero the hashcode is the same as what was called during the loop, but if it differs the hashcode does as well. I've found simply changing the struct to a class resolves the issue but I'm not sure why. The read velocity segment also always has the correct hashcode either way.
14 replies