Help - (semi) Endless Loops

API Discussion
I know there may be a simple solution, but here goes.

I created a WoW addon years ago (like...2009) that replies an emote with an emote. The problem was, when you had two accounts with this addon, and their characters had each other targeted, then you do an emote, you can sometimes cause an endless loop of emotes, kicking both accounts to the login screen.

Well, I decided to resurrect this addon, which uses the old API. I've updated it and it works. It's fairly simple. The problem I'm having is keeping it from an attempted endless loop. I have a timer thingy preventing the looping to last longer than a second. I'm sure it's some logic thing I'm not thinking of. Anyway...

My LUA assigns the global variables as such
--EMOTE TOKENS
TOKEN_AGREE="AGREE"
TOKEN_AMAZE="AMAZE"
TOKEN_ATTACKMYTARGET="ATTACKMYTARGET"
TOKEN_BRB="BRB"
TOKEN_BARK="BARK"
... etc

--search strings
AGREE="agree"
AMAZE="amaze"
ATTACKTARGET="attack"
BRB="back"
BARK="bark"
BASHFUL="bashful"
... etc

Then I populate a table
Emotes = {};
table.insert( Emotes,{ Trigger = YES, Reply = TOKEN_AGREE});
table.insert( Emotes,{ Trigger = YAY, Reply = TOKEN_AMAZE});
table.insert( Emotes,{ Trigger = YAWN, Reply = TOKEN_ANGRY});
table.insert( Emotes,{ Trigger = WRATH, Reply = TOKEN_APOLOGIZE });
... etc

So if someone has my character targeted and performs a "yawn" emote, my character will perform an "angry" emote. Can you see how this can get into loop issues if both characters have the addon and have each other targeted?

---OLD CODE--- (had some endless loops, especially if you configured the emotes accordingly)
function EmoteReply_OnEvent()
if (event == "CHAT_MSG_TEXT_EMOTE") then
foreach(Emotes,
function(k,v)
if (string.find(arg1, v.Trigger)) then
if (string.find(arg1, "you")) then
DoEmote(v.Reply,true);
end
return;
end
end
);
end
end

---NEW CODE--- (loops but not endlessly because of this timer code I tossed in there)
function EmoteReply_OnEvent(self, event, ...)
local message, sender, language, channelString, target, flags, unknown, channelNumber, channelName, zero, counter = ...;

--this code is to prevent endless loops longer than 1 second
--
currentTime = time();
if (lastTime==0) then
lastTime=currentTime;
elseif((currentTime-lastTime)<1) then
lastTime=0;
return;
end

--if it's me sending the emote, ignore'
--
if (UnitName("player")==sender) then
return;
end

--loop through the emote triggers until you find a match, then respond with assigned emote reply
--
foreach(Emotes,
function(k,v)
if (string.find(message, v.Trigger)) then
if (string.find(message, "you")) then
DoEmote(v.Reply,true);
end
lastTime=currentTime;
return;
end
end
);
end

Now, I've thought about keeping track of who sends the emote and not allowing consecutive emotes from the same person ... and also not allowing consecutive emotes less than one second apart...

The current code will get into loops, but not endless...but you can bounce a lot of emotes back and forth in one second.

I'd just like to bounce this off the community here for any suggestions. Be kind please... it's been a long time since I've done any coding.
Hi Martigan, you've reach the wrong forum. This forum is for the battle.net APIs. I think you want the WOW UI & Macro forum, located at the link below:

https://us.forums.blizzard.com/en/wow/c/guides/ui-macro

Join the Conversation

Return to Forum