Register forum user name Search FAQ

Gammon Forum

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.
 Entire forum ➜ MUSHclient ➜ Lua ➜ XP (or other stats) gained per given period

XP (or other stats) gained per given period

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Hidelensman   (10 posts)  Bio
Date Tue 23 Aug 2022 12:35 PM (UTC)

Amended on Fri 26 Aug 2022 01:47 PM (UTC) by Hidelensman

Message
Hi Nick and all,

As a newbie to mushclient, I am trying to follow Nick's reply in this post to build up my own tracker for a MUD I'm playing:
https://mushclient.com/forum/?id=8843&reply=4#reply4

Here is my code:

date = os.date ("%d-%m-%Y")  -- eg. 06-08-2008

xp_gained [date] = (xp_gained [date] or 0) + xp

xp = %2  -- the xp gained


%2 is set by the trigger as below(sorry the description is in Traditional Chinese since I'm playing a MUD built up and run in Taiwan):

^吒]為你殺死了(.*),得到了\s+(\d+)點經驗值和\s+(\d+)點潛能以及\s+(\d+)點聲望。$


The trigger is successfully triggered, while I encountered this error message and could not resolve it after trail and errors for several days:

Run-time error
World: 再戰江湖
Immediate execution
[string "Trigger: "]:3: attempt to index global 'xp_gained' (a nil value)
stack traceback:
[string "Trigger: "]:3: in main chunk


Could Nick or anyone shed some light on this? Ideally, I want to track 3 stats (XP, POT, and SC) after killing mobs in the given period (hours or days) and show in a table.

Thanks for help in advance.
Top

Posted by AdInfinitum   (74 posts)  Bio
Date Reply #1 on Tue 23 Aug 2022 03:00 PM (UTC)
Message
Hidelensman said:


xp_gained [date] = (xp_gained [date] or 0) + xp

Run-time error
World: 再戰江湖
Immediate execution
[string "Trigger: "]:3: attempt to index global 'xp_gained' (a nil value)
stack traceback:
[string "Trigger: "]:3: in main chunk


The error simply states that "xp_gained" is nil. You'll need to make sure that "xp_gained" is previously defined as a table prior to trying to pull information from it. If you create a new table daily, then the easiest way will be:


xp_gained = {}


If you tend to save the table over time, you'll want to save it as a variable and load it each time you restart. See https://mushclient.com/forum/?id=6030&reply=11#reply11

Hopefully this helps break it down for you.
Top

Posted by Nick Gammon   Australia  (23,098 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 24 Aug 2022 12:39 AM (UTC)
Message
If you are doing that in a trigger you don't want to make the table every time. Try:


if not xp_gained then
  xp_gained = {}  -- make table if it doesn't exist
end

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Hidelensman   (10 posts)  Bio
Date Reply #3 on Fri 26 Aug 2022 02:43 PM (UTC)

Amended on Sat 27 Aug 2022 01:45 AM (UTC) by Hidelensman

Message
AdInfinitum said:


The error simply states that "xp_gained" is nil. You'll need to make sure that "xp_gained" is previously defined as a table prior to trying to pull information from it. If you create a new table daily, then the easiest way will be:


xp_gained = {}


If you tend to save the table over time, you'll want to save it as a variable and load it each time you restart. See https://mushclient.com/forum/?id=6030&reply=11#reply11

Hopefully this helps break it down for you.


Thanks for the explanation and the tips to fix it.

Nick Gammon said:

If you are doing that in a trigger you don't want to make the table every time. Try:


if not xp_gained then
  xp_gained = {}  -- make table if it doesn't exist
end



It works now - thanks for the sharing.

However, when I further tried to replicate in the same trigger with the similar codes for other 2 stats captured by %3 and %4, I bumped into another error message:

Run-time error
World: 再戰江湖
Immediate execution
[string "Trigger: "]:12: attempt to perform arithmetic on global 'sc' (a nil value)
stack traceback:
[string "Trigger: "]:12: in main chunk

My code is like below:

date = os.date ("%d-%m-%Y")  

if not xp_gained then
  xp_gained = {}  -- make table if it doesn't exist
end

if not pot_gained then
  pot_gained = {}  -- make table if it doesn't exist
end

if not sc_gained then
  sc_gained = {}  -- make table if it doesn't exist
end

xp_gained [date] = (xp_gained [date] or 0) + xp
pot_gained [date] = (pot_gained [date] or 0) + pot
sc_gained [date] = (sc_gained [date] or 0) + sc

xp = %2  -- the xp gained
pot = %3  -- the pot gained
sc = %4  -- the sc gained


May I have your guidance about what went wrong? Thanks.
Top

Posted by Nick Gammon   Australia  (23,098 posts)  Bio   Forum Administrator
Date Reply #4 on Sat 27 Aug 2022 05:17 AM (UTC)
Message


-- Here you do arithmetic on sc:
sc_gained [date] = (sc_gained [date] or 0) + sc

xp = %2  -- the xp gained
pot = %3  -- the pot gained

-- Here you establish a value for sc:
sc = %4  -- the sc gained


In other words, you need to establish the value of sc before adding it to something.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Hidelensman   (10 posts)  Bio
Date Reply #5 on Sun 28 Aug 2022 07:34 AM (UTC)

Amended on Sun 28 Aug 2022 07:48 AM (UTC) by Hidelensman

Message
Nick Gammon said:



-- Here you do arithmetic on sc:
sc_gained [date] = (sc_gained [date] or 0) + sc

xp = %2  -- the xp gained
pot = %3  -- the pot gained

-- Here you establish a value for sc:
sc = %4  -- the sc gained


In other words, you need to establish the value of sc before adding it to something.


Thanks Nick for the guidance. I set sc = %4 before sc_gained [date] then it can work :)

I further revised the code to track 3 stats in the mud I am playing - share as below:

Quote:
trigger: ^吒]為你殺死了(.*),得到了\s+(\d+)點經驗值和\s+(\d+)點潛能以及\s+(\d+)點聲望。$


Once the trigger is matched, send
date = os.date ("%d-%m-%Y")

xp = %2  -- the xp gained
pot = %3  -- the pot gained
sc = %4  -- the sc gained

if not stats then
  stats = {}
end

if not stats[date] then
  stats[date] = {}  -- make table if it doesn't exist
end

stats[date].xp_gained = (stats[date].xp_gained or 0) + xp
stats[date].pot_gained = (stats[date].pot_gained or 0) + pot
stats[date].sc_gained = (stats[date].sc_gained or 0) + sc


Add in script file the function Nick mentioned in the following post to establish a recursive table printer:
https://www.gammon.com.au/forum/?id=4903

function tprint (t, indent, done)
  done = done or {}
  indent = indent or 0
  for key, value in pairs (t) do
    Tell (string.rep (" ", indent)) -- indent it
    if type (value) == "table" and not done [value] then
      done [value] = true
      Note (tostring (key), ":");
      tprint (value, indent + 2, done)
    else
      Tell (tostring (key), "=")
      print (value)
    end
  end
end


Then set 2 variables for saving / loading accumulated stats:

save_stats
require "serialize"
SetVariable ("stats", serialize.save ("stats"))
Note ("Accumulated stats have been saved.")


load_stats
loadstring (GetVariable ("stats")) ()
tprint (stats)
Note ("Accumulated stats have been loaded.")


The codes are not ideal but just want to do an experience sharing for those who are troubled by similar topic.

Also welcome Nick and other experts' comments for improvement. Thanks.
Top

Posted by Nick Gammon   Australia  (23,098 posts)  Bio   Forum Administrator
Date Reply #6 on Sun 28 Aug 2022 11:09 PM (UTC)
Message
You can just:


require "tprint"

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Hidelensman   (10 posts)  Bio
Date Reply #7 on Fri 02 Sep 2022 01:40 PM (UTC)
Message
Nick Gammon said:

You can just:


require "tprint"



Thanks Nick - works perfectly
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.


11,810 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.