Is there a better way than this to do unlocks in a video game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ox0zOwra

I have a score variable and a function in _process() in my AutoLoad that checks every frame if the score has reached a certain value, and if so, it checks off the appropriate milestone flag. This is the function:

func milestone_check():
    if milestones["score_10000"] == false and score >= 10000:
        milestones["score_10000"] = true

    if milestones["score_20000"] == false and score >= 20000:
        milestones["score_20000"] = true

etc...

The score in this game increases only when you click on enemies. Considering that I’m planning to add way more score milestones in the future, I’m not sure if this is the best approach for this sort of problem, but I don’t really know what I should change (and if anything at all, since it’s just a bunch of if-checks still). I want to make sure that this function is readable enough and doesn’t slow the game down once I add more features.

:bust_in_silhouette: Reply From: SnapCracklins

Seems ok. You could also just run a match statement on the one value, like:

func milestone_check():
     match score:
           10000:
                milestones["score_10000"] = true
           20000:
                milestones["score_20000"] = true

Not sure if that saves you much besides text though. If you’re worried about it, you can put it in a ready function or at the beginning of a “start game” or “end game” process.

Oh, I completely forgot about the match statement. I should try that too.

Ox0zOwra | 2022-09-11 19:06

:bust_in_silhouette: Reply From: Inces

This indeed is not optimal way of solving the problem.
All You need is to learn about setget, and You will easily find elastic sollution

Oh, you’re right! Thanks, I think I see what you mean now.
What I did is I put this entire check into the setter function, so now it only checks whenever score changes.

Ox0zOwra | 2022-09-11 19:11

Also, I don’t know what these milestones are for, but You can propably compact their code into one line, by getting division rest and comparing text to integer values with str() and int()

for example :

if score%10000 == 0 :
    milestones["score_" + str((score%10000)*10000)] = true

code above will actually work for You if score is always gained equally, so the milestones are reached in exact number. Otherwise, You need to redesign my example :slight_smile:

Inces | 2022-09-11 20:29