Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Inventory Log
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Wed 12 Feb 2014 03:42 AM (UTC) |
| Message
| I'm attempting to create a log of character inventories and output this to a single file (ideally), though even separate files would work. I've run into two problems
I'm using WorldLog ("item") to log it to a file. I don't want it logging everything so I only have it log certain information, and every time the log opens it erases all previous data in the log, is there anyways to make it just append the log? What I'm really trying to achieve is something set up like:
Character1:
item
item
item
Character2:
item
item
item
If I have to do a separate log for each character thats not a horrible issue, but it would be much easier with just one txt file.
The second issue is erasing information from a file in lua, is there anyway to read whats in the file and remove something? That way if a character drops an item it will be erased from the log?
I know I can achieve this in C++, but that doesn't do much good and I'm hoping I can do it in Lua | | Top |
|
| Posted by
| Fiendish
USA (2,550 posts) Bio
Global Moderator |
| Date
| Reply #1 on Thu 13 Feb 2014 04:38 PM (UTC) |
| Message
| |
| Posted by
| Nick Gammon
Australia (23,166 posts) Bio
Forum Administrator |
| Date
| Reply #2 on Thu 13 Feb 2014 07:56 PM (UTC) |
| Message
| |
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Reply #3 on Mon 17 Feb 2014 04:49 AM (UTC) |
| Message
| So I took a look at Tables and Databases, tables I somewhat understood. Databases I didn't at all. Then I found the Lua I/O Library read over that and found pretty much everything I wanted.
I have my script around 75% or more finished. It notices when I log into a specific character and open/create the appropriate .txt file. Then everytime that character 'gets' an item ("You get a sword.") it writes "a sword" followed by a newline to "character.txt" and a flush. When the character logs out it closes the file.
The final task, which I can't seem to find anything useful on, is to remove an item that is dropped. So the line "You drop a sword." would fire a trigger that checks the file for "a sword" and when found erases it from the file.
Any suggestions? | | Top |
|
| Posted by
| Fiendish
USA (2,550 posts) Bio
Global Moderator |
| Date
| Reply #4 on Mon 17 Feb 2014 05:07 AM (UTC) |
| Message
| | When the character logs in, load the file and read inventory into a Lua table. When items are gained and dropped, modify the table but don't write anything to the file. When the character logs out, write the contents to the file. There's no great reason to use a text file on your hard drive for random access temporary storage. |
https://github.com/fiendish/aardwolfclientpackage | | Top |
|
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Reply #5 on Mon 17 Feb 2014 06:10 AM (UTC) |
| Message
| I apparently understand even less about tables than I thought, I'm currently reading over http://lua-users.org/wiki/TablesTutorial and I don't see how I'm supposed to achieve my goal.
We'll say that my file contains:
two small brass lanterns
a moon pendant
a red cultist's robe
two skull masks
a ruelbone hairpin
a copper halberd
a small brass lantern
a copper medallion
lacquered greaves
a targe
a falcata
a heavy carven staff
eldimleaf
a razor-barbed spiked chain
a black silk bandana
dark robes
I can't figure out how to make each one of those items it's own table, let alone how to reverse it and take my tables and make them lines in a text file.
The trigger I tried first just to create a table when I get an item..
<triggers>
<trigger
enabled="y"
match="You get a *."
send_to="12"
sequence="100"
>
<send>inv.%1= {
name = '%1'
}</send>
</trigger>
</triggers>
Which didn't produce and error, but it didn't seem to do anything else. Needless to say I'm pretty lost when it comes to tables. | | Top |
|
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Reply #6 on Mon 17 Feb 2014 07:26 AM (UTC) |
| Message
| After a lot of thinking and reworking I came up with this here.
<aliases>
<alias
match="inventory"
enabled="y"
send_to="12"
sequence="100"
>
<send> for line in io.lines("Francis.txt") do
table.insert (inventory, line)
end
table.foreachi (inventory, print)
</send>
</alias>
</aliases>
This works as intended as gives the following as output as a note1 two small brass lanterns
2 a moon pendant
3 a red cultist's robe
4 two skull masks
5 a ruelbone hairpin
6 a copper halberd
7 a small brass lantern
8 a copper medallion
9 lacquered greaves
10 a targe
11 a falcata
12 a heavy carven staff
13 eldimleaf
14 a razor-barbed spiked chain
15 a black silk bandana
16 dark robes
17 a black silk bandana
This tells me that it's properly reading the file and inserting everything into a table, I can change it from doing so via an alias into the log in trigger rather easily this was just better for testing. I still don't know how to remove one of the lines when I drop an item, I know how to remove a line manually (or at least in theory I do). I don't know how to make it automatically remove line #6 when I drop the halberd though.
Then there's the final step of opening the character file and rewriting it. I'm inching closer to the goal at least! | | Top |
|
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Reply #7 on Tue 18 Feb 2014 12:39 AM (UTC) |
| Message
| This is how I'm attempting to delete a specific line, but it's achieving nothing that I can see.
<triggers>
<trigger
enabled="y"
group="Inventory Log"
match="You drop *."
name="Dropper"
omit_from_log="y"
send_to="12"
sequence="100"
>
<send>inventory["%1"] = nil</send>
</trigger>
</triggers>
If I change "%1" to 1 then it deletes the first entry which isn't quite what I'm needing. If I change "%1" to %1 (no quotes) then it returns the following error.
Compile error
World: Blood Dusk 1
Immediate execution
[string "Trigger: Dropper"]:1: ']' expected near 'red'
This was when I was dropping "a red cultist's robe" | | Top |
|
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Reply #8 on Tue 18 Feb 2014 05:44 AM (UTC) |
| Message
| | Just another update looking for some insight. I think what I need to utilize is string.match to match the wildcard from drop and remove it from the table or file, but I don't know how to get it to work off a wildcard, heh. | | Top |
|
| Posted by
| Nick Gammon
Australia (23,166 posts) Bio
Forum Administrator |
| Date
| Reply #9 on Tue 18 Feb 2014 07:14 AM (UTC) |
| Message
| | Try some debugging prints. Eg. print what %1 is. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Panaku
(34 posts) Bio
|
| Date
| Reply #10 on Sun 23 Feb 2014 03:36 AM (UTC) |
| Message
| When I do a "print %1" when dropping it prints exactly what it should "a red cultist's robe" for example.
Run-time error
World: Blood Dusk 1
Immediate execution
[string "Trigger: Dropper"]:2: table index is nil
stack traceback:
[string "Trigger: Dropper"]:2: in main chunk
This is what happens if I test the trigger I posted earlier using a single worded item "You drop knife." Even though actual output would never look like that.
You drop a red cultist's robe.
Compile error
World: Blood Dusk 1
Immediate execution
[string "Trigger: Dropper"]:2: ']' expected near 'red'
This is what happens if I drop an item with multiple words. | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
36,211 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top