steven4547466 / LC-API

General purpose utility API for modding Lethal Company
GNU Lesser General Public License v2.1
8 stars 3 forks source link

The `Player` variable (From Features), is null, when used within a command. #21

Closed CoolSlimbo closed 9 months ago

CoolSlimbo commented 10 months ago

As the title states, the Player variable returns a NullReferenceException when used within a command. I do not know if this problem persists through accessing it via other means, however, at any point in time, it has shown to be null when executing a chat command. (Currently tested in orbit, and on ground)

[Error  :Lethal Company API] Error handling command: lc
[Error  :Lethal Company API] System.NullReferenceException: Object reference not set to an instance of an object
  at LethallyCheated.Plugin.LethallyCheated (System.String[] args) [0x00006] in <3ba071e22617494dbacf7b69db2df2fb>:0 
  at LC_API.ClientAPI.CommandHandler+SubmitChatPatch.HandleMessage (HUDManager manager) [0x00066] in <2cbba39d1e4241d2961a30e8f27ca788>:0 

Further testing will be done within 24 hours, to see if this is command specific, or not.

steven4547466 commented 10 months ago

Player is a class, it can't be "null." static variables in it might be, can you show me your code?

CoolSlimbo commented 10 months ago

Sorry. Should’ve clarified.

Code:

static void command(string args[]) {
    Player.LocalPlayer.Hurt(5);
    // Player.HostPlayer.Hurt(5);
}

This is the code with the reference, and than the only thing I else that is relevant is the use of CommandHandler.RegisterCommand to register command.

What I can also mention (that should’ve been done so in the initial comment of the issue), is that the start method of the class is not run through my testing, which is the only time either HostPlayer or LocalPlayer get set from what I can see in the code.

If you believe you might know why this is happening, I am free to edit a fork and send in a PR to fix it to save time on your end.

steven4547466 commented 10 months ago

Interesting, here's my code:

ClientAPI.CommandHandler.RegisterCommand("test", (string[] args) =>
{
    GameInterfaceAPI.Features.Player.LocalPlayer.Hurt(10);
});

And here's a video showing it in action:

https://github.com/steven4547466/LC-API/assets/23726534/964d4839-0b3e-47de-a659-32686cd72f60

steven4547466 commented 9 months ago

Marking as stale. Reopen if this is still an issue.