2

Brand new to lua as of a few hours ago, I have some moderate background in C++ and Java but nothing amazing.

I'm trying to work on an addon for a game that checks for players around me, and if so(within 10 yards) greets them.

It works great, except I ONLY want it to run once per player, as it would be spammy and annoying to constantly greet people.

I figured the best way to do this was to store their character name in an array, but I'm struggling to understand the syntax of arrays.

     function Wave()
        local totalObjects = GetObjectCount()
        local shouldMessage = false
        local player = GetActivePlayer()
        arrayNames = {}
        for i = 1, totalObjects do
            local object = GetObjectWithIndex(i)
            if object ~= player and UnitIsPlayer(object) == true and UnitIsDead(object) == false then
                local yards = GetDistanceBetweenObjects(player, object)
                 local name = ObjectName(object)

                  ----------------- The beginning of my issue ----------------

                if yards < 10 and arrayNames[i] ~= name then -- if name isnt in array already?
                    arrayNames[i] = name -- trying to add the name to array
                    print(arrayNames[i])

                    break
                end
            end         


    end
    if storeName then   
    end
end

1 Answer 1

1

The issue is that your table is getting cleared after each call to Wave. This is because you are doing arrayNames = {} inside your function, so each time it is run the table is set to a new empty table. you can define arrayNames outside of your Wave function or change it to arrayNames = arrayNames or {} The second option will set arrayNames equal to arrayNames when it is defined or to a new table if it is not defined.


Additionally

Your code only checks if the name exists in the array at the specific index, rather then checking the whole array. If the player's index can change then you will likely greet them again using this method.

You will need to go over the whole array too be sure you have not greeted this person already. This means as you greet more and more people the check will get longer and longer

Rather than use an array, I suggest using a set:

if yards < 10 and not arrayNames[name] then -- if name isnt in set already?
    arrayNames[name] = true -- trying to add the name to set
    print(name)

    break
end

simply add to the table using the name as the key and setting the value to true this will provide O(1) performance for your check.

Here is more information on sets: https://en.wikipedia.org/wiki/Set_(abstract_data_type)

In computer science, a set is an abstract data type that can store unique values, without any particular order. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply! This makes sense but is still running repeatedly, I should mention that this function gets re-called every frame update in game, does this mean it is resetting the local variables each time? (and thus not working properly)
Ah then the issue is that your table is getting cleared after each frame. it needs to be defined outside of the loop that is being run every frame... to be more clear because you do arrayNames = {} inside your function you reset the table each time. you can change it to arrayNames = arrayNames or {}
Ah, that was the problem!! I didn't even notice that but it makes perfect sense now. Thank you very much! It's working great.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.