+1 vote

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.

Godot version 3.5 mono
in Engine by (47 points)

2 Answers

+1 vote
Best answer

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

by (7,742 points)
selected by

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.

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 :)

+1 vote

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.

by (534 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.