Jump to content

Saving in Lua in C#


Ordush
 Share

Recommended Posts

Hey all
This is a followup post from a previous one, i decided to make a new one to clear up all the clutter.
I have some code below that is ALMOST working. The problem is: My variables set in the settings of my FC defaults to false when not loaded from in-game lua.
Therefor when i reload the game, instead of setting my settings to what they were before, they just default to false.
Anyone with more C# experience than me, who can see what is wrong?
These variables are set in my lua: HuntersMarkDisabled = true/false
When the profile is loaded: LoadedInLua = true <-- This is so it doesn't overwrite it's own settings before the profile is loaded.

[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(); // LOAD SETTINGS FROM LUA
                return true;
            }
            CurrentSetting = new HunterBeastmasterSettings();

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

    public bool Save()
    {
        try
        {
            CurrentSetting.PushToLua(); // SETTINGS LOADED, MUST BE TRANSLATED TO 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()
    {
        var LoadedInLua = Lua.LuaDoString<bool>("return LoadedInLua");
        if (LoadedInLua)
        {
            HuntersMark = Lua.LuaDoString<bool>("return HuntersMarkDisabled");
        }
    }

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

 

Link to comment
Share on other sites

maybe me, but you dont understand how its works.

you load config from file to wrobot, and after that you should push settings to lua.

when you save settings, you you to fetch values from lua and save to file.

your error, as example: 

HunterMark default value is true, user set in wow HunterMark as false, before saving settings to file, you push settings to lua > override lua HunterMark to true and save HunterMark=true into xml

when you load settings from file, HunterMark become true, but after loading you fetch HunterMark from lua and no matter what saved into xml file, its gonna be overriden by lua fetch.

how to fix: fetch before save, push after load

Edited by camelot10
Link to comment
Share on other sites

2 minutes ago, camelot10 said:

maybe me, but you dont understand how its works.

you load config from file to wrobot, and after that you should push settings to lua.

when you save settings, you you to fetch values from lua and save to file.

your error, as example: 

HunterMark default value is true, user set in wow HunterMark as false, before saving settings to file, you push settings to lua > override lua HunterMark to true and save HunterMark=true into xml

when you load settings from file, HunterMark become true, but after loading you fetch HunterMark from lua and no matter what saved into xml file, its gonna be overriden by lua fetch.

how to fix: fetch before save, push after load

Yeah, i thought that was the problem, but i tried swapping them around as you just said, but then nothing happens at all, it neither loads nor saves anything, it just stays "false" no matter what. :/
And you are right, i don't fully understand it, since C# is not my strongest. Happy that you are willing to help, i will try swap them around and see if i was mistaken. :)

Link to comment
Share on other sites

@camelot10

I have tried this: Just as you told me to, but if i do it like this. It does not work.
HuntersMark just defaults to false here for some reason. It is as if it doesn't save.
My example i set HuntersMarkDisabled = true in-game. then i close the bot, and do /reloadui so that my lua is cleared, then i load the bot and run it. now HuntersMarkDisabled = false
Even SteadyShot which should not in any way be influenced by the game keeps going to "false". :O

[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.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;
    }

    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;
        }
    }

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

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

 

Edited by Ordush
Link to comment
Share on other sites

 
	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark + "; print('PUSHED LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HunterMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result+ " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result+ " ");
	}
as i see, lua also not your strong side. also programming in general same.

step by step tracing from point where no bug to point where bug appear called bug hunting. any programming related entity know how to do that

do you ever before develop anything related to programming/coding?

Link to comment
Share on other sites

10 hours ago, camelot10 said:

 
	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark + "; print('PUSHED LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HunterMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result+ " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result+ " ");
	}
as i see, lua also not your strong side. also programming in general same.

step by step tracing from point where no bug to point where bug appear called bug hunting. any programming related entity know how to do that

do you ever before develop anything related to programming/coding?

I really do appreciate your help mate.
But do you HAVE to be so condescending?
I am not a programmer no, everything i know i have taught myself over the last 6 months.
I can promise you that i am not bad with lua at all! And yes i do know what "bug hunting" is, or as it is really called debugging... I already have debugged my lua code. Nothing is wrong with my lua code at all. Everything works perfect from the lua perspective.
The only place where i am having issues is using the c#.
If i was pro programmer, i would not go to these forums to ask for help.
Anyway when i load the bot it gives me:
PUSHED TO LUA VALUE HuntersMarkDisabled=
When i open fight class settings i get:
PUSHED TO LUA VALUE HuntersMarkDisabled=
When i close fight class settings i get:
FETCH FROM LUA VALUE HuntersMarkDisabled=
So it seems as if it does not read the value from neither settings nor in-game. Although i know that earlier it has been reading the settings. Since it did change the value in settings, but it just didn't save them.

I allrdy earlier figured that you can't do HuntersMarkDisabled = " + HuntersMark + ";
Since it makes HuntersMarkDisabled nil.
That is why i did:

if (HuntersMark)
        {
            Lua.LuaDoString("HuntersMarkDisabled = true");
        }
        else
        {
            Lua.LuaDoString("HuntersMarkDisabled = false");
        }
Edited by Ordush
Link to comment
Share on other sites

10 minutes ago, camelot10 said:

you miss something. review code and check if all code where its should be. report what wrobot logging and wow chat info messages

My lua code is not missing anything, it works just fine without this code.
I allready know what the problem is:
It's this line: HuntersMarkDisabled = " + HuntersMark + ";
It doens't work like that, it will just make HuntersMarkDisabled nil, instead of HuntersMark value.

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

This however, does work.

Link to comment
Share on other sites

so you see difference and didnt understand what happened?

there is no such definition in wow lua as False only false thats why is set that var to nil

 

	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark.ToString().ToLower() + "; print('PUSHED LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HunterMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result + " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result + " ");
	}

just make sure for bool in C# before pushing to lua to convert to string and make lowercase

ToString().ToLower()

Edited by camelot10
Link to comment
Share on other sites

1 minute ago, camelot10 said:

so you see difference and didnt understand what happened?

there is no such definition in wow lua as False only false thats why is set that var to nil

 

	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark.ToString().ToLower() + "; print('PUSHED LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HunterMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result + " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result + " ");
	}

just make sure for bool in C# before pushing to lua to convert to string and make lowercase

ToString().ToLower()

I see. No hoenestly, i did not notice the Uppercase False. I will try doing lower case. :)

Link to comment
Share on other sites

38 minutes ago, camelot10 said:

so you see difference and didnt understand what happened?

there is no such definition in wow lua as False only false thats why is set that var to nil

 

	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark.ToString().ToLower() + "; print('PUSHED LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HunterMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result + " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HunterMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result + " ");
	}

just make sure for bool in C# before pushing to lua to convert to string and make lowercase

ToString().ToLower()

It still gives the same result. even with ToLower (I copy pasted your code)

Link to comment
Share on other sites

9 minutes ago, camelot10 said:

whats in log? wow & wrobot

install bugsack addon for wow and check what error catched

wow is not giving any errors.
It's still just writing:
PUSHED TO LUA VALUE HuntersMarkDisabled=
and WRobot writes the same as before:
18:28:49 - Wrobot push to LUA HunterMark=False return result=False isCorret=True
18:28:51 - Wrobot fetch from LUA HunterMark=False

Link to comment
Share on other sites

If i do /run print(HuntersMarkDisabled) it shows false now, instead of nil. So it seems that it worked.
But it still doesn't save the settings.

Edit: If i stop and start the bot, it also turns HuntersMarkDisabled into false even if it was true before i started the bot.
So it just defaults to false for some reason. Just as it did before.

Edited by Ordush
Link to comment
Share on other sites

	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark.ToString().ToLower() + "; print('PUSHED LUA VALUE HuntersMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result + " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HuntersMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result + " ");
	}

ok. here is main proble. you asked a quick example. i write you quick example in notepad. no spellchecking/code reviewing.

there is a simple typo, what lead to 2 hour of my brainf#cking. and main problem here that you refuse to think and analyze code, only copy-pasting and waiting solution

Edited by camelot10
Link to comment
Share on other sites

9 minutes ago, camelot10 said:
	void PushToLua()
	{
		var result = Lua.LuaDoString<bool>("HuntersMarkDisabled = " + HuntersMark.ToString().ToLower() + "; print('PUSHED LUA VALUE HuntersMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled;");
		Logging.Write("Wrobot push to LUA HunterMark=" + HuntersMark + " return result=" + result + " isCorret=" + (HuntersMark == result) + " ");
	}
	void FetchFromLua()
	{
		var result = Lua.LuaDoString<bool>("print('FETCH FROM LUA VALUE HuntersMarkDisabled=', HuntersMarkDisabled); return HuntersMarkDisabled");
		HuntersMark = result;
		Logging.Write("Wrobot fetch from LUA HunterMark=" + result + " ");
	}

ok. here is main proble. you asked a quick example. i write you quick example in notepad. no spellchecking/code reviewing.

there is a simple typo, what lead to 2 hour of my brainf#cking. and main problem here that you refuse to think and analyze code, only copy-pasting and waiting solution

There it is again, the condescending.. Why do you keep doing that?
I did not copy paste your code, i allready did change the typo in HunterMarkDisabled i also changed the typo in LUA HunterMark...
This did not change anything at all.

Still the same in-game and in the log.

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...