A note on efficiency:
Here’s a famous quote by the inventor of Quicksort: “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”
You can find a fuller quote in the docs: General optimization tips — Godot Engine (stable) documentation in English
But in short, focus on correctness (your code does the right thing), rather than speed (unless it causes noticeable frame drops).
(END OF RANT)
Now about your question:
your approach is basically what the find method does. It’s quite possible that the find method is faster because it is implemented in C++ rather than gdscript, but I didn’t measure it and unless your array contains at least a few hundred elements, I doubt you’ll see much difference.
However, I don’t really understand why you need to find the value if you already have it. What I mean is if the statement
if value == specific_value: equates to true, why not just return specific_value instead of going through the array? You’ve already proven the two values are equal.
Maybe your example is incomplete. Maybe your code is more like this:
func get_item(item_name: String):
for item in self.item_list:
if item.name == item\_name:
Although calling the find method like this:
self.item_list.find(item_name) wouldn’t work, since the item list contains items and not strings. I am a bit confused by what you want to do. Could you share your whole script where you handle the array?
I have an array with my currently active quests. The specific value is just the quest I am searching for and I don`t always know if that certain quest is active and thus inside the array.
The array can contain quiet a lot of active quests, which is, why I was worrying about performance. But I guess I should not mind this too much.
And thank you for explaining what the find() method does
Ciavarie | 2020-10-11 10:56
Oh, I see.
If you are worried about performance, instead of an array try a dictionary. Each dictionaty entry has a key and a value. In your case, the key could be the quest name or quest id, and the value is the actual quest. Then instead of find you would simply check
quest_dictionary.has(quest_name). Dictionary lookup is O(1), meaning it’s instantaneous basically, as it does not have to iterate over each entry.
Bernard Cloutier | 2020-10-12 04:45