• Assigning a table a range of values
    9 replies, posted
Hey guys! I am trying to make a NPC leveling system. In this system, any npc in a given table are assigned a random level between a range of values assigned to them. However, I am not sure how to assign these NPCs in a table a range of values. Below is my attempt: NPC_That_Lvls = { ['npc_zombie'] = 5, 10 ['npc_headcrab'] = 1, 5 } Another question that I have that follows this: how do I use these assigned values in a function? For example if I made a function that sets the level of a NPC would I use the two values like so: for k, v in pairs(ents.GetAll()) do if NPC_That_Lvls[v:GetClass()] then v:SetLevel(math.random(VALUE1, VALUE2)) --I know values are not right, but what should go here? end What should value1 and value2 be to get 5 and 10?
Just make your range of values another table, and access it like so NPC_That_Lvls = { ["npc_zombie"] = {5, 10} ["npc_headcrab"] = {1, 5} } for k, v in pairs(ents.GetAll()) do if NPC_That_Lvls[v:GetClass()] then local range = NPC_That_Lvls[v:GetClass()] v:SetLevel(math.random(range[1], range[2])) end end
Thank you!
Don't use ents.GetAll use ents.FindByClass
FindByClass is the same thing.
ents.GetAll is more efficient, he's doing it in one loop and comparing against the class - and then doing the operation with the data from the table. If he used FindByClass, he'd need to do a loop per table entry, which is, honestly retarded - I'd say don't give advice to anyone if your advice is wrong.
It's okay to be wrong, everyone is always learning. Mistakes can be made by anyone; we shouldn't discourage them from trying to help just because they were wrong.
] lua_run local s = SysTime() for i=1, 1000 do ents.FindByClass("npc_monk") end print(SysTime() - s) > local s = SysTime() for i=1, 1000 do ents.FindByClass("npc_monk") end print(SysTime() - s)... 0.0054993589038759 ] lua_run local s = SysTime() for i=1, 100 do ents.GetAll("npc_monk") end print(SysTime() - s) > local s = SysTime() for i=1, 100 do ents.GetAll("npc_monk") end print(SysTime() - s)... 0.0056361358299739 In my tests, ents.GetAll is a lot slower. I've ran ents.FindByClass a thousand times and ents.GetAll just a hundred times and it's already eating more performance. Returning a table of a few entities is faster than returning yourself a table of up to 8192 entities at once. At this times 20 npc_monks were placed in the map for test purposes. It's more than 10 times faster to use ents.FindByClass. If you have a table with more than 10 entries, ents.GetAll should be used, I agree. But apart from that, ents.FindByClass seems to be way faster.
The internal Source implementation for the function still iterates over every entity in the game, and the wiki even states that FindByClass works by using GetAll. I can't imagine where the timing difference is coming from, as the only thing different about the two implementations should be space complexity. Bar that, the time difference is such a negligible amount especially for his use case. Assuming we used FindByClass, however, the approach would be either: A. Loop over every class in his table, and perform FindByClass inside that loop. We're now basically at O(n * A) time complexity where A is the amount of NPCs of that class, assuming it only iterates over the desired class or B. Perform a single loop, using FindByClass' wildcard functionality. Let's say the standard "npc_*" format is used. Now inside our FindByClass loop we use a direct lookup using his classname dictionary and the class of the entity we're iterating over. Okay, we've cut down performance by removing a loop, but he's now lost a bit of functionality as the NPC has to match the format npc_*. Not too bad, but some NPC authors from what I remember don't exactly follow that standard. Complexity wise and functionality wise I think the current solution of using GetAll is best. If you want to test out the time taken for these solutions, please let me know if I'm wrong.
I think it's faster because it returns less entities + it doesn't need to check with lua every entity the server has. Doing operations in Lua is slower than letting the engine handle it.
Sorry, you need to Log In to post a reply to this thread.