mono_v2_get_started icon indicating copy to clipboard operation
mono_v2_get_started copied to clipboard

Source Attribute ([Source]) Error/Issue.

Open vipexv opened this issue 2 years ago • 2 comments

What happened?

Trying to grab the players name, but somehow it keeps returning with an error of not providing the first parameter when you trigger a function/native but i'm not doing that here.

This happens regardles of being called by client or directly through the server console, the exact same error pops up.

Code:

using CitizenFX.Core;
using CitizenFX.Server;
using CitizenFX.Server.Native;

namespace Server
{
    public class Server_Main : BaseScript
    {
        public Server_Main() {

            Debug.WriteLine("Hello from the server using MonoV2");

            Natives.RegisterCommand("hello", new Action<Player>(Hello_Command), false);
        }
        public void Hello_Command([Source] Player player) {
            Debug.WriteLine($"Hello {player.Name}");
        }
    }
}

Error:

[     72953] [    GTAProcess]             MainThrd/ ^3Failed with exception:
[     72953] [    GTAProcess]             MainThrd/ System.InvalidOperationException: Error executing native 0x00000000406b4b20 at address 0x24b2ac6a2f8, exception: Argument at index 0 was null.
[     72953] [    GTAProcess]             MainThrd/   at (wrapper managed-to-native) CitizenFX.Core.ScriptInterface.InvokeNative(uintptr,CitizenFX.Core.fxScriptContext&,ulong)
[     72953] [    GTAProcess]             MainThrd/   at CitizenFX.Core.ScriptContext.InvokeNative (System.UIntPtr& handle, System.UInt64 hash, System.UInt64* data, System.Int32 size) [0x0002a] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore-v2\Native\ScriptContext.cs:182 
[     72953] [    GTAProcess]             MainThrd/   at CitizenFX.Server.Native.NativesImpl.OutString_0x406b4b20 (CitizenFX.Core.CString playerSrc) [0x0002c] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore-v2\Native\NativesServer.cs:3644 
[     72953] [    GTAProcess]             MainThrd/   at CitizenFX.Server.Native.Natives.GetPlayerName (CitizenFX.Core.CString playerSrc) [0x00000] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore-v2\Native\NativesServer.cs:7712 
[     72953] [    GTAProcess]             MainThrd/   at CitizenFX.Server.Player.get_Name () [0x00000] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore-v2\Server\ServerWrappers.cs:60 
[     72953] [    GTAProcess]             MainThrd/   at Server.Server_Main.Hello_Server (CitizenFX.Server.Player player) [0x00001] in <74a8511c03024d58805b4261986f0b01>:0 
[     72953] [    GTAProcess]             MainThrd/   at (wrapper dynamic-method) System.Object.Server.Server_Main.Hello_Server(object,CitizenFX.Core.Remote,object[])
[     72953] [    GTAProcess]             MainThrd/   at CitizenFX.Core.ReferenceFunctionManager.Invoke (System.Int32 reference, System.Byte* arguments, System.UInt32 argsSize) [0x00024] in C:\gl\builds\4ff63adb\0\cfx\fivem\code\client\clrcore-v2\Interop\ReferenceFunctionManager.cs:174 ^7

Expected result

Print the players name, somehow everything from the Player object is returning an error.

Reproduction steps

Simply trying to learn C# and started to move to MonoV2, trying out the Updated SourceAttribute and everything from the Player object returns an error.

Importancy

Can't use this feature until fixed

Specific version

FiveM, Localhost Windows 11, Latest Artifact

Extra

No response

vipexv avatar Aug 25 '23 03:08 vipexv

Commands follow a different protocol from events (which do support player info separately) and can't be separated from other calls that run through the same interface (which lack player info, e.g.: Callbacks), this is why [Source] isn't working by default.

By default you'd make a function like this for commands:

public void MyCommand(int playerId, object[] arguments, string fullCommand)
{
	// ...
}

If you're still interested to use the [Source] attribute, then you can set RemapParameters = true in the command attribute, e.g.:

[Command("hello", RemapParameters = true)]
public void MyCommand([Source] Player player)
{
	Debug.WriteLine($"Hello {player.Name}");
}

Remapping was requested by https://github.com/thorium-cfx/mono_v2_get_started/issues/19

Keeping this open so that we update the examples page to include this.

thorium-cfx avatar Aug 28 '23 08:08 thorium-cfx

Thank you, i'm pretty new to C# but that makes sense.

vipexv avatar Aug 29 '23 09:08 vipexv