Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums and/or MValue adapters not working properly for VirtualEntities #824

Open
Rodhin opened this issue Nov 10, 2023 · 0 comments
Open

Comments

@Rodhin
Copy link

Rodhin commented Nov 10, 2023

Hi, I was trying to set some enum values to a virtual entity's dataDict dictionary on server side (both a custom one and one from AltV.Net.Shared.Enums) and the server was complaining that it cannot convert those enums so I've created MValueAdapters for both but now on client side the value will always be the default value for the enum, should MValue adapters even work for VirtualEntity dataDict?

Server version: 15.48 (release)
Nuget version: 15.0.5

Server side code:

Dictionary<string, object> data = new()
{
    { "type", VirtualEntityType.Marker },
    { "markerType", MarkerType.MarkerCylinder },
    { "color", color },
    { "scale", (Position)scale },
    { "faceCamera", faceCamera },
};
VirtualEntity = Alt.CreateVirtualEntity(_virtualEntityGroup, Position - new Position(0, 0, 1), 20, data);

Client side code:

var hasType = virtualEntity.HasStreamSyncedMetaData("type");
var hasType2 = virtualEntity.GetStreamSyncedMetaData("type", out VirtualEntityType type);
var hasMarkerType = virtualEntity.HasStreamSyncedMetaData("markerType");
var hasMarkerType2 = virtualEntity.GetStreamSyncedMetaData("markerType", out MarkerType markerType);

Alt.Log($"[DATA CHECK] hasType {hasType} {hasType2} {type}");
Alt.Log($"[DATA CHECK] hasMarkerType {hasMarkerType} {hasMarkerType2} {markerType}");

Client side log:

[DATA CHECK] hasType True False None 
[DATA CHECK] hasMarkerType True False MarkerCone 

Custom enum:

namespace Shared.Enums;

public enum VirtualEntityType
{
    None,
    Marker,
}

MValueAdapter (both looks the same):

using AltV.Net;
using AltV.Net.Shared.Enums;

namespace Shared.Adapters;

public class MarkerTypeAdapter : IMValueAdapter<MarkerType>
{
    object IMValueBaseAdapter.FromMValue(IMValueReader reader)
    {
        return FromMValue(reader);
    }

    public void ToMValue(MarkerType value, IMValueWriter writer)
    {
        writer.BeginObject();
        writer.Name("markerType");
        writer.Value(Convert.ToUInt32(value));
        writer.EndObject();
    }

    public MarkerType FromMValue(IMValueReader reader)
    {
        MarkerType data = default;

        reader.BeginObject();
        while (reader.HasNext())
        {
            switch (reader.NextName())
            {
                case "markerType":
                    data = (MarkerType)reader.NextUInt();
                    break;
            }
        }

        reader.EndObject();

        return data;
    }

    public void ToMValue(object obj, IMValueWriter writer)
    {
        if (obj is MarkerType value)
        {
            ToMValue(value, writer);
        }
    }
}
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

No branches or pull requests

1 participant