Replace per-second FindObjectsOfType cart search with a cheap nearby check

This commit is contained in:
Justin Oros
2026-09-22 22:43:57 -07:00
parent 048bf77543
commit cb1768163a
2 changed files with 26 additions and 13 deletions
+25 -12
View File
@@ -16,7 +16,8 @@ public class BackupCam : SonsMod
{ {
const float ReverseSpeed = -0.4f; const float ReverseSpeed = -0.4f;
const float HideDelay = 0.6f; const float HideDelay = 0.6f;
const float SearchInterval = 1f; const float SearchInterval = 0.5f;
const float SearchRadius = 2.5f;
const string ScreenPath = "GolfCartScreen/GolfCartGps/Canvas/ScreenMask"; const string ScreenPath = "GolfCartScreen/GolfCartGps/Canvas/ScreenMask";
const string ScreenImageName = "BackupCamScreen"; const string ScreenImageName = "BackupCamScreen";
@@ -51,7 +52,7 @@ public class BackupCam : SonsMod
protected override void OnSdkInitialized() protected override void OnSdkInitialized()
{ {
RLog.Msg("BackupCam 1.1.0 loaded. Reverse the golf cart to show the camera on its GPS screen. Console: backupcamoffset, backupcamdump"); RLog.Msg("BackupCam 1.1.1 loaded. Reverse the golf cart to show the camera on its GPS screen. Console: backupcamoffset, backupcamdump");
} }
protected override void OnGameStart() protected override void OnGameStart()
@@ -117,22 +118,34 @@ public class BackupCam : SonsMod
_body = null; _body = null;
_screenGo = null; _screenGo = null;
foreach (var cart in Object.FindObjectsOfType<GolfCartController>()) var player = LocalPlayer.Transform;
{ var root = player.root;
var body = cart.GetComponentInParent<Rigidbody>(); if (root != player && TryUseCart(root.GetComponentInChildren<GolfCartController>())) return true;
if (body == null) body = cart.GetComponentInChildren<Rigidbody>();
if (body == null || !IsInCart(body)) continue;
_cart = cart; foreach (var col in Physics.OverlapSphere(player.position, SearchRadius, ~0, QueryTriggerInteraction.Ignore))
_body = body; {
AttachCamera(); if (col == null) continue;
AttachScreen(); if (TryUseCart(col.GetComponentInParent<GolfCartController>())) return true;
return true;
} }
return false; return false;
} }
bool TryUseCart(GolfCartController cart)
{
if (cart == null) return false;
var body = cart.GetComponentInParent<Rigidbody>();
if (body == null) body = cart.GetComponentInChildren<Rigidbody>();
if (body == null || !IsInCart(body)) return false;
_cart = cart;
_body = body;
AttachCamera();
AttachScreen();
return true;
}
bool IsInCart(Rigidbody body) bool IsInCart(Rigidbody body)
{ {
var player = LocalPlayer.Transform; var player = LocalPlayer.Transform;
+1 -1
View File
@@ -3,7 +3,7 @@
"id": "BackupCam", "id": "BackupCam",
"name": "BackupCam", "name": "BackupCam",
"author": "JustinOros", "author": "JustinOros",
"version": "1.1.0", "version": "1.1.1",
"description": "Shows a backup camera when reversing the golf cart", "description": "Shows a backup camera when reversing the golf cart",
"gameVersion": "1.0.0", "gameVersion": "1.0.0",
"platform": "Client", "platform": "Client",