Persistent settings

Hello!

I have a question about persistently storing data in a command in c++. I have found that i can use CRhinoSettings for this. However, I run into some trouble using. Below is a very simple example command.

CRhinoCommand::result CCommandNewTestCommand::RunCommand(const CRhinoCommandContext& context)
{
int TestCounterValue = 0;
CRhinoSettings CommandSettings = this->Settings();

if (CommandSettings.GetInteger(L"TestCounter", TestCounterValue))
{
    RhinoApp().Print(L"Load succesfull!\n");
}

ON_wString str;
str.Format(L"Counter: %i\n", TestCounterValue);
RhinoApp().Print(str);

TestCounterValue++;

CommandSettings.SetInteger(L"TestCounter", TestCounterValue);

return CRhinoCommand::success;
}

I expect this to simply increment the counter every time I run the command. However, this is the output I get:
image

It does increment between successive debug runs, as can be seen from the fact that the counter has gotten up to 14. This is surprising to me. In order to check if this is me having the wrong expectations.

Below is a similar command in c#.

    protected override Result RunCommand(RhinoDoc doc, RunMode mode)
    {

        double DoubleVal;
        string Key = "VerySpecificKey";
        if (this.Settings.TryGetDouble(Key, out DoubleVal))
        {
            RhinoApp.WriteLine("Loading successfull!");
            DoubleVal = DoubleVal + 1.0f;
        }
        else
        {
            DoubleVal = 8.12;
        }

        this.Settings.SetDouble(Key, DoubleVal);

        RhinoApp.WriteLine("The {0} command set the value of {1} to {2}", EnglishName, Key, DoubleVal);

        // ---
        return Result.Success;
    }

This produces the output:
image

This is more along the lines of what I expect the output to be. The question then is: what am I doing wrong in c++ that I can not get the same result?

You are creating a copy of your command settings. Use
CRhinoSettings& CommandSettings = this->Settings();

Notice the addition of the &

Thanks, this entirely solves my problem! But I’m not entirely sure why it does. I understand that I get a copy of the settings object and use that to set a new value to wherever it is saved the first time round I call the command. This acts through the copy and not the original Settings object. The copy then gets out of scope at the end of the command.

Why then do i get zero the second time round? Why not either the value of the settings command before I updated it or the value at the save location?

I haven’t had a chance to test this code. Have you tried stepping through this in the debugger? Do you go into the first if statement every time?