Thread Tools Display Modes
09-18-16, 03:46 PM   #1
vola313
A Murloc Raider
Join Date: Sep 2016
Posts: 6
Saving Data To File

Hi, I'm pretty new to coding in general so apologies if this is a simple task.

I'm experimenting with web dev and am trying to make a "completionist" website for WoW, which will require plenty of data. At the moment I'm working with lua (I'm aware it's probably easier to pull data from the Armoury instead, but I'd like to know how to do this in case I also develop an addon).

I've written this basic code which lists mount info in-game:

local mountIDs = C_MountJournal.GetMountIDs();

for i = 0, #mountIDs do
print(C_MountJournal.GetMountInfoByID(mountIDs[i]));
end

What's the easiest way to save this info to a file so I can work with it later? (With mounts I could feasibly copy-paste it all, but it would be useful to know how to do this if I end up working with bigger data sets for other things - transmog etc).

I've done a bit of research already and have read about SavedVariables being the only option. Is there any other way at the moment? Either way, what's the best way to go about it?

Sample code would be awesome, but I'm also happy just to be pointed in the right direction and work it out from there

Many thanks

Last edited by vola313 : 09-18-16 at 03:50 PM.
  Reply With Quote
09-18-16, 04:12 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
The only way to save to file is using saved variables. The link is to wowpedia.org which has multiple tutorials and an extensive reference to the game's API.

Keep in mind a few things:
  • You cannot choose when to write to file. Only the game can do that, and the most common times are logging out, exiting altogether, and reloading the user interface. You can do the last one manually with the /reloadui command.
  • Saved variables are global in scope, so name them something unique in your ToC. BillsExcellentDB is preferable than myDB, or worse, savedvariable. Appending "DB" to the end of the name is optonal, as it is a variable and can be called anything. You will notice most AddOns' SV is named after the AddOn: Bartender4DB, for example.
  • While the linked example uses the event "PLAYER_LOGOUT" to create a timestamp, registering for this event is not necessary. The game will write your SVs automatically during the event. You'd really only register for PL if you needed some data that was only available during the event.
  • World of Warcraft has no access to your hard drive in general, nor internet access outside of Blizzard servers. That means you cannot read/write to My Documents, nor can you surf the web.
  Reply With Quote
09-18-16, 07:15 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
#1 - You should familiarize yourself with the list of things addons/macros can't do.

#2 - Here is the code I use to dump pet journal data to saved variables:

Code:
## Interface: 70000
## SavedVariables: SANDBOXSV1
Sandbox1.lua
Code:
function DumpPets(t)
	t = t and wipe(t) or {}
	for i = 1, C_PetJournal.GetNumPets() do
		local _, speciesID, _, _, _, _, _, speciesName, _, _, npcID = C_PetJournal.GetPetInfoByIndex(i)
		tinsert(t, strjoin(";", speciesName, speciesID, npcID))
	end
	print("pet dump complete")
	return t
end
Code:
/run DumpPets(SANDBOXSV1)
/reload
The saved variables file for this addon then contains the following:

Code:
SANDBOXSV1 = {
	"Anubisath Idol;1155;68659", -- [1]
	"Arcane Eye;1160;68819", -- [2]
	...
}
... and I can easily convert that to CSV (just strip off the table wrapper, comments, and indentation) and do whatever I want with it.

You should be able to adapt this to mount journal data (or any other data) quite easily.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
09-19-16, 12:00 AM   #4
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by vola313 View Post
Code:
local mountIDs = C_MountJournal.GetMountIDs();

for i = 0, #mountIDs do
	print(C_MountJournal.GetMountInfoByID(mountIDs[i]));
end
Also keep in mind that Lua array-style tables start at 1, not 0.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote
09-19-16, 03:55 AM   #5
vola313
A Murloc Raider
Join Date: Sep 2016
Posts: 6
Thanks for the replies, all very useful info. I'll have a play around with it later!
  Reply With Quote
09-19-16, 12:49 PM   #6
vola313
A Murloc Raider
Join Date: Sep 2016
Posts: 6
Phanx I got your code to work fine with both mounts and pets - managed to end up with some nice CSV files using them. However I must've changed something and it's not working any more - the saved variables file is just defaulting to COMPLETIONISM = nil every time I run the function. Can't seem to work out what I've broken since I'm sure the code is exactly the same as before!

The table is populating fine, it's just not saving to the SavedVariables file any more. Any idea what might be causing this? Thanks

Edit: Got it working again, not really sure what the problem was! :S

Last edited by vola313 : 09-20-16 at 05:28 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Saving Data To File


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off