How to write to a file in wow addon

来源网址:  https://us.battle.net/forums/en/wow/topic/20043655806

 

 

 

World of Warcraft
Forums
UI and Macro
Can an addon write to a log file?
The World of Warcraft community forums have moved!
These forums are in read-only mode. Please visit the new World of Warcraft community forums at https://us.forums.blizzard.com/en/wow

Can an addon write to a log file?
UI AND MACRO

Cold
<God Tier>
110 Human Mage
11030
3138 posts
Dec 12, 2015
Does wow allow addons to write data to a txt file for example?

Gello
<Underhill>
100 Gnome Priest
16605
4654 posts
Dec 12, 2015
Not directly. All file i/o is removed from addons.

However, an addon can tag a variable to be saved in the SavedVariables folder under the addon’s name. An addon can tag it to save in a global file or a per-character file.

And this variable can record stuff over time to create a log. (It’s how addons can keep track of stuff over time)

While this file can’t be forced into a format immediately recognizable to an outside program (unless it reads lua files), there may be an ODBC written up for doing this to convert to Access or maybe a csv for Excel or something.

Cold
<God Tier>
110 Human Mage
11030
3138 posts
Dec 12, 2015
Any chance you could link me to what you are talking about?

Every page I looked at has a common theme: Blizzard won’t allow io because of bots.

Never even heard a mention about what you just said.

Gello
<Underhill>
100 Gnome Priest
16605
4654 posts
Dec 12, 2015
I don’t know if this would qualify as a link for this intended purpose but:
http://wow.gamepedia.com/Saving_variables_between_game_sessions

It’s easier to explain by example. Create the two files named in bold in a folder called TargetLog under AddOns:

TargetLog.toc
## Interface: 60200
## Title: TargetLog
## Notes: Uses SavedVariables to log targets across multiple sessions
## SavedVariables: TargetLogData
TargetLog.lua

TargetLog.lua
TargetLogData = {} — initial value (will be overwritten when/if SavedVariables load)
local f = CreateFrame(“Frame”)
f:SetScript(“OnEvent”,function(self)
if UnitExists(“target”) then
local name = UnitName(“target”)
local level = UnitLevel(“target”)
local health = UnitHealthMax(“target”)
— adds date/time,name,level,health of target to TargetLogData
tinsert(TargetLogData,format(“%s,%s,%s,%s”,date(),name,level,health))
end
end)
f:RegisterEvent(“PLAYER_TARGET_CHANGED”)

The next time you log in, as you target stuff, it gathers a few bits of info about your target and adds it to a table (TargetLogData). This data will accumulate across sessions (the purpose of SavedVariables is to keep stuff between sessions).

Note the ## SavedVariables: TargetLogData in the .toc. This is where you tag a variable to save. Then the addon itself fills the variable (almost always a table) with stuff.

Now when you logout or reload (and ONLY when you logout or reload–addons can’t control when this stuff writes beyond forcing a /reload which a user is generally not going to want while they play), the contents of TargetLogData is saved to the file WTF\Account\%accountname%\SavedVariables\TargetLog.lua. This is a text file that looks like:

TargetLogData = {
“12/12/15 00:19:57,Lunarfall Infantry,100,195708”, — [1]
“12/12/15 00:19:59,Muradin Bronzebeard,100,29356200”, — [2]
“12/12/15 00:20:02,Deedree,90,17503”, — [3]
“12/12/15 00:20:05,Captain Towix,100,97854”, — [4]
“12/12/15 00:20:06,Officer Jeor,100,97854”, — [5]
“12/12/15 00:20:08,Alliance Sailor,100,1467810”, — [6]
“12/12/15 00:20:09,Lieutenant Thorn,94,63226”, — [7]
“12/12/15 00:20:11,Shield Captain Chien,100,97854”, — [8]
“12/12/15 00:20:13,Sentinel Dawnshadow,100,97854”, — [9]
“12/12/15 00:20:15,Ro,100,385260”, — [10]
“12/12/15 00:20:18,Lunarfall Infantry,100,195708”, — [11]
“12/12/15 00:20:19,Porter,2,50”, — [12]
“12/12/15 00:20:22,Seer Kazal,100,97854”, — [13]
“12/12/15 00:20:24,Aenir,90,14002”, — [14]
“12/12/15 00:20:25,Squiggy,90,35006”, — [15]
“12/12/15 00:20:27,Rooter the Ravenous,100,146781”, — [16]
“12/12/15 00:20:28,Lio the Lioness,98,65312”, — [17]
“12/12/15 00:20:29,Quintessence of Light,25,788”, — [18]
}

This is a lua table of the data saved. When you log in later, part of the addon loading process is to run any existing SavedVariables files, so the table you saved on last logout is created with previous data on next login.

That is the complete and total extent that addons are allowed to write files.

An addon cannot change the structure of this file. The table name and syntax (table = { stuff, stuff, stuff, etc }) is there so it can be run. An addon has no control over this. The game creates it for its own purpose and makes no accomidation for outside programs to read it. If they ever change the structure of this file it would probably be to encrypt it so outside programs can’t use it.

To use it as a log for an outside program would require massaging this text to remove the syntax stuff to get a CSV or whatever format you choose. And it’s important to remember that this stuff only writes on a logout/reload. They don’t want it writing in realtime to prevent outside programs from learning about what’s going on in the game.

Cold
<God Tier>
110 Human Mage
11030
3138 posts
Dec 12, 2015
Thanks!
Join the Conversation
Have something to say? Log in to join the conversation.
Thanks for visiting the Blizzard Forums (3.8.2) · Patch Notes
CAREERSABOUTSUPPORTCONTACT USPRESSAPI
©2019 BLIZZARD ENTERTAINMENT, INC. ALL RIGHTS RESERVED.
All trademarks referenced herein are the properties of their respective owners.
PRIVACYLEGALTERMSCOOKIES
en-US