C# Issues returning the proper Task<bool> value from and async Task<bool> function

Viewed 44

I am trying to create a "minigame" or "skill check" system of sorts in FiveM.

I have an export that I wish for external scripts to call and receive a True or False callback depending on if the player passed or failed the skill check.


public void RegisterExport(string name, Delegate action) => Exports.Add(name, action);
        
public bool gameStarted = false;
public bool gameWon = false;

public ClientMain()
        {
            RegisterExport("startGame", new Task<bool>(StartGame));
            RegisterNuiCallbackType("finish");
            EventHandlers["__cfx_nui:finish"] += new Action<ExpandoObject, CallbackDelegate>(Finish);

        }
        
        private async Task<bool> StartGame()
        {
            
            gameStarted = true;
            SendNuiMessage(@"{""startGame"":true, ""number"": 4}");
            SetNuiFocus(true, true);

            Debug.WriteLine("Game started");

            while (gameStarted)
            {
                await Delay(100);
            }

            Task<bool> result = new Task<bool>(() => gameWon);
            result.Start();
            return await result;
            //return Task.Factory.StartNew(() => gameWon).Result;
            //return gameWon;

        }

The RegisterExport line, specifically the StartGame is underlined stating the wrong return type. I have tried swapping out the return to return Task.Factory.StartNew(() => gameWon).Result;

It states it is returning a Task<bool>, but still StartGame at the top says Incorrect return type, Task<bool>

0 Answers
Related