WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   [Classic]LF help with LUA (https://www.wowinterface.com/forums/showthread.php?t=57607)

sh4dowburn 10-16-19 05:04 AM

[Classic]LF help with LUA
 
Hi everyone!

I'm trying to write my first AddOn about showing my total money and latency in game,
But I notice that the total amount and latency only updated after input "/reload" in game now,

here is my code,
Code:

local m = CreateFrame("Frame", nil, UIParent);
      m:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -88, 6);
      m:SetWidth(80);
      m:SetHeight(10);
      m:RegisterEvent("PLAYER_MONEY");
      m:RegisterEvent("PLAYER_ENTERING_WORLD");

local Getmoney = GetMoney();
      money = m:CreateFontString(Getmoney, nil, nil);
      money:SetAllPoints(m);
      money:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE");
      money:SetJustifyH("LEFT");
      money:SetTextColor( 1, 1, 1 );
      money:SetFormattedText(("%d|cffFFD700g|r ".." %d|cffB5B5B5s|r ".." %d|cffCD853Fc|r"):format(Getmoney /10000, (Getmoney / 100) % 100, Getmoney % 100));

Code:

local bandwidthIn, bandwidthOut, latencyHome, latencyWorld = GetNetStats();

  local l = CreateFrame("Frame", nil, UIParent)
        l:SetPoint("BOTTOMRIGHT", UIParent, "BOTTOMRIGHT", -21, 6)
        l:SetWidth(80)
        l:SetHeight(10)
        l:RegisterEvent("PLAYER_ENTERING_WORLD")

        latency = l:CreateFontString(latency, nil, nil)
        latency:SetAllPoints(l)
        latency:SetFont(STANDARD_TEXT_FONT, 12, "OUTLINE")

        --latency:SetShadowOffset(1, -1)
        --latency:SetShadowColor(0, 0, 0, 1)

        latency:SetJustifyH("RIGHT")
        latency:SetTextColor( 1, 1, 1 )
        latency:SetFormattedText(latencyHome.."/"..latencyWorld.."|cff87CEEB ms")

I'm new to Lua, learn all above codes from wowwiki and blizz source codes.

Urtgard 10-16-19 06:10 AM

You need an event handler and you must tell the frame to use this handler.

Lua Code:
  1. local frame = CreateFrame("FRAME", "FooAddonFrame");
  2. frame:RegisterEvent("PLAYER_ENTERING_WORLD");
  3. local function eventHandler(self, event, ...)
  4.  print("Hello World! Hello " .. event);
  5. end
  6. frame:SetScript("OnEvent", eventHandler);
https://wow.gamepedia.com/Handling_events

To update the latency you maybe need an OnUpdate script.

Kanegasi 10-16-19 06:20 AM

Latency data is updated somewhere around every 30 seconds to a minute. An OnUpdate for that is completely unnecessary. I suggest registering a regular event to update latency, such as UNIT_AURA or COMBAT_LOG_EVENT_UNFILTERED. Make sure this event only calls for latency, doing more with these events is a potential performance hit in combat.

SDPhantom 10-20-19 12:41 AM

Quote:

Originally Posted by Kanegasi (Post 334287)
Latency data is updated somewhere around every 30 seconds to a minute. An OnUpdate for that is completely unnecessary. I suggest registering a regular event to update latency, such as UNIT_AURA or COMBAT_LOG_EVENT_UNFILTERED. Make sure this event only calls for latency, doing more with these events is a potential performance hit in combat.

Using events to update something that isn't related to is generally a bad idea. There are a ton of issues that happen depending on which data you're trying to access or even trying to update your output in a timely manner. There is no single or group of events that is guaranteed to fire every X seconds in all situations.

A more direct alternative to using OnUpdate is the C_Timer lib. C_Timer.NewTicker() wraps a function you give it and repeatedly calls C_Timer.After(). See SharedXML\C_TimerAugment.lua for Blizzard's implementation.


All times are GMT -6. The time now is 09:29 PM.

vBulletin © 2020, Jelsoft Enterprises Ltd
© 2004 - 2019 MMOUI