LoTROInterface

LoTROInterface (https://www.lotrointerface.com/forums/index.php)
-   Lua Programming Help (L) (https://www.lotrointerface.com/forums/forumdisplay.php?f=50)
-   -   Reading Chat in LUA (https://www.lotrointerface.com/forums/showthread.php?t=3591)

Elliss11 11-15-2015 09:38 PM

Reading Chat in LUA
 
Hi.. i´m not a developer but I want to convert an existing plugin for own use.
can someone tell me how I have to write it that the counter will only count if a certain text found ?

for example :
if "test" match in Chat(PlayerCounter) then Count+1


ChatMonitor = Turbine.Chat;
ChatMonitor.Received = function(f, args)
local msg = args.Message;
if(args.ChatType == Turbine.ChatType.PlayerCombat) then
count=Count+1
do_update()
end
end


thanks for helping
Elliss11

Garan 11-16-2015 05:45 AM

Before addressing how to match a specific chat message, I would like to point out that there are two errors in the code snippet above. The first is relatively minor but would still cause the code to fail - Lua is case sensitive, the line "count = Count + 1" will not work as intended since the second "Count" starts with a capital letter. I also assume you initialize the value somewhere otherwise you will get an error about adding 1 to nil.

The second error is the more significant error and will not only affect your plugin but any other plugin in the same environment. The line that starts "ChatMonitor.Recieved = " assigns a specific value to a shared object. All Turbine objects are shared and their event handlers should never be assigned a specific function or you will clobber any other already loaded plugin's event handlers. Additionally, the Lua environment does not always exit nicely if event handlers are not removed prior to plugins unloading. To avoid this, we use a pair of functions, "AddCallback" and "RemoveCallback" to allow multiple plugins to share event handlers and to safely clean up prior to unloading. This is all discussed in greater detail in the post:
https://www.lotro.com/forums/showthr...03#post5784203

As to matching a specific message, that is fairly simple. The code snippet you posted already tests for a specific channel. The only thing left to do is add a comparison for the message:
if args.Message=="some message" then
-- do something
end

It is generally more desirable to match a "pattern" or sub string of the message. That gets a tiny bit more complicated as you use the string.match() function and Lua patterns (similar to but not identical to Regular Expressions):
matches={string.match(args.Message,"some pattern")}
Lua patterns are described in greater detail in section 5.4.1 of the online Lua 5.1 manual:
http://www.lua.org/manual/5.1/manual.html#5.4.1

Elliss11 11-16-2015 07:54 AM

OK . I must admit that I'm going to get it not alone.
there are other things that I have not observed.
I would like to not have the complete message as a hit ,only a part.


Also
Is in CombatPlayer Channel the word "test" found , then Count+1

Example:
This is a test

count+1

can someone help me there?

thanks
have a nice day

Garan 11-16-2015 09:20 AM

Quote:

Originally Posted by Elliss11 (Post 11725)
OK . I must admit that I'm going to get it not alone.
there are other things that I have not observed.
I would like to not have the complete message as a hit ,only a part.


Also
Is in CombatPlayer Channel the word "test" found , then Count+1

Example:
This is a test

count+1

can someone help me there?

thanks
have a nice day

A simplified version of the string.find function will work for this specific case since you are looking for a static substring, in this case the word "test":

if string.find (args.Message,"test",1,true)~=nil then
end

so, your code snippet becomes:
Code:

count=0; -- initialize variable
 -- define the AddCallback function
 function AddCallback(object, event, callback)
  if (object[event] == nil) then
    object[event] = callback;
  else
    if (type(object[event]) == "table") then
      table.insert(object[event], callback);
    else
      object[event] = {object[event], callback};
    end
  end
  return callback;
 end

 ChatMonitor = Turbine.Chat;
 ChatReceived = function(f, args)
 local msg = args.Message;
 if(args.ChatType == Turbine.ChatType.PlayerCombat) then
  if string.find (args.Message,"test",1,true)~=nil then
    count=count+1
    do_update()
  end
 end
 end
 AddCallback(Turbine.Chat, "Received", ChatReceived)

Note, you should still read about AddCallback and RemoveCallback and use RemoveCallback in your plugin's unload handler to avoid crashing the client (only in very rare circumstances now that Turbine has fixed a number of their bugs) when unloading the plugin.

Elliss11 11-16-2015 10:06 AM

it works .... thank you for your time

Elliss11 11-16-2015 11:13 AM

it works fantastic...But one thing I'd like to improve it.

<<<<<<<<<<<<if Out of Combat then
BodyStatus:SetText("6")
else



count=0; -- initialize variable
-- define the AddCallback function
function AddCallback(object, event, callback)
if (object[event] == nil) then
object[event] = callback;
else
if (type(object[event]) == "table") then
table.insert(object[event], callback);
else
object[event] = {object[event], callback};
end
end
return callback;
end

ChatMonitor = Turbine.Chat;
ChatReceived = function(f, args)
local msg = args.Message;

<<<<<<<<<<<<
if(args.ChatType == Turbine.ChatType.PlayerCombat) then
if string.find (args.Message,"Verbesserter Gnadenschuss",1,true)~=nil then
BodyStatus:SetText("6")
else
if killcount==0 then
do_update()
else
if string.find (args.Message,"Verbesserter Schneller Bogen",1,true)~=nil then
killcount=killcount-0.333
do_update()

end
end
end
end
end

AddCallback(Turbine.Chat, "Received", ChatReceived)


can you help me there too?

have a nice day
elliss11

Garan 11-16-2015 11:59 AM

The test for combat state is localPlayer:IsInCombat() where localPlayer is an instance of the Turbine.Gameplay.LocalPlayer object.
I'm not sure of your intent, but it looks like you should put the condition you are testing in the chat handler (definitely not outside the function definition). That does leave some situations where you could leave combat without generating any messages in that channel which might not always be the result you are after.
I believe you are after something like:

Code:

localPlayer=Turbine.Gameplay.LocalPlayer:GetInstance();
 count=0; -- initialize variable
 -- define the AddCallback function
 function AddCallback(object, event, callback)
  if (object[event] == nil) then
    object[event] = callback;
  else
    if (type(object[event]) == "table") then
      table.insert(object[event], callback);
    else
      object[event] = {object[event], callback};
    end
  end
  return callback;
 end

 ChatMonitor = Turbine.Chat;
 ChatReceived = function(f, args)
  local msg = args.Message;

  if localPlayer:IsInCombat() then
    if(args.ChatType == Turbine.ChatType.PlayerCombat) then
      if string.find (args.Message,"Verbesserter Gnadenschuss",1,true)~=nil then
        BodyStatus:SetText("6")
      else
        if killcount==0 then
          do_update()
        else
          if string.find (args.Message,"Verbesserter Schneller Bogen",1,true)~=nil then
            killcount=killcount-0.333
            do_update()
     
          end
        end
      end
    end
  else
    BodyStatus:SetText("6")
  end
 end

 AddCallback(Turbine.Chat, "Received", ChatReceived)

If as I assumed earlier, you are trying to default the text to "6" whenever you exit combat then the correct approach is to use the InCombatChanged handler to set the default text:
Code:

localPlayer=Turbine.Gameplay.LocalPlayer:GetInstance();
 count=0; -- initialize variable
 -- define the AddCallback function
 function AddCallback(object, event, callback)
  if (object[event] == nil) then
    object[event] = callback;
  else
    if (type(object[event]) == "table") then
      table.insert(object[event], callback);
    else
      object[event] = {object[event], callback};
    end
  end
  return callback;
 end

 CombatChanged = function(sender, args)
  if not localPlayer:IsInCombat() then
    BodyStatus:SetText("6")
  end
 end
 AddCallback(localPlayer, "InCombatChanged", CombatChanged)

 ChatMonitor = Turbine.Chat;
 ChatReceived = function(f, args)
  local msg = args.Message;

  if(args.ChatType == Turbine.ChatType.PlayerCombat) then
    if string.find (args.Message,"Verbesserter Gnadenschuss",1,true)~=nil then
      BodyStatus:SetText("6")
    else
      if killcount==0 then
        do_update()
      else
        if string.find (args.Message,"Verbesserter Schneller Bogen",1,true)~=nil then
          killcount=killcount-0.333
          do_update()
        end
      end
    end
  end
 end

 AddCallback(Turbine.Chat, "Received", ChatReceived)


Elliss11 11-16-2015 12:34 PM

The second text is working with a small change . but you had me already pointed


THANKS !!!!




localPlayer=Turbine.Gameplay.LocalPlayer:GetInstan ce();
count=0; -- initialize variable
-- define the AddCallback function
function AddCallback(object, event, callback)
if (object[event] == nil) then
object[event] = callback;
else
if (type(object[event]) == "table") then
table.insert(object[event], callback);
else
object[event] = {object[event], callback};
end
end
return callback;
end

CombatChanged = function(sender, args)
if not localPlayer:IsInCombat() then
--BodyStatus:SetText("6")
killcount=6;
do_update()
end
end
AddCallback(localPlayer, "InCombatChanged", CombatChanged)

ChatMonitor = Turbine.Chat;
ChatReceived = function(f, args)
local msg = args.Message;

if(args.ChatType == Turbine.ChatType.PlayerCombat) then
if string.find (args.Message,"Verbesserter Gnadenschuss",1,true)~=nil then
killcount=6;
do_update()
else
if killcount<=0 then
do_update()
else
if string.find (args.Message,"Verbesserter Schneller Bogen",1,true)~=nil then
killcount=killcount-0.333
do_update()
end
end
end
end
end

AddCallback(Turbine.Chat, "Received", ChatReceived)

Elliss11 11-16-2015 02:32 PM

CombatChanged = function(sender, args)
if not localPlayer:IsInCombat() then
--BodyStatus:SetText("6")

<<<<<<<


killcount=6;
do_update()
end
end
AddCallback(localPlayer, "InCombatChanged", CombatChanged)



Is it still possible here with a sleep function to install ??

because the buff after end of the fight still goes 9sec

Elliss11 11-21-2015 03:12 PM

#push


All times are GMT -5. The time now is 04:38 PM.

vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI