[Help][C#]Export function

For example I want to export pNotify or something else. I would use

exports.pNotify:SendNotification({options}) or TriggerEvent(“pNotify:SendNotification”, {options});

But VS show tons of errors because this doesnt follow some format. How would I use export in C# ?

I created these methods for exports so I didn’t need to register / use exports in the BaseScript chosen class

        public void RegisterExport(string _export, Delegate _action)
        {
            try
            {
                Exports.Add(_export, _action);
            }
            catch (Exception ex)
            {
                Utils.Throw(ex);
            }
        }

        public ExportDictionary CallExport()
        {
            try
            {
                return Exports;
            }
            catch (Exception ex)
            {
                Utils.Throw(ex);
                return null;
            }
        }

To register an export I believe you do (USING MY METHOD)

RegisterExport("export_string", new Action<string>(MethodHere))

private void MethodHere(string _message)
{
	Debug.WriteLine(_message);
}

Calling an export (USING MY METHOD)

CallExport()["export_string"]("Hello World");

[[ If you are wanting to do this stuff directly into the BaseScript class ]]

public class Main: BaseScript
{
	public Main()
	{
		// Register Export
		Exports.Add("string_export", new Action<string>(ExportMethod));

		// Calling Export
		Exports["string_export"]("Hello World");
	}

	private void ExportMethod(string _message)
	{
		Debug.WriteLine(_message);
	}
}

So basically calling the export would be the same way I showed to call the export we created.

1 Like

But what about situations like this one:

exports.pNotify:SendNotification({
            text = "Testing Notification",
            type = "error",
            timeout = math.random(1000, 10000),
            layout = "centerLeft",
            queue = "left"
        })

When I need to export and fill options?

Okay. Well I believe you can do a couple things. You can make a class that has the same variables or you can do a dynamic ExpandoObject

Maybe you could do this?

private TestingMethod()
{
	dynamic args = new ExpandoObject();
	args.text = "";
	args.type = "";
	args.timeout = "";
	args.layout = "";
	args.queue = "";
	Exports["SendNotification"](args);
}

or this?

private TestingMethod()
{
	dynamic args = new ExpandoObject();
	args.text = "";
	args.type = "";
	args.timeout = "";
	args.layout = "";
	args.queue = "";
	Exports["pNotify:SendNotification"](args);
}

I have never called a Lua export in C#. I am not sure you are supposed to define the resource or not. I think that is the best I am gonna be able to help.

Thanks! Gonna try it as soon as I can. I think that should work just fine.

Like I said I may be completely wrong. I got no clue :smiley: but I mean you could def always try it. Maybe somebody will swoop in here call me an idiot and set me straight if I am wrong :smile:

So it would look like this?

        private void ExportMethod(string _message)
        {

            dynamic args = new ExpandoObject();
            args.text = "Press INSERTBUTTON to saw off an ATM";
            args.type = "info";
            args.timeout = "5000";
            args.layout = "centerLeft";
            args.queue = "left";
            Exports["pNotify:SendNotification"](args);
        }

And I will call it like this:
Exports["string_export"]("pNotify:SendNotification");

Honestly I am unsure which is why I did two different calls for you to try :rofl:

Would just see if it works

So I made another project for this to try it out.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
using CitizenFX.Core;
namespace Notification_extract
{
    public class Notification_extract:BaseScript
    {
        public Notification_extract()
        {

            // Register Export
            Tick += OnTick;
            Exports.Add("string_export", new Action<string>(ExportMethod));

            // Calling Export

           // Exports["string_export"]("pNotify:SendNotification");
        }

        private void ExportMethod(string _message)
        {

            dynamic args = new ExpandoObject();
            args.text = "Herro?";
            args.type = "info";
            args.timeout = "5000";
            args.layout = "centerLeft";
            args.queue = "left";
            Exports["pNotify:SendNotification"](args);
        }
        private async Task OnTick()
        {
            Exports["pNotify:SendNotification"](args);
            await Delay(5000);
        }
    }
}

So now I want to somehow get export from ExportMethod but its telling me that args doesnt exist in current context. What is wrong?

Exports["pNotify"].sendNotification(new { option = "whatever you normally put" });

7 Likes

Thanks for clearing that up. I didn’t now you could make an object like that for the args.

Oh well I know now.

Thanks a lot! Little correction [“pNotify”].SendNotification

1 Like