PDA

View Full Version : Saving and loading help


Queekusme
11-12-2011, 10:25 AM
Hiya,

For Kmabc 1.2 i am implementing saving and loading. But however, i am at a standstill:(. I am trying to save quickslot data to the plugindata but cannot get anywhere. the code so far is:
local savedatax ={
quickslot1SavData = "",
quickslot2SavData = "",
quickslot3SavData = "",
quickslot4SavData = "",
quickslot5SavData = "",
quickslot6SavData = "",
quickslot7SavData = "",
quickslot8SavData = "",
quickslot9SavData = "",
}
and
Save.Click = function( sender , args )

savedatax.quickslot1SavData = quickslot1:GetShortcut();
savedatax.quickslot2SavData = quickslot2:GetShortcut();
savedatax.quickslot3SavData = quickslot3:GetShortcut();
savedatax.quickslot4SavData = quickslot4:GetShortcut();
savedatax.quickslot5SavData = quickslot5:GetShortcut();
savedatax.quickslot6SavData = quickslot6:GetShortcut();
savedatax.quickslot7SavData = quickslot7:GetShortcut();
savedatax.quickslot8SavData = quickslot8:GetShortcut();
savedatax.quickslot9SavData = quickslot9:GetShortcut();

Turbine.Shell.WriteLine("Pre Save");

Turbine.PluginData.Save(Turbine.DataScope.Characte r, "KmabcSaveData", savedatax); --

Turbine.Shell.WriteLine("Post Save");
end

can anyone point me in the right direction please

thanks Queekusme

PS. i get the file with the names in but no value to put into them.

PPS. i know how to put plain text into the savedata file but not quickslot data so i request that people dont tell me this.

Cearbhall
11-12-2011, 11:20 AM
the reason that is not working is because :GetShortcut() returns the class Turbine.UI.Lotro.Shortcut
which isn't the data you want

this is what i do

quickslot.ShortcutChanged = function(sender, args)
local shortcut = sender:GetShortcut()
local type, data = shortcut:GetType(), shortcut:GetData()
if type == Turbine.UI.Lotro.ShortcutType.Undef then
DB.quickslots[sender._index] = nil
else
DB.quickslots[sender._index] = {type, data}
end
end

--and to load the data

if DB.quickslots[quickslot._index] then
quickslot:SetShortcut(Turbine.UI.Lotro.Shortcut(DB .quickslots[quickslot._index][1], DB.quickslots[quickslot._index][2]))
end


--quickslot._index is something i set to show what slot it represented

Queekusme
11-12-2011, 02:12 PM
I dont really understand this, what i want to do is save the data to the file so that it puts a hexcode there... how can i use this to make it do that?

MrJackdaw
11-12-2011, 04:54 PM
local type, data = shortcut:GetType(), shortcut:GetData()
Is the important part here and the data you need - this is the hex code for the skill and the type of skill. These are also what you use when you set a skill.

Do you use a bar plugin at all? If you do you should be able to extract the codes you need from its save data.

Alternatively

shortcut= quickslot:GetShortcut()
type, data = shortcut:GetType(), shortcut:GetData()

Will get you the data you need from one quickslot that you build. Obviously, save etc as you wish!

Queekusme
11-13-2011, 06:04 AM
So how do i put that in the context of my code is it something like



local savedatax ={

shortcut = quickslot1:GetShortcut()
local type, data = shortcut:GetType(), shortcut:GetData()

quickslot1SavData = type,data,
-- And then copy for the other shortcuts...
}


and to load it (copied from Cearbhall)

quickslot1:SetShortcut(Turbine.UI.Lotro.Shortcut(t ype value,data value)) -- type and data values called from the loadup process

Garan
11-13-2011, 09:43 AM
quickslot1:SetShortcut(Turbine.UI.Lotro.Shortcut(t ype value,data value)) -- type and data values called from the loadup process

If there is any chance that your saved quickslot data could have an Alias type shortcut, you can run into a Turbine bug using the default Shortcut constructor with international characters. To avoid that issue with german and french users, use the :SetData() method to set the shortcut like:

local sc=Turbine.UI.Lotro.Shortcut();
sc:SetType(typeValue);
sc:SetData(dataValue); -- works with international characters
Quickslot1:SetShortcut(sc);


It's not as pretty but it avoids the international character bug in the constructor. You should also be using some form of encoding for your save data and load data routines (I use Vindar's patch available here on LoTROInterface.com)

Queekusme
11-13-2011, 01:42 PM
Thanks for the help, i'll try this tomorrow, and i'll look into vindir....'s patch as encoding allways helps

thanks again

Queekusme

EDIT: thanks again for this but i never intended for there to be alias strings to be saved anyway, but thanks as this will prevent people complaining :)

Equendil
11-13-2011, 08:30 PM
So how do i put that in the context of my code is it something like



local savedatax ={

shortcut = quickslot1:GetShortcut()
local type, data = shortcut:GetType(), shortcut:GetData()

quickslot1SavData = type,data,
-- And then copy for the other shortcuts...
}


That wouldn't work.

If you write "a,b = x,y" a is set to the value of x and b is set the value of y. If you write "a =x,y" a is set to the value of x and y is discarded.

In your case, 'quickslot1SavData' would be set to the value of 'type' and 'data' would be discarded.

You could however, make your quickslotxSavData tables then set both type & data:

quickslot1SavData = {}
quickslot2SavData = {}
...


(...)
quickslot1SavData.type, quickslot1SavData.data = type, data
(...)

Queekusme
11-14-2011, 11:08 AM
So what you're saying is that i should save each piece seperately insted of in the same piece of information string.

i see where you are getting at, ok, i'll try this after my tea and homework tonight

Equendil
11-14-2011, 04:07 PM
So what you're saying is that i should save each piece seperately insted of in the same piece of information string.
Well, you *can't* set one variable to two values.

If you write "quickslot1SavData = type,data", what the lua runtime does is "quickslot1SavData = type", and then it discards 'data'.

See Lua Reference Manual (http://www.lua.org/manual/5.1/manual.html#2.4.3)

Before the assignment, the list of values is adjusted to the length of the list of variables. If there are more values than needed, the excess values are thrown away. If there are fewer values than needed, the list is extended with as many nil's as needed.

Queekusme
11-15-2011, 02:28 PM
right, so i need to set each datastring of quickslot(n)savedatax to a table with data and type

(where n = quickslot's number)

e.g.



shortcut(n) = quickslot(n):GetShortcut()
local type(n) = shortcut(n):GetType(),
local data(n) = shortcut(n):GetData()

quickslot1SavData = type,data,
-- And then copy for the other shortcuts...
}

local savedatax ={
quickslot(n)SavData = { Data = data(n),
Type = type(n),
}
...
}


and for loading:



savedatax = Turbine.etc.loading(Blardyblar)

local sc(n)=Turbine.UI.Lotro.Shortcut();
sc(n):SetType(savedatax.quickslot(n)SavData.Type);
sc(n):SetData((savedatax.quickslot(n)SavData.Data) ;
quickslot(n):SetShortcut(sc(n));



God that's a bit confusing! :eek: lets try it shall we...


SRY 4 FORMATTING

Queekusme
11-15-2011, 02:55 PM
ok it's not working!

i can get the type value of 0.000000 but no data, i've tried items, skills and alias'... here's the code:



Save = Turbine.UI.Button();
Save:SetParent( window );
Save:SetPosition( 210, 34 );
Save:SetSize( 78, 15 );
Save:SetBackColor( Turbine.UI.Color( 0.0, 0.05, 1.0 ) );
Save:SetText( ""..Lang[1][21] ); --for lang files
Save:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter );
Save:SetForeColor( Turbine.UI.Color( 0.0, 0.0, 0.0 ) );

local savedatax ={
quickslot1SavData = { Data = "",
Type = "",
}
}

shortcut1 = quickslot1:GetShortcut();
local type1 = shortcut1:GetType();
local data1 = shortcut1:GetData();

Save.Click = function( sender , args )

savedatax.quickslot1SavData.Data = data1;
savedatax.quickslot1SavData.Type = type1;

Turbine.Shell.WriteLine("Pre Save");

Turbine.PluginData.Save(Turbine.DataScope.Characte r, "KmabcSaveData", savedatax);

Turbine.Shell.WriteLine("Post Save");
end

Equendil
11-16-2011, 01:04 AM
You seem to be building your data for saving where you initialize the UI. At that point I imagine your quickslot is empty.

Queekusme
11-16-2011, 02:10 AM
You seem to be building your data for saving where you initialize the UI. At that point I imagine your quickslot is empty.

so... how do i sort this? I've never done this before and so have no clue what i'm doing really.

Equendil
11-16-2011, 02:20 AM
so... how do i sort this? I've never done this before and so have no clue what i'm doing really.

Move that stuff:

shortcut1 = quickslot1:GetShortcut();
local type1 = shortcut1:GetType();
local data1 = shortcut1:GetData();

to where you save the data.

Queekusme
11-16-2011, 01:40 PM
so in savedatax, i'll try it now!

Queekusme
11-16-2011, 02:04 PM
still not getting the data,

Save = Turbine.UI.Button();
Save:SetParent( window );
Save:SetPosition( 210, 34 );
Save:SetSize( 78, 15 );
Save:SetBackColor( Turbine.UI.Color( 0.0, 0.05, 1.0 ) );
Save:SetText( ""..Lang[1][21] );
Save:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter );
Save:SetForeColor( Turbine.UI.Color( 0.0, 0.0, 0.0 ) );

shortcut1 = quickslot1:GetShortcut();

local savedatax ={
quickslot1SavData = { Data = shortcut1:GetType(),
Type = shortcut1:GetData(),
}
}

quickslot1.ShortcutChanged = function( sender , args ) -- I changed this to save me time whilst clicking the button, Should be Save.Click...

savedatax.quickslot1SavData.Data = shortcut1:GetData();
savedatax.quickslot1SavData.Type = shortcut1:GetType();

Turbine.Shell.WriteLine("Pre Save");

Turbine.PluginData.Save(Turbine.DataScope.Characte r, "KmabcSaveData", savedatax);

Turbine.Shell.WriteLine("Post Save");
end

could people copy this into their posts when they reply so i can see exactly what you are referring to, as it would really help me,

thanks

PS, when i change the line:
shortcut1 = quickslot1:GetShortcut();

to
shortcut1 = quickslotlyrical:GetShortcut();

it gets the value for the link, but not on any of the quickslots with items :(

Equendil
11-17-2011, 01:15 PM
If your quickslot has changed, you must reacquire the shortcut. Move "shortcut1 = quickslot1:GetShortcut();" to when you save things.

There is no magic performed by the lua runtime, if you get components/data before shortcuts are set, you'll get empty shortcuts.


Save = Turbine.UI.Button();
Save:SetParent( window );
Save:SetPosition( 210, 34 );
Save:SetSize( 78, 15 );
Save:SetBackColor( Turbine.UI.Color( 0.0, 0.05, 1.0 ) );
Save:SetText( ""..Lang[1][21] );
Save:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter );
Save:SetForeColor( Turbine.UI.Color( 0.0, 0.0, 0.0 ) );

local savedatax ={
quickslot1SavData = { Data = shortcut1:GetType(),
Type = shortcut1:GetData(),
}
}

quickslot1.ShortcutChanged = function( sender , args ) -- I changed this to save me time whilst clicking the button, Should be Save.Click...
shortcut1 = quickslot1:GetShortcut();
savedatax.quickslot1SavData.Data = shortcut1:GetData();
savedatax.quickslot1SavData.Type = shortcut1:GetType();

Turbine.Shell.WriteLine("Pre Save");

Turbine.PluginData.Save(Turbine.DataScope.Characte r, "KmabcSaveData", savedatax);

Turbine.Shell.WriteLine("Post Save");
end

Queekusme
11-18-2011, 02:04 AM
IT'S WORKING

For anyone else's reference:


Save = Turbine.UI.Button(); -- Not used anymore but will keep in
Save:SetParent( window );
Save:SetPosition( 210, 34 );
Save:SetSize( 78, 15 );
Save:SetBackColor( Turbine.UI.Color( 0.0, 0.05, 1.0 ) );
Save:SetText( ""..Lang[1][21] );
Save:SetTextAlignment( Turbine.UI.ContentAlignment.MiddleCenter );
Save:SetForeColor( Turbine.UI.Color( 0.0, 0.0, 0.0 ) );

shortcut = quickslot1:GetShortcut();

local savedatax ={
quickslot1SavData = { Data = shortcut:GetType(),
Type = shortcut:GetData(),
}
}

quickslot1.ShortcutChanged = function( sender , args )

shortcut1 = quickslot1:GetShortcut();
savedatax.quickslot1SavData.Data = shortcut1:GetData();
savedatax.quickslot1SavData.Type = shortcut1:GetType();

Turbine.Shell.WriteLine("Pre Save");

Turbine.PluginData.Save(Turbine.DataScope.Characte r, "KmabcSaveData", savedatax);

Turbine.Shell.WriteLine("Post Save");
end