Jump to content

From C# to Lua


Ordush
 Share

Recommended Posts

Hey all, i've been tinkering with importing settings into in-game lua variables.
With the help of camelot10
I'm almost there getting everything to work.

void FetchFromLua()
    {
        HuntersMark = Lua.LuaDoString<bool>("return HuntersMarkDisabled");
    }

This works great, as it changes my setting based on the in-game variable HuntersMarkDisabled.

The problem is, when i want to send that data back to the game after a new session. I use.
 

void PushToLua()
	{
		Lua.LuaDoString("HuntersMarkDisabled = " + HuntersMark);
	}

Which should set the variable HuntersMarkDisabled to whatever HuntersMark was in C# before.
Now, what happens is: This turns HuntersMarkDisabled into nil instead of true/false
My guess is that, what it is doing is: It's looking for the lua Variable HuntersMark instead of C#. Any help would be appreciated! :)

Edit:
Here is all my code:

[Serializable]
public class HunterBeastmasterSettings : Settings
{

	private bool _huntersmark = true;
	[Setting]
	[DefaultValue(true)]
	[Category("Combat Spells - Hunter Abilities")]
	[DisplayName("Hunter's Mark Disabled")]
	[Description("Is Hunter's Mark Disabled?")]
	public bool HuntersMark { get { return _huntersmark; } set { _huntersmark = value; } }

	private bool _steadyshot = true;
	[Setting]
	[DefaultValue(true)]
	[Category("Combat Spells - Hunter Abilities")]
	[DisplayName("Steady Shot")]
	[Description("Use Steady Shot")]
	public bool SteadyShot { get { return _steadyshot; } set { _steadyshot = value; } }



	private HunterBeastmasterSettings()
	{
		ConfigWinForm(new System.Drawing.Point(400, 400), "HunterBeastmaster " + Translate.Get("Settings"));
	}

	public static HunterBeastmasterSettings CurrentSetting { get; set; }

	public bool Save()
	{
		try
		{
			return Save(AdviserFilePathAndName("CustomClass-HunterBeastmaster", ObjectManager.Me.Name + "." + Usefuls.RealmName));
		}
		catch (Exception e)
		{
			Logging.WriteError("HunterBeastmasterSettings > Save(): " + e);
			return false;
		}
	}

	public static bool Load()
	{
		try
		{
			if (File.Exists(AdviserFilePathAndName("CustomClass-HunterBeastmaster", ObjectManager.Me.Name + "." + Usefuls.RealmName)))
			{
				CurrentSetting = Load<HunterBeastmasterSettings>(AdviserFilePathAndName("CustomClass-HunterBeastmaster", ObjectManager.Me.Name + "." + Usefuls.RealmName));   
				CurrentSetting.PushToLua();
				CurrentSetting.FetchFromLua();
				return true;
			}
			CurrentSetting = new HunterBeastmasterSettings();
			
		}
		catch (Exception e)
		{
		Logging.WriteError("HunterBeastmasterSettings > Load(): " + e);
		}
		return false;
	}
	void FetchFromLua()
	{
		HuntersMark = Lua.LuaDoString<bool>("return HuntersMarkDisabled");
	}

	void PushToLua()
	{
		Lua.LuaDoString("HuntersMarkDisabled = " + HuntersMark);
	}
}

 

Edited by Ordush
Link to comment
Share on other sites

[Serializable]
public class HunterBeastmasterSettings : Settings
{
	const string FILE_PREFIX = "CustomClass-HunterBeastmaster";

	[Setting]
	[DefaultValue(true)]
	[Category("Combat Spells - Hunter Abilities")]
	[DisplayName("Hunter's Mark Disabled")]
	[Description("Is Hunter's Mark Disabled?")]
	public bool HuntersMark { get; set; }

	[Setting]
	[DefaultValue(true)]
	[Category("Combat Spells - Hunter Abilities")]
	[DisplayName("Steady Shot")]
	[Description("Use Steady Shot")]
	public bool SteadyShot { get; set; }

	HunterBeastmasterSettings()
	{
		ConfigWinForm(new System.Drawing.Point(400, 400), "HunterBeastmaster " + Translate.Get("Settings"));
	}

	public static HunterBeastmasterSettings CurrentSetting { get; set; }

	public bool Save()
	{
		try
		{
			CurrentSetting.FetchFromLua(); // LOAD SETTINGS FROM LUA
			return Save(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName));
		}
		catch (Exception e)
		{
			Logging.WriteError(FILE_PREFIX + " save error: " + e);
			return false;
		}
	}

	public static bool Load()
	{
		try
		{
			if (File.Exists(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName)))
			{
				CurrentSetting = Load<HunterBeastmasterSettings>(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName));
				CurrentSetting.PushToLua(); // SETTINGS LOADED, MUST BE TRANSLATED TO LUA
				return true;
			}
			CurrentSetting = new HunterBeastmasterSettings();

		}
		catch (Exception e)
		{
			Logging.WriteError(FILE_PREFIX + " load error: " + e);
		}
		return false;
	}
	void FetchFromLua()
	{
		HuntersMark = Lua.LuaDoString<bool>("return HuntersMarkDisabled");
	}

	void PushToLua()
	{
		Lua.LuaDoString("HuntersMarkDisabled = " + HuntersMark);
	}
}

 

Link to comment
Share on other sites

4 hours ago, camelot10 said:
[Serializable]
public class HunterBeastmasterSettings : Settings
{
	const string FILE_PREFIX = "CustomClass-HunterBeastmaster";

	[Setting]
	[DefaultValue(true)]
	[Category("Combat Spells - Hunter Abilities")]
	[DisplayName("Hunter's Mark Disabled")]
	[Description("Is Hunter's Mark Disabled?")]
	public bool HuntersMark { get; set; }

	[Setting]
	[DefaultValue(true)]
	[Category("Combat Spells - Hunter Abilities")]
	[DisplayName("Steady Shot")]
	[Description("Use Steady Shot")]
	public bool SteadyShot { get; set; }

	HunterBeastmasterSettings()
	{
		ConfigWinForm(new System.Drawing.Point(400, 400), "HunterBeastmaster " + Translate.Get("Settings"));
	}

	public static HunterBeastmasterSettings CurrentSetting { get; set; }

	public bool Save()
	{
		try
		{
			CurrentSetting.FetchFromLua(); // LOAD SETTINGS FROM LUA
			return Save(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName));
		}
		catch (Exception e)
		{
			Logging.WriteError(FILE_PREFIX + " save error: " + e);
			return false;
		}
	}

	public static bool Load()
	{
		try
		{
			if (File.Exists(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName)))
			{
				CurrentSetting = Load<HunterBeastmasterSettings>(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName));
				CurrentSetting.PushToLua(); // SETTINGS LOADED, MUST BE TRANSLATED TO LUA
				return true;
			}
			CurrentSetting = new HunterBeastmasterSettings();

		}
		catch (Exception e)
		{
			Logging.WriteError(FILE_PREFIX + " load error: " + e);
		}
		return false;
	}
	void FetchFromLua()
	{
		HuntersMark = Lua.LuaDoString<bool>("return HuntersMarkDisabled");
	}

	void PushToLua()
	{
		Lua.LuaDoString("HuntersMarkDisabled = " + HuntersMark);
	}
}

 

Once again
Thanks a ton Camelot!

Link to comment
Share on other sites

I will just make an example of how i'd love for this to work. :)
SLASH_TOGGLEHUNTERSMARK1 = '/togglehuntersmark'
SlashCmdList.TOGGLEHUNTERSMARK1=function()
    if HuntersMarkDisabled then
        HuntersMarkDisabled = false
    else
        HuntersMarkDisabled = true
    end
end

Then the condition is set to HuntersMarkDisabled false to use hunters mark.

Works very well without any problems. Now the problem is i relog the game HuntersMarkDisabled is reset (due to the variable no longer being true/false).
So camelot10 came up with the code above, so that my HuntersMarkDisabled would be stored with the Fight Class settings. Here the problem is that when it does

Lua.LuaDoString("HuntersMarkDisabled = " + HuntersMark);

it doesn't turn HuntersMarkDisabled into what HuntersMark is, but it just sets it to nil.

Hope someone can help with this problem. :)

Link to comment
Share on other sites

On 10.9.2017 at 4:19 PM, Ordush said:

I will just make an example of how i'd love for this to work. :)
SLASH_TOGGLEHUNTERSMARK1 = '/togglehuntersmark'
SlashCmdList.TOGGLEHUNTERSMARK1=function()
    if HuntersMarkDisabled then
        HuntersMarkDisabled = false
    else
        HuntersMarkDisabled = true
    end
end

Then the condition is set to HuntersMarkDisabled false to use hunters mark.

Works very well without any problems. Now the problem is i relog the game HuntersMarkDisabled is reset (due to the variable no longer being true/false).
So camelot10 came up with the code above, so that my HuntersMarkDisabled would be stored with the Fight Class settings. Here the problem is that when it does

Lua.LuaDoString("HuntersMarkDisabled = " + HuntersMark);

it doesn't turn HuntersMarkDisabled into what HuntersMark is, but it just sets it to nil.

Hope someone can help with this problem. :)

Never worked with ingame variables but give this a try:

CVar.SetCVar("HuntersMarkDisabled", HuntersMark);

instead of the Lua.DoString();

If you want to read out your variable try:

bool test = CVar.GetCVar<bool>("HuntersMarkDisabled");

Hope that helps.

Edited by iMod
Link to comment
Share on other sites

6 hours ago, iMod said:

Never worked with ingame variables but give this a try:

CVar.SetCVar("HuntersMarkDisabled", HuntersMark);

instead of the Lua.DoString();

If you want to read out your variable try:

bool test = CVar.GetCVar<bool>("HuntersMarkDisabled");

Hope that helps.

Thanks for the tips, i will give it a try. :)

Link to comment
Share on other sites

That did not work, it just gives the error: Couldn't find CVar named 'HuntersMarkDisabled'.
However i did fix it by doing this:
 

[Serializable]
public class HunterBeastmasterSettings : Settings
{
    const string FILE_PREFIX = "CustomClass-HunterBeastmaster";

    [Setting]
    [DefaultValue(true)]
    [Category("Combat Spells - Hunter Abilities")]
    [DisplayName("Hunter's Mark Disabled")]
    [Description("Is Hunter's Mark Disabled?")]
    public bool HuntersMark { get; set; }

    [Setting]
    [DefaultValue(true)]
    [Category("Combat Spells - Hunter Abilities")]
    [DisplayName("Steady Shot")]
    [Description("Use Steady Shot")]
    public bool SteadyShot { get; set; }

    HunterBeastmasterSettings()
    {
        ConfigWinForm(new System.Drawing.Point(400, 400), "HunterBeastmaster " + Translate.Get("Settings"));
    }

    public static HunterBeastmasterSettings CurrentSetting { get; set; }

    public static bool Load()
    {
        try
        {
            if (File.Exists(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName)))
            {
                CurrentSetting = Load<HunterBeastmasterSettings>(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName));
                CurrentSetting.FetchFromLua(); // SETTINGS LOADED, MUST BE TRANSLATED TO LUA
                return true;
            }
            CurrentSetting = new HunterBeastmasterSettings();

        }
        catch (Exception e)
        {
            Logging.WriteError(FILE_PREFIX + " load error: " + e);
        }
        return false;
    }

    public bool Save()
    {
        try
        {
            CurrentSetting.PushToLua(); // LOAD SETTINGS FROM LUA
            return Save(AdviserFilePathAndName(FILE_PREFIX, ObjectManager.Me.Name + "." + Usefuls.RealmName));
        }
        catch (Exception e)
        {
            Logging.WriteError(FILE_PREFIX + " save error: " + e);
            return false;
        }
    }
    void FetchFromLua()
    {
        HuntersMark = Lua.LuaDoString<bool>("return HuntersMarkDisabled");
    }

    void PushToLua()
    {
        if (HuntersMark)
        {
            Lua.LuaDoString("HuntersMarkDisabled = true");
        }
        else
        {
            Lua.LuaDoString("HuntersMarkDisabled = false");
        }
    }
}

With that code, i don't combine C# and Lua in same string. But it does what it is supposed to.
It also does change in the settings and changes in-game if i change it in the settings. So all is good.
However, when i reload the game, it seems as if the FetchFromLua Happens before the PushFromLua so it resets to false because of that. This does make sense since it's supposed to fetch from lua on load, but if i swap the two so it fetches on save and pushes on load, then nothing works.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...