FirstGearGames
FirstGearGames
CC#
Created by FirstGearGames on 3/11/2023 in #help
❔ Passing around thread to perform work on?
Disregard. I'll just use the ThreadPool methods to control utilization better.
5 replies
CC#
Created by FirstGearGames on 3/11/2023 in #help
❔ Passing around thread to perform work on?
Although reading more about this, it would suggest Tasks might be okay? I'd still like to be able to control the thread used if possible.
Tasks on the other hand are a higher-level .NET abstraction that basically represents a promise of a separate work, that’ll be completed in future. For example, a Task<T> is nothing but a task that comes with the promise of returning a value of type T when the task completes. They are compositional in nature, capable of returning values and being chained (at any amount) by using task continuations. They are also capable of using the Thread Pool and extremely handy for being used for I/O bound operations.
Tasks on the other hand are a higher-level .NET abstraction that basically represents a promise of a separate work, that’ll be completed in future. For example, a Task<T> is nothing but a task that comes with the promise of returning a value of type T when the task completes. They are compositional in nature, capable of returning values and being chained (at any amount) by using task continuations. They are also capable of using the Thread Pool and extremely handy for being used for I/O bound operations.
5 replies
CC#
Created by FirstGearGames on 3/11/2023 in #help
❔ Passing around thread to perform work on?
I'm aware you could run the second half of DoThing in a Task but that method could be called hundreds of times at once and starting new Tasks that frequently seems like a bad idea.
5 replies
CC#
Created by FirstGearGames on 2/11/2023 in #help
❔ Optimization thoughts on a loop.
double checking my work
8 replies
CC#
Created by FirstGearGames on 2/11/2023 in #help
❔ Optimization thoughts on a loop.
I'm just curious if there's something that sounds out as 'could be better'
8 replies
CC#
Created by FirstGearGames on 9/21/2022 in #help
Multiple types in a params without casting?
okay thanks. I'll probably just eat the cast cost. its not really super performant crucial and shouldn't hurt anything in the long run.
7 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
my RigidbodyData was being weird with the hashcodes while my Rigidbody2DData was not.
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
The other struct had different value types but otherwise was exactly the same.
private struct Rigidbody2DData
{
/// <summary>
/// Rigidbody for data.
/// </summary>
public Rigidbody2D Rigidbody2d;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public Vector2 Velocity;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public float AngularVelocity;
private struct Rigidbody2DData
{
/// <summary>
/// Rigidbody for data.
/// </summary>
public Rigidbody2D Rigidbody2d;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public Vector2 Velocity;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public float AngularVelocity;
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
@mtreit Still interesting that it works on one struct but not the other, and that moving the method out of the struct solved it.
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
Here's the working version. I just moved the method call out.
private struct RigidbodyData
{
/// <summary>
/// Rigidbody for data.
/// </summary>
public Rigidbody Rigidbody;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public Vector3 Velocity;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public Vector3 AngularVelocity;

public RigidbodyData(Rigidbody rigidbody)
{
Rigidbody = rigidbody;
Rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
Velocity = Vector3.zero;
AngularVelocity = Vector3.zero;
}
}
private struct RigidbodyData
{
/// <summary>
/// Rigidbody for data.
/// </summary>
public Rigidbody Rigidbody;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public Vector3 Velocity;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
public Vector3 AngularVelocity;

public RigidbodyData(Rigidbody rigidbody)
{
Rigidbody = rigidbody;
Rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
Velocity = Vector3.zero;
AngularVelocity = Vector3.zero;
}
}
for (int i = 0; i < _rigidbodyDatas.Count; i++)
{
if (!SetIsKinematic(isKinematic, i))
{
_rigidbodyDatas.RemoveAt(i);
i--;
}
}

//Sets isKinematic status and returns if successful.
bool SetIsKinematic(bool isZKinematic, int index)
{
RigidbodyData rbData = _rigidbodyDatas[index];
Rigidbody rb = rbData.Rigidbody;
if (rb == null)
return false;

if (isZKinematic)
{
rbData.Velocity = rb.velocity;
rbData.AngularVelocity = rb.angularVelocity;
rb.isKinematic = true;

//Update data.
_rigidbodyDatas[index] = rbData;
}
else
{
rb.isKinematic = false;
rb.AddForce(rbData.Velocity, ForceMode.Impulse);
rb.AddTorque(rbData.AngularVelocity, ForceMode.Impulse);
}

return true;
}
for (int i = 0; i < _rigidbodyDatas.Count; i++)
{
if (!SetIsKinematic(isKinematic, i))
{
_rigidbodyDatas.RemoveAt(i);
i--;
}
}

//Sets isKinematic status and returns if successful.
bool SetIsKinematic(bool isZKinematic, int index)
{
RigidbodyData rbData = _rigidbodyDatas[index];
Rigidbody rb = rbData.Rigidbody;
if (rb == null)
return false;

if (isZKinematic)
{
rbData.Velocity = rb.velocity;
rbData.AngularVelocity = rb.angularVelocity;
rb.isKinematic = true;

//Update data.
_rigidbodyDatas[index] = rbData;
}
else
{
rb.isKinematic = false;
rb.AddForce(rbData.Velocity, ForceMode.Impulse);
rb.AddTorque(rbData.AngularVelocity, ForceMode.Impulse);
}

return true;
}
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
I resolved it using a work-around. I'm going to share the information here to potentially help others. Here's my structure before the fix. Minding, all I was doing was calling SetIsKinematic(true/false) that was causing the issue.
public class RigidbodyPauser
{
#region Types.
/// <summary>
/// Data for a rigidbody before being set kinematic.
/// </summary>
private struct RigidbodyData
{
/// <summary>
/// Rigidbody for data.
/// </summary>
private Rigidbody _rigidbody;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
private Vector3 _velocity;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
private Vector3 _angularVelocity;

public RigidbodyData(Rigidbody rigidbody)
{
_rigidbody = rigidbody;
_rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
_velocity = Vector3.zero;
_angularVelocity = Vector3.zero;
}

/// <summary>
/// Sets isKinematic status and returns if successful.
/// </summary>
public bool SetIsKinematic(bool isKinematic)
{
if (_rigidbody == null)
return false;

if (isKinematic)
{
_velocity = _rigidbody.velocity;
//_VELOCITY was being set to the wrong object hashcode if it contained anything other than Vector3.zero.
_angularVelocity = _rigidbody.angularVelocity;
_rigidbody.isKinematic = true;
}
else
{
_rigidbody.isKinematic = false;
_rigidbody.AddForce(_velocity, ForceMode.Impulse);
_rigidbody.AddTorque(_angularVelocity, ForceMode.Impulse);
}

return true;
}
}
public class RigidbodyPauser
{
#region Types.
/// <summary>
/// Data for a rigidbody before being set kinematic.
/// </summary>
private struct RigidbodyData
{
/// <summary>
/// Rigidbody for data.
/// </summary>
private Rigidbody _rigidbody;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
private Vector3 _velocity;
/// <summary>
/// Cached velocity when being set kinematic.
/// </summary>
private Vector3 _angularVelocity;

public RigidbodyData(Rigidbody rigidbody)
{
_rigidbody = rigidbody;
_rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
_velocity = Vector3.zero;
_angularVelocity = Vector3.zero;
}

/// <summary>
/// Sets isKinematic status and returns if successful.
/// </summary>
public bool SetIsKinematic(bool isKinematic)
{
if (_rigidbody == null)
return false;

if (isKinematic)
{
_velocity = _rigidbody.velocity;
//_VELOCITY was being set to the wrong object hashcode if it contained anything other than Vector3.zero.
_angularVelocity = _rigidbody.angularVelocity;
_rigidbody.isKinematic = true;
}
else
{
_rigidbody.isKinematic = false;
_rigidbody.AddForce(_velocity, ForceMode.Impulse);
_rigidbody.AddTorque(_angularVelocity, ForceMode.Impulse);
}

return true;
}
}
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
I've never seen anything like this before.
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
I'm not using any overrides but the value is definitely disappearing somewhere. The object the value was 'cached' on would change when a non-default value was being written.
14 replies
CC#
Created by FirstGearGames on 8/31/2022 in #help
HashCode randomly changing on structures.
and the REALLY stupid part is I have another variant of the same structure that works fine.
14 replies