• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Saving text to files!

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Hello, have you ever made your own command or any other kind of script and you wanted to save some information in a text file?

Okay, here we go; your dream has just became true! :thumbup:
Code for saving to files:
PHP:
local saveFile = {folder="logs/", filename="saves.txt", saveLog=true, linesMax=1000} 

if (saveFile.saveLog == true) then 
    local lines = 0 
    for _ in io.lines(saveFile.folder .. saveFile.filename) do 
        lines = lines+1 
    end 
    local file = io.open(saveFile.folder .. saveFile.filename, (lines < linesMax-1 and "a") or "w") 
    file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n") 
    file:close() 
end

This script saves the text "HERE GOES THE TEXT" BUT it also reset the whole file when it reach 1000 lines :blink:
If you don't want that:
PHP:
local saveFile = {folder="logs/", filename="saves.txt", saveLog=true} 

if (saveFile.saveLog == true) then 
    local file = io.open(saveFile.folder .. saveFile.filename, "a") 
    file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n") 
    file:close() 
end

And keep \n there, it makes the newline, otherwise it will all come like "[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE[date here] TEXT GOES HERE" and you don't want that to happen, huh?

Also, if the file doesn't exist it will be created!


The "write modes" for io.open() is:
"r" read mode (the default);
"w" write mode;
"a" append mode;
"r+" update mode, all previous data is preserved;
"w+" update mode, all previous data is erased;
"a+" append update mode, previous data is preserved, writing is only allowed at the end of file.
That's for io.open("file", "a")


And a simple fileExists function:
PHP:
function fileExists(filename) 
    file=io.open(filename, "r") 
    return (file == nil and 0) or file:close() and 1
end
Returns 1 if file exists, and 0 if not exists.
PHP:
if fileExists(filename) == 1 then
    ...
end

An other version:
PHP:
function fileExists(filename) 
    file=io.open(filename, "r") 
    return (file ~= nil and file:close())
end
Returns true if file exists, and false if not exists.
PHP:
if fileExists(filename) then
    ...
end


The date formats:
%a abbreviated weekday name (e.g., Wed)
%A full weekday name (e.g., Wednesday)
%b abbreviated month name (e.g., Sep)
%B full month name (e.g., September)
%c date and time (e.g., 09/16/98 23:48:10)
%d day of the month (16) [01-31]
%H hour, using a 24-hour clock (23) [00-23]
%I hour, using a 12-hour clock (11) [01-12]
%M minute (48) [00-59]
%m month (09) [01-12]
%p either "am" or "pm" (pm)
%S second (10) [00-61]
%w weekday (3) [0-6 = Sunday-Saturday]
%x date (e.g., 09/16/98)
%X time (e.g., 23:48:10)
%Y full year (1998)
%y two-digit year (98) [00-99]
%% the character `%´

Gimme rep if you like/will use it :D

Regards,
Colandus
 
Last edited:
I had this comment:
brilliant, btw can you pm how to know whether a gm summons a monster or not, i would really like to know how to do this as i dont want any of my gm's summoning monsters to any players.

But I can't see the names unfortunately, so who wrote it? ^.-
 
It was me, would that be possible?

I think, but I'm not sure if it can tell which monster :/

Just make a talkaction called the same as that command (e.g. /m) and make so it ONLY writes to the file :p
 
yeh, good idea. Or i will just remove the summon monster command out of commands.xml. Why didnt i think of this earlier. Lol. Ty anyway.
 
is it possible to allow every words?
something like <talkaction words="*" script="log"/>
 
@Boy67: Why don't you enable command logs for your GM's. Then it will outprint ALL of the commands they have used. All you need to then do is go 'ctrl + f' and search for the term '/m'.

The logs are presented like this:
[09/03/2008 19:28] /goto ned
[09/03/2008 19:28] /goto kakan
[09/03/2008 19:28] /goto frodo
[09/03/2008 19:28] /goto bill
[09/03/2008 19:28] /goto spider
[09/03/2008 19:29] /c spider
[09/03/2008 19:29] /c orshabaal
[09/03/2008 19:29] /c demon
[09/03/2008 19:30] /goto player
[09/03/2008 19:30] /goto wolf
[09/03/2008 19:31] /goto demon
[09/03/2008 19:31] /c grapes
[10/03/2008 17:00] /c demon
 
at last i can save commands log i had many commands but they wasn't saved,
ty for posting it here in Otland.
 
Sorry for double posting but i wanted to bump it up to post prob i am facing

I can't understand how should i add it to a command.
add it for this for example:
+
PHP:
bool Commands::ghost(Creature* creature, const std::string& cmd, const std::string& param)
{
	Player* player = creature->getPlayer();
	if(player)
	{
		player->ghostMode = !player->ghostMode;
		char buffer[60];
		sprintf(buffer, "You have switched ghost mode mode to: %s.", (player->ghostMode ? "enabled" : "disabled"));
		player->sendTextMessage(MSG_INFO_DESCR, buffer);
		return true;
	}
	return false;
}
 
oh, ty alot also then useful as well gonna try adding it and i will till u.

hmm couldn't add it
PHP:
function onSay(cid, words, param)
if getPlayerGroupId(cid) > 4 then
doSetItemOutfit(cid, 2701, 60000)
else
            doPlayerSendCancel(cid, "Only Gamemasters may change his look to a tree.")
    end
end
 
Last edited:
PHP:
function onSay(cid, words, param)
    if getPlayerGroupId(cid) > 4 then
        doSetItemOutfit(cid, 2701, 60000)
        local file = io.open(saveFile.folder .. saveFile.filename, "a")  
        file:write("[" .. os.date("%Y-%m-%d %H:%M:%S") .. "] HERE GOES TEXT\n")  
        file:close()  
    else
        doPlayerSendCancel(cid, "Only Gamemasters may change his look to a tree.")
    end
end
 
I get an error telling me "attempt to index global 'io' <a nil value>

I need to edit a file saying this...
guilds = {['guild 1'] = 0, ['guild 2'] = 1, ['guild 3'] = 3}
and all i need to do is add more to it...
guilds = {['guild 1'] = 0, ['guild 2'] = 1, ['guild 3'] = 3, ['guild 4'] = 4}

how do i make it remove the last "}" and then add the ", ['guild 4'] = 4}"

and why is "io" not working?
 
Back
Top