Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Issue with lua chat and my custom removemessage function?

Asked by 5 years ago

I added a removemessage function into the lua chat that lets me remove a message from the chat given the message's text. This works fine for the most part but I realised that after a while the chat just.. died. All messages had disappeared and any messages players sent wouldn't appear either.

After a few hours of trying to figure out why they were disappearing I found out that I was only removing the messages from the MessageDisplayLog and not the MessageLog, meaning that once the MessageLog reached the maximum amount, it would start removing the last message from the chat.

So, inside ChatScript.ChatMain.ChatChannel I modified my RemoveMessage function to have a loop to remove the message from the messagelog while also calling the function inside MessageLogDisplay to actually remove the message from the chat.

function methods:RemoveMessage(messageData)
    self.MessageLogDisplay:RemoveMessage(messageData)
    local int=0
    print(#self.MessageLog)
    for _, v in pairs(self.MessageLog) do
        int=int+1
        if(v.Message:gsub("%s+","")==messageData:gsub("%s+","")) then
            table.remove(self.MessageLog,int)
        end
    end
    print(#self.MessageLog)
end

I also added some sanity check print statements before and after the loop. First print statement before the loop prints 1 (this is just testing with one message in the chat at the time) and the print statement after the loop prints 0 meaning it has successfully removed the message from the log. (also, the way I use this function is so that every time I send a system message, I can remove the old one so the chat doesn't get cluttered with them)

So I tested the game, and still the messages disappeared after a while. What? So I then added a print statement to this function to also check the length of MessageLog

function methods:AddMessageToChannel(messageData)
    table.insert(self.MessageLog, messageData)
    if self.Active then
        self.MessageLogDisplay:AddMessage(messageData)
    end
    if #self.MessageLog > ChatSettings.MessageHistoryLengthPerChannel then
        self:RemoveLastMessageFromChannel()
    end
    print("!!",#self.MessageLog)
end

and in here, messagelog is continuously rising. Why? I'm not adding any messages except for 1 system message which also being removed as proven by my RemoveMessage function.

If anyone can shed some light on why this is occuring, that would be great.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Your RemoveMessage function is incorrect (though you wouldn't notice if you only had 1 message in your test). You manually keep track of the index to remove from the MessageLog table with the int variable, but int has no relation to the actual index of the message you wish to remove (since pairs can iterate in any order). Though you would be decreasing the size of the MessageLog, you'd be deleting the wrong message(s)! Instead, consider a loop like this:

for i = #self.MessageLog, 1, -1 do
    local v = self.MessageLog[i]
        if(v.Message:gsub("%s+","")==messageData:gsub("%s+","")) then
            table.remove(self.MessageLog,i)
        end
end

As a bonus, by iterating over it in reverse, table.remove will be more efficient if there are multiple messages with the same text.

"and in here, messagelog is continuously rising. Why?" MessageLog contains all the chat messages for a channel; it should be increasing (unless you're saying that it is increasing even when no one is chatting). If the above change doesn't fix things, let me know.

Ad

Answer this question