Skip to content

Conversation

@k073l
Copy link

@k073l k073l commented May 19, 2025

Add a simple way to spawn and customize vehicles with S1API.

Example:

using S1API.Vehicles;

var vehicle = new LandVehicle("veeper");
vehicle.TopSpeed = 100f;
vehicle.Color = VehicleColor.Cyan;
vehicle.IsPlayerOwned = true;
vehicle.Spawn(new Vector3(0, 50, 0), Quaternion.identity);

Copy link
Collaborator

@MaxtorCoder MaxtorCoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-order the access modifiers as well to how it looks like in the NPC class for example.

Comment on lines 80 to 95
private void _setConnection()
{
var nm = InstanceFinder.NetworkManager;
if (nm.IsClientOnly)
{
var tempConn = InstanceFinder.ClientManager.Connection;
if (tempConn != null && tempConn.IsValid)
_conn = tempConn;
}
else if (nm.IsServerOnly || (nm.IsServer && !nm.IsClient))
{
var owner = S1LandVehicle.Owner;
if (owner != null && owner.IsValid)
_conn = owner;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private void _setConnection()
{
var nm = InstanceFinder.NetworkManager;
if (nm.IsClientOnly)
{
var tempConn = InstanceFinder.ClientManager.Connection;
if (tempConn != null && tempConn.IsValid)
_conn = tempConn;
}
else if (nm.IsServerOnly || (nm.IsServer && !nm.IsClient))
{
var owner = S1LandVehicle.Owner;
if (owner != null && owner.IsValid)
_conn = owner;
}
}
private void SetConnection()
{
var nm = InstanceFinder.NetworkManager;
if (nm.IsClientOnly)
{
var tempConn = InstanceFinder.ClientManager.Connection;
if (tempConn != null && tempConn.IsValid)
_conn = tempConn;
}
else if (nm.IsServerOnly || (nm.IsServer && !nm.IsClient))
{
var owner = S1LandVehicle.Owner;
if (owner != null && owner.IsValid)
_conn = owner;
}
}

Comment on lines 128 to 136
private void _setIsPlayerOwned(bool isPlayerOwned)
{
S1LandVehicle.SetIsPlayerOwned(_conn, isPlayerOwned);
// make sure to add/remove the vehicle from the player owned vehicles list
if (isPlayerOwned)
NetworkSingleton<S1Vehicles.VehicleManager>.Instance.PlayerOwnedVehicles.Add(S1LandVehicle);
else
NetworkSingleton<S1Vehicles.VehicleManager>.Instance.PlayerOwnedVehicles.Remove(S1LandVehicle);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private void _setIsPlayerOwned(bool isPlayerOwned)
{
S1LandVehicle.SetIsPlayerOwned(_conn, isPlayerOwned);
// make sure to add/remove the vehicle from the player owned vehicles list
if (isPlayerOwned)
NetworkSingleton<S1Vehicles.VehicleManager>.Instance.PlayerOwnedVehicles.Add(S1LandVehicle);
else
NetworkSingleton<S1Vehicles.VehicleManager>.Instance.PlayerOwnedVehicles.Remove(S1LandVehicle);
}
private void SetIsPlayerOwned(bool isPlayerOwned)
{
S1LandVehicle.SetIsPlayerOwned(_conn, isPlayerOwned);
// make sure to add/remove the vehicle from the player owned vehicles list
if (isPlayerOwned)
NetworkSingleton<S1Vehicles.VehicleManager>.Instance.PlayerOwnedVehicles.Add(S1LandVehicle);
else
NetworkSingleton<S1Vehicles.VehicleManager>.Instance.PlayerOwnedVehicles.Remove(S1LandVehicle);
}

Comment on lines 151 to 163
private void _setColor(VehicleColor color)
{
var setOwnedColorMethod =
typeof(S1Vehicles.LandVehicle).GetMethod("SetOwnedColor",
BindingFlags.Instance | BindingFlags.NonPublic);
if (setOwnedColorMethod == null)
{
Logger.Error("SetOwnedColor method not found!");
return;
}

setOwnedColorMethod.Invoke(S1LandVehicle, [_conn, (S1Vehicles.Modification.EVehicleColor)color]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private void _setColor(VehicleColor color)
{
var setOwnedColorMethod =
typeof(S1Vehicles.LandVehicle).GetMethod("SetOwnedColor",
BindingFlags.Instance | BindingFlags.NonPublic);
if (setOwnedColorMethod == null)
{
Logger.Error("SetOwnedColor method not found!");
return;
}
setOwnedColorMethod.Invoke(S1LandVehicle, [_conn, (S1Vehicles.Modification.EVehicleColor)color]);
}
private void SetColor(VehicleColor color)
{
var setOwnedColorMethod =
typeof(S1Vehicles.LandVehicle).GetMethod("SetOwnedColor",
BindingFlags.Instance | BindingFlags.NonPublic);
if (setOwnedColorMethod == null)
{
Logger.Error("SetOwnedColor method not found!");
return;
}
setOwnedColorMethod.Invoke(S1LandVehicle, [_conn, (S1Vehicles.Modification.EVehicleColor)color]);
}

/// <summary>
/// Logger for the LandVehicle class.
/// </summary>
private static readonly Log Logger = new Log("S1API.LandVehicle");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static readonly Log Logger = new Log("S1API.LandVehicle");
private static readonly Log _logger = new Log("S1API.LandVehicle");

Copy link
Collaborator

@MaxtorCoder MaxtorCoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the NetworkSingleton generic either. You can just call S1Vehicles.VehicleManager.Instance directly.

Should've been in the last one and I forgot, my bad.

@k073l k073l requested a review from MaxtorCoder May 22, 2025 18:17
@MaxtorCoder MaxtorCoder changed the base branch from bleeding-edge to pr-verification May 22, 2025 18:22
@MaxtorCoder MaxtorCoder merged commit ebd9c5c into KaBooMa:pr-verification May 22, 2025
1 check failed
@MaxtorCoder
Copy link
Collaborator

Thanks for the PR, I will fix any further issues with build :)

@k073l k073l deleted the vehicles branch May 22, 2025 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants