ā How to Save HighScore in each difficulty separately?
I don't know how to save players HighScore for each difficulty separately, so that it will not be the same HighScore for every difficulty.
885 Replies
well, first you would have separate difficulties
then you would save a player's HighScore separately for each one
I will send you right now code
I did that, I have separate difficulties
I don't know how to save the highscore whenever I click to each difficulty and play
I did something like
use playerPrefs like you used before?
Yes but I have buttons
I click on each difficulty
I don't want to make more high scores
One high score for each
Wait I think I have an idea
I have a difficulty script and in this script I have a button.addlistener
So in this script st the start() method I have to make all that to happen
Maybe I can use tags
For each button and then compare them
And do my work
u said u want to save highscore for each difficulty right?
If (it is easy and isclikced) then
do playerprefs
Else if ()
do again the same stuff
Yes that what I want
That's
this was just a representation on how to save for each difficulty
you take each line for each button
Yes I don't want three high scores to have
if (easy) then PlayerPrefs.SetInt("HighScore_Easy", score); // for easy
Oh wait
The high score will be the first one I made right?
So I have to make three variables
For high score for each difficulty
3 variables of what?
Highscore_easy and all of that
Or I am confused
you just need 1 variable of score to display on screen
Yes and the other for high score
Why you used three strings in playerptefs
Prefa
u also need 1 variable of high score to display on screen
Prefs
depending on what difficulty u chose, u load the high score from playerPrefs then assign it to the high score variable that u need to display on screen
Yes but I didn't understand what those three strings do inside of player prefs?
No need to load the rest of the high scores that you wont display
theyre just data stored in your pc
and playerprefs load or save them to ur pc
So, they don't actually do something right?
if they dont then Unity wouldnt have created this class
I still can't understand bro how we use player prefs. Like what's the difference between playerprefs.getint and set int
GetInt gets the specified data from ur pc as Integer
Playerptefs("High score", score) it store the score to high score string
SetInt saves the specified data into ur pc
Ok
Which here the specified data is the score?
u specify it
Yes so this is the specified?
which one?
The score
In played prefs
Player prefs
current score or high score?
Current
PlayerPrefs.SetInt("HighScore_Easy", current_score);
I save the score that I checked before and pass it to the high score something like that isn't it
?
saves ur current score into ur pc in
HighScore_Easy
say ur current_score was 12345
and u called this method
next time u do current_score = PlayerPrefs.SetInt("HighScore_Easy");
whether u restarted the game or not
it will store 12345 into current_score
Ok that's understood but I don't want that I want it to be stored on high score
Not in the current score
choose ur variable then
Oh ok
Coming in 1 hour back I have some work to do thanks!
Hey, I am back
I did what you said
I made this method
and I call it to startgame method
I call it in that
But I am not sure if it is correct
I don't see anything to happen
This is in game manager
I will send the code in difficulty script
ur trying to load from
HighScore
without specifing the one with a difficultyMaybe I have to add more parameters for each button
I don't think so
but check the other code
Thats in difficulty script
does that compile?
What do you mean compile?
like, can u run the game without any errors?
Yes
The game has no errors
what do the values of
difficulty
represent?
0 for easy, 1 for medium, 2 for hard?The actualy problem I have is how to save my highscore on start for each difficulty and on game over i want it to be saved for another try if the player want to try it again
The game has a restart
So, I reload the scene back to menu
?
The values of difficulty represent the spawn rate speed
So what I did was that
spawnRate /= difficulty;
so how would the game know if ur on easy, medium or hard?
Because I add a listener
Check the code above
here
so u know based on the spawn rate speed?
Maybe I have to compare tags in set difficulty method?
yes
thats not a professional way to do it
use an Enum
I am not a professional XD
I am just a beginner
enum? how to use it?
Enumeration types - C# reference
Learn about C# enumeration types that represent a choice or a combination of choices
Can you show me how to make it happen?
in ur case u define it like
Oh, ok.
enum is a type, like classes
Alright
u can define it in its own file
So, that will be in game manager right?
After that what are the next steps?
define it in its own file
I did it.
ok create a field for it in the game manager
oh, ok
the default value will be Easy since its the first one defined in the enum
for which button is that on the first image?
It is for each of them like for easy, medium and hard
you mean that
?
no, that
can u show the full class of that image?
yes
I will take 2 screenshots cuz it is big
u can just use $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/oh yes
Can you give me an example of how to do it
?
oh understood
How to share it I saved it in blazebin
?
copy paste the link
BlazeBin - pgglgggzsruv
A tool for sharing your source code with the world!
huh
i dont see this code on the first image
I just delete it
I thought was unuseful
I can write it again
do u want to write it again?
Ok
Do i have to write it again?
you have 3 buttons to set difficulty right?
I will use it somewhere?
Yes
how will the buttons know which difficulty to set?
Oh yes so i have to compare tags
compare tags where?
In game manager
Wait the first image you mean this ?
Thats in another script
I have it
Thats the diffuclty script
?
I don't know but I think i have to make a method in which I will compare tags that will be the buttons and the I will save with prefs and also to call it in start game. I confused a little bit with that
ok so u attached this script to each button right?
yes
i guess that works,
What I said?
change
public float difficulty;
I have it float
to
public Difficulty difficulty;
oh
ok
and set Easy, Medium,Hard for each button in their inspector window
I cant set it difficulty difficulty
In the other script
what do u mean?
IT says that
show screenshot?
Only difficulty button
where did u define the enum?
In gamemanager
makes sense it cant find it
gamemanager.difficutly
maybe
i told you define it in its own file
public GameManager.difficulty difficulty;
I did that
it is correct no errors
thats just ugly and not recommended
ok
+ C# naming conventions recommend u to name Enum with PascalCase
Difficulty not difficulty
and camelCase for fields
difficulty
so enum difficulty
what?
change
to
the bottom is the recommended way
oh ok
I did the first one
dont do what i didnt write
Now it works
I just have it the same as the parameter
Thats why I change it
I do what you say
š
what parameter?
I just have it the same with the parameter in start game
Now i have a problem
i have no idea what parameter ur talking about
Check that in difficulty script
yeah
This parameter
ud need to change the parameter type of StartGame
its float, change it to the enum
public void StartGame(Difficulty difficulty)
oh ok
Now I ve got some problems i did that you said
Why that happens? I did what you said it got me some errors here
lets start with the first line
Ok
go to the line where the first error is
Here it is
there is a lonely
public
on line 8
have i ever told u that?I just removed it
ok
XD
next error
Here is the next
Because I cant in start game method divide that
we will get to that later
It is not an int or a float
Ok
what happened to the field
difficulty
?
u deleted it?no it is in game manager
i told you this
but you deleted it instead
I did that in game manager
keep the one in
DifficultyButton
class
to
too
so each button has its own Difficulty value, to assign to the one in GameManager laterDo i have to remove it from game manager?
No
oh ok
It says difficulty button
It doesn't have the Difficulty
what?
that
in difficulty script
show new screenshot
well where did u define the enum?
you did the same mistake before, u sure are forgetful
I am not forgetfull i just lose control easy cuz I dont know I just do it first itme
time
I couldn't have done that alone
I will take some lessons soon
To learn more but ok
i believe you defined it in GameManager, right?
put it outside the class atleast
the enum type that is
yes
Here i did that
In game manager
capitalize each first letter of easy, medium and hard
I have also that in game manager
public Difficulty difficulty;
now in difficulty script
I capitalize them
ok
Now which variable do i have to create in difficulty script
what do u mean?
I have that
the difficulty is an error
put
public Difficulty difficulty;
error because difficulty
variable does not exist in this classI cant use the Difficulty in this class
Thats the problem
why not?
oh u defined the enum in GameManager class right?
yes
It does not allow
so for you
Like i did before
I told you that
You said thats ugly
Yeah thats why you need to define the enum
in its own file atleast
I did that
can you show?
in the other class and I told you to make public gamemanager.difficulty bluh bluh and you said that it is ugly to do so i removed it and waited
it is
I will add it again wait
and i believe i told u to define it in its own file
or outside the GameManager class
public GameManager.Difficulty difficulty;
I did that
so... ur not gonna define it in its own file?
Oh how to define
it in its own file??
create a new .cs file just like u did with the rest of the scripts?
in the Assets/Scripts folder
if you have GameManager.cs
ok
and DifficulyButton.cs
yes
u can also have Difficulty.cs
ok
so to make a script or a folder?
make a script
ok
I call it Difficulty
.cs ?
the file extension is important
Oh ok
so Unity can know its a csharp file
Ok
I call it Difficulty.cs
Now what i do
ok nice, is it in Assets/Scripts folder like the rest of the files?
yes
u put in that file
so i remove it from game maanger
manager
?
yes
ok
where to put it in start() or in update()
?
or before start
oh nothing
Understood
I made my own
put what?
outside of thos
those
nothing
nevermind
I did that
I put it insdie
inside
Now i do have to get reference?
now u can safely define
public Difficulty difficulty;
in DifficultyScript
since the enum isnt in the GameManager anymoreoh in my DifficultyButton script right?
yes
I did that
I cant divide the difficulty
with spawn rate
Still have an error with that
I did what you said but I got only one error
We will continue later I have to go. I will read your messages later on
Bye, thanks!
yeah
you will not divide the difficulty
you will use if else or switch statements
for example
Hey, I am back
I did that but it give me an error
I will show you, let me open visual and unity
So, I did that
I name it difficulties not Difficulty i had a name called that so I change it
if (difficulty == Difficulty.Difficulties.Easy)
This also get me an error
@TheRanger
is ur enum named Difficulty or Difficulties?
my enum named difficulties
talking about this
the type itself
yeah
i cant see the error fully
I call it difficulties, I ve deleted the script and made a new one again difficulty.cs and then i call the enum difficulties
I will show you
Wait
can u show that file?
You want me to make the if statement in a method that already exists or in a new one for example SaveHighScoreForEachDifficulty() and also in which script to do that?
the if statement is suppose to be in StartGame method
Ok but i can make a method and call it in there
in startgame() right?
Wait i will show you the file
do not define enums in a class
as ive said before
what do u need that class for?
The difficulty you mean?
and why do u need it to inherit from MonoBehaviour?
Don't you said to make the enum in difficulty.cs?
yes, ur point?
that file should only have
with nothing around it
I don't need mono behaviour?
What is actually mono behaviour? I have never thought about what it does
its for Unity to detect it as a Unity script
oh wait I read it
Thats clearly understandable
to allow you to attach it to game objects
yeah
and detect methods like Start and Update method to call them
So, I dont want it to be attached in game objects
so thats why I have to remove it?
nope, the Enum is only a value type
yes
Alright, understood!
brb 30 minutes
ok bb
back
where were we?
Yes i did that
check
We have to be fast because I have school and I haven't finished homework that my teacher assigned to me
So check
I did that in the method
I cut everything i had in checksavehighscore method and put it in this one
Hey are you there?
yeah
I delete it and I add it again in checksavehighscore i notice a problem in that
check save highscore method it is for game over check
not in start game i dont want it to be saved in the start
We are there were we left it
So, what are the next steps
?
does this method work as u wanted to?
let me chck
I have that problem in console
I made three variables floats
for spawn rate
for each difficulty
and?
I made them private
variables and I did assigned a value
ok?
read the message
those are warnings not errors
its telling you you never used them
I made it public and it fixed
i know they are not errors
ok
So, now what i do ?
TheRanger#3357
does this method work as u wanted to?
Quoted by
<@!197732653326139392> from #How to Save HighScore in each difficulty separately? (click here)
React with ā to remove this embed.
no it doesn't worked
try to debug and see where the issue is
For one reason my difficulty is the same for each diff
I assigned the values
first, check which if condition does it call for each difficulty u set
start by putting that in the if condition of easy difficulty for example
I need a bol
bool
isclicked
I think
It works
I wrote debug.log and it shows that in the console
the click works but the game has no difficulty for a reason
also does not save in start game
default difficulty is easy if you never set it
only in game over and then reload scene nothing happens
it cant have
no difficulty
I made three varibles
spawnrate for easy for med and hard
what is the goal of this method?
It checks and if it is easy then it saves updates the score in start game it is wrong actually
when do u call this method?
BlazeBin
A tool for sharing your source code with the world!
This is the game manager script check the last method
? i see no code there, thats the home page of the website
XD
wait
BlazeBin - qxuipuejrjqo
A tool for sharing your source code with the world!
I have also that script that i didn't show you
BlazeBin - xrstnejhjqpq
A tool for sharing your source code with the world!
It is where i destroy out of bounds target and game over and everything I call from game maanger
manager
ur calling
SaveHighScoreForEachDifficulty
method when u click start game?Yes
Thats not right
?
oh u modified the method
it looked like this moments ago
I change it and get back where I was before
ok so, when u open the game
what do you see?
I add only that in save high score method
I have not diff
make up ur mind already
š
the score doesn't save on reload
XD
lets take steps
when u open the game, what do u see?
When I open the game i see in score = 0 and highscore = 0 as well and the menu screen
can u show screenshot?
Check
Wanna a video
ok did you give difficulty values for each difficulty button?
video record how the game starts and ends?
not now
TheRanger#3357
ok did you give difficulty values for each difficulty button?
Quoted by
<@!197732653326139392> from #How to Save HighScore in each difficulty separately? (click here)
React with ā to remove this embed.
Yes
In game manager script
I add them
can u screenshot that?
I have them in there as well
here as well
I have these variables public
ok so Difficulty is Medium for Medium button, and Hard for Hard button?
Wait I did something wrong
Spawnrateeasy in hardbutton
that doesnt look like the inspector window of the difficulty button
Wait a minute
click on one of these buttons and show me their inspector window
I found the problem
I didn't change the difficulty in buttons
XD
easy in all buttons i have
show me that you did
I just changed it
ok cool
now each button has their own difficulty value
show the DifficultyButton script
Still doesn't change the diff
yea we will get there
take steps
yes
step 2, DifficultyButton script, show it
ok
do u know why the difficulty didnt change?
oh i didn't call it in setdifficulty
no
in start()
sorry
you do call it
as a proof, StartGame method gets called right?
that calls the SaveHighScoreForEachDifficulty, right?
yes
yes
the
the difficulty here is easy because
you never gave it a value
this is
public Difficulties difficulty;
's variableyes
not the one who came from the parameter StartGame
oh
So the parameter comes from start game i need
?
yes
u have 2 ways
No i have set it difficulties the enum
I cant divide like i said before
assign the difficulty from StartGame's difficulty
why are u so obsessed with dividing
anyway ok i will do it
XD
u either do
or
yeah i didn't add the parameter in save highscore
no
so, u either take the value from the parameter and add it to the field
it its not that
or u pass it as a parameter to the SaveHighScoreForEachDifficulty method
it says that error when i want to pass the parameter to the method
u should know what it means
add the parameter to the method
it says no overload i add it
show me how u added it
ok wait
now the difficulty variable in that method, will be the one in the parameter, not the field
or just use this way since u need to tell the game which difficulty to set
ok now it fixed
i got another error in target script
it needs an argument
where i call it in destroy out of bound so when it does game over
what to add difficulty?
brb
I have to leave very soon willl you be at night 9:00 or 10:00 to try to fix that problem with me?
look
it looks like ur just copy pasting my code without knowing what it does
or how it works
learn C# before starting unity
I cant right now learn all of that
take ur time
Yes i will do that in summer
I have some exams thats why
in 1 month
what does exams have to do with this game?
I am trying to do simultaneously and the game and study for exams but I wanted to do that last thing and actually done with that until i give the exams
why not delay it to summer?
Ok it will be right here the post
usually spoon feeding isnt allowed here
So we will continue maybe 12 june when i will done?
So do you want me to delete it
for now
you could start learning the basics of C# in link below
$helloworld
Written interactive course https://learn.microsoft.com/en-us/users/dotnet/collections/yz26f8y64n7k07
Videos https://dotnet.microsoft.com/learn/videos
Yes
I have some platforms
also
solo learn and w3schools
Anyway do i have to del that thread?
taake ur time, u wont learn if youre trying to learn too fast and skip steps
for now?
no, you dont have to
So, I will have it right here
u cant anyway
oh ok
Thanks for your help! I will contact you after 12 jun maybe if i will continue with that game ok
?
thats fine, tho anyone can help you
Hey, how old are you?
I am going in 9 days 18
why do u ask?
I just ask
Is there a problem to tell me how old you are?
If yes tell me where is the problem?
it is a secret
Come on I am not gonna tell it to anybody
Trust me
thats not the issue
Where are you from? I am from greece.
Hey, I have some time
I return from school
Can you help me complete last thing
I have some time free
I don't want to delete it
and start from the start after 12th june
I left it now in the middle and I want to complete the last thing
what thing?
I want to complete that with difficulties
And then i close this thread
which part exactly?
So, I still got the problem with the values my diff doesn't respond properly
it is the same
in all diffs
how so?
show some code
ok
So that is the savehighscoreforeachdiff() I ve added the parameter in there so I have it done
Here difficultbutton script
so what current issue are u running into at the moment?
here is the start game method i am sending you all of that in because I dont want everytime taking screenshots thats better
So, at the moment i have the problem with difficulties
I click easy it has default diff, i click the medium it has again the default
Every one of the button when gets clicked the value is default
u mean when u click hard, it prints
"The button easy was clicked!"
?yeah
It shows that in cosnole
console
But the value is default
The spawn rate is the same in all of them
Easy, medium and hard
u probably didnt set each button's difficulty value in their inspector window
Check i ve set it
spawn rate easy
which button is that?
it is the game manager inspector
game manager object
yeah for which button is this inspector window showing info of?
in this image
easy
it shows easy
in diff
show medium and hard
and it has its values in spawn rate ez mid and hard
buttons?
yes
this medium?
first easy
second mid
third hard
so when u click hard
it prints ``The button easy was clicked!" ?
wait let me check
I wrote debug.log in the method so i will check it
it says the button hard is click
clicked
when i click hard
ok so its working
Medium button was clicked when i click mid
so yes it works
But it spawns in the same rate
can u show code of gamemanager
yes the whole
?
yes
BlazeBin - iqzprnwnspso
A tool for sharing your source code with the world!
check
yeah
u are assigning values to fields spawnRateEasy, spawnRateMedium, and SpawnRateHard
yes
but u are using the field
spawnRate
to spawn withyes
as seen in line 46
yield return new WaitForSeconds(spawnRate);
spawnRate's value is 1.0yeah true
yeah
because u set its value in
private float spawnRate = 1.0f;
but you never changed its value in the SaveHighScoreForEachDifficulty method
and thats why the spawnrate is still the sameyes but i have different variables for eas, mid and hard
pointless, u dont need them
oh ok
u arent even using them
So, I have to replace that variables with spawnrate that i have
yes
ok
I have an error in target script
show what you changed first
ok
I changed spawn rate variables the three i had to spawn rate only
so i assume it spawns correctly now right?
How do you mean that?
Correctly?
like the spawn rate isnt the same for each difficulty u choose
No it is
because i have an error
then u probably ran an old build
Like i said with parameter
probably
what error?
wait
Check
yeah the method needs an argument to pass
where are u calling that method?
Yeah but which one i have the difficulty parameter do i have to use it?
in game over
and in start i have it there
why are u calling it?
in game over
You are right. It is pointless to have it there
I have also one method that saves the highscore in game over
I removed it from there
So take a look
that was with the method
I am calling that method only in start game right now
let me ask u again, what is the purpose of the SaveHighScoreForEachDifficulty method?
The purpose is to save highscore in each difficulty
save it where?
In the start game
so whenever the game ends it will save the highscore in easy dif
what do u mean by the score end?
so when the player wants to play again in the same dif then he continue from there where he left it
Check it again
whenever the game ends
so you want to save the high score when the game ends right?
Yes i do it
in game over with checksavehighscore
method
check here
I want it to be saved when i restart the game
and click again the diff button to play
I will show you a video record
can u tell me what
save highscore
meanssave highscore means that whenever the game goes over the highscore is being saved
it saves the current highscore player did
so why are u trying to save the highscore at the start of the game?
I got confused, so i have to have the savehighscoreforeachdiff() somewhere else?
Just leave that method alone in game manager
?
what do u think?
Let me take you a video record
You will understand the problem
k
Tell me truly do I have bad english?
Some people told me I have mid
mid i guess
š¦
where''s the video?
Wait
Do you know which is the best platform or app that I can use in order to video record?
Maybe my windows 11 has that predownloaded?
idk
screenshots should be fine tho
ur suppose to load the high score of the current difficulty at the start of the game
are u doing that?
yes
I noticed something
When i ve clicked medium
I was the difficulty value of easy
when i clicked easy it had faster spawn rate
yeah?
we already solved that
so ur current issue is
We solved the spawn rate?
yes
When I click easy it spawns faster than mid
why?
because you told it so?
0.7 means an item will spawn every 0.7 seconds
1.4 means an item will spawn every 1.4 seconds
Yes the opposite happens thats what i mean
XD
in mid where i have 1.4 happens the 0.7
are u sure?
Wait a minute
i cant see the mouse in ur video so idk which button u clicked
First is the easy
oh but i see the button being clicked
after that is the mid
its correct in ur video
medium = 1.4
easy = 0.7
i can see what button u clicked in the video
in ur video, u played easy first, then medium
so why are u saying the opposite happens?
yes
I understood the problem
I just mixed that with the division i ve had before
I got confused with that
ok so ur current problem is
ur not loading the highscore at the start of the game
Yea works
Lets go to the other problem
That was the problem s
It solved
Yeah
Thats the problme
I cant load the highscore at the start
from my previous game
i want it to be saved from my previous game in the new one in the same diff that i have started playing
well try creating a method called
LoadHighScore
oh ok
with a parameter of difficulty
That i have made?
public void LoadHighScore(Difficulties difficulty)
yeah
are u on windows?
yes
open
regedit
in run commandopened
registry editor is opened
Expand HKey_Current_User
yeah
then expand Software
yeah
can u open this window in Unity ?
yes
open it
what is the company name and product name?
ok back to regedit
ok
under HKey_Current_User/Software
u should see
DefaultCompany
is it there?I see it
expand it, then click MyGame
what do u see?
There is not mygame
u sure?
I see new unity projects
and playime laucnhed
launched
playtimelaucnher
launcher
under DefaultCompany?
did u expand
DefaultCompany
?yes i told you what was in there
š¤
it says default
when i click defaultcompany
Do i really need that?
Why we want that?
can u run
%userprofile%\AppData\Local\Packages\
in run ?YEs i can
I see a lot of files
microsoft related
and more
one of the folders uses ur game's package id
So, why we are doing that?
we want to see where the highscore gets stored
ok do i have to find that
In every file?
if ur on windows 10 or 11 its harder
are u on any of these windows?
iam on 11
nvm then
anyway do i have to close that all?
yyeah u can close regedit
Is there another way to see where it is stored?
so back to this
yes
I ve got an error with that
well u can debug
for example
u save and load and debug the value
just for debugging purposes
yes
try atleast executing that code when u open the game
and tell me what it printed
ok
Can you tell me also what to do with that one
we want to make sure if it saves and loads data from ur pc
ignore that
did u open unity twice or something?
no
why?
nvm then
It seems like in the code above nothing happens
where did u put that code?
no it happens
Sorry
it says value is 0 at the game over
i did 60 highscore
i dont care about that atm
did it print
Value is 0
?I mean i did 60 highscore and it says value is 60 i also dont care
value is 0
this code?
Where do you want me to put that code you send in which method?
I ve putted where it checks and saves at the game over
well u can put it at StartGame method
I will put it in both
doesnt reallyt matter, as long as it gets executed
Yeah it says value is 0 at the game over
can u show the code?
I ve putted in start game and in destroy out of bound where it goes game over
and the print message
yes
here
Here is the console what shows
So, we actually want to find where it saves the highscore with debug can i test that you send in other methods?
no no
copy that exactly
ok
dont put it in the if condition
where
?
in the StartGame Method
just not in the if condition
Do i have to remove the previous one
from both methods?
wont affect but sure
the issue here is, u assigned value to
highscore
but you printed the value of highScore
they are 2 different variables
š
thus why it printed 0oh
Check now it shows 9999
I did that you said i putted out of the if cond
ok cool so now we know it saves and loads fine
delete the code and go back to the loadhighscore method
yes so how to fix that with the 0 in the start game
Just i have to change the highscore in player prefs?
I set int highscore
lets take baby steps first
go to the loadhighscore method
I have an error
there i have told you before
method public is not valid for this item
show code?
i have to remove the public modifier
waity
wait
Check
Check the code
Dont give attention to diff
I write it difficulty and still the error exists
oh I am an idiot
I made the method in another method
XD
now its fine no errors
So, lets take baby steps like you said
moving on
you should know what to do by know
So what i do now in load highscore can you tell me a little bit fast because i want to complete it no time for thinking
and searching and all of that
cmon u already have the if condition and the playerprefs methods
it doesnt even require thinking
I have to get the if condition and the player prefs inside and paste them in load highscore?
I dont think so
ull use if condition to check each difficulty and use playerPrefts GetInt to get each key for each difficulty and store the value to
highScore
careful, saying no time and be fast can get u muted hereoh really
Then sorry š
we're here to teach, not to spoon feed
if you dont have time maybe you should just code it later
Thats true my fault š
I know what to do just give me a sec
but before i go there i want to know something
I have already the SaveHighScore for each () and there i have if conditions so we make load highscore, so in load highscore i will save highscore in load so i will call that method in the Savehighscore mthod right?
baby steps
lets finish the loadhighscore method first
hahaha I love that you say babysteps
It was steps now it is babysteps
Ok anyway
I did that but I am not sure if it is correct
Also in that I ve added the load highscore
u never used the difficulty argument here
You mean inside the loadhighscore method?
or in the difficulties for each
yes inside the method
I use it insdie
inside the method
is this code still the same or changed?
I changed something
I did if condition
i wonder how it looks now
I did something like that but I am not sure.
what does isClicked do?
wait
Give me some more hints please if you can
i just did
I removed it
why did u put it in the first place?
oh
??
Maybe something like that
HasKey is redundant here
as GetInt will return 0 if "highScore" wasnt stored in ur pc
We will continue tomorrow bro bb I just tired a little bit with all of that
When I will have free time we will continue
bye
He, bro!
I made highscore to show the highscore in the start of the game
Now, I am gonna make it so that it will show for each difficulty the highscore
I dont need any help right now if i will need i will tell oyu
yoyu
you
Range need some help
In game manager it says that in line 124
this one
what says what in line 124?
menu screen.gameobjects.setactive
it is that in the screenshot i send you
@ReactiveVeina Do you have anything to say about that message?
here
@TheRanger I change something in LoadHighScore() and in DifficultyForEach() and check my code right now how it looks, but I still got a problem with saving my highscore for each difficulty. I already fixed the problem that my highscore was 0 at the start of the game. Now, for each button when i click and play it saves the highscore.
Check that with startgame(), LoadHighScore() and DifficultyForEach()
Yes, I saw. What is your question about that?
I don't understand what that means?
It says that it is redundant
what what means?
what says that what is redundant?
I don't understand why it says that?
Check
Here
For that
why what says what?
you sent a screenshot showing a line of code, yes
you've linked it to me multiple times
I've seen it
it doesn't "say" anything
multiple times?
it's a line of code
yes check the message
what is your question about that line of code?
it says that this line of code is redundant
what does?
indirection is redundarnt
redundant
about this line of code
what says that?
I solve the problem myself. I just find what was the problme
problem
menuscreen.setactive(false)
i don't need gameobject
k
game object is redundant
What do you have to say about my problem with difficulties?
I am clicking every difficulty easy, medium and hard and what i see is that it saves the highscore in each difficulty but it is the same for each
the game starts with score to be 0 and highscore
BlazeBin - eazlgjkqziix
A tool for sharing your source code with the world!
Here is the script of game manager
where is the code that saves the high score value?
Here i have my difficultybutton script
wait
Check game manager
There is a method i have that saves the highscore
BlazeBin - zivsciytsbhy
A tool for sharing your source code with the world!
Here is also another script where the game gets over then it saves highscore and all of that happen
so, StartGame() is what loads the high scores?
yes
it is
great
Startgame() loads it
where is the code that saves the high scores?
Give me a minute
ive been summoned
ur
LoadHighScoreOnStartGame
method is only loading highscore from ur disk without knowing which difficulty is chosen So, this is the code that saves highscore on game over in the other script I have sent called target script
Here is also the UpdateHighScore() where it updates the highscore at the start of the game and at the game over it display the highscore.
so, PlayerPerfs.Save()
finish the LoadHighScoreOnStartGame method first
go with what he says, he's read into this already
oh, I think I know what to do. You give me an idea, gotta try if it works
Hey, wanna help me with the game?
I did some changes some more changes in my code and actually in LoadHighScore()
what kind of changes?
Check but I still cant do it my mind doesn't know how to solve this problem, I am not skilled enough maybe.
show what you did atleast
I have tried to communicate with DifficultyButton maybe I dont have to take that difficulty in game manager and take the other one from the other script
what does
highScore = PlayerPrefs.GetInt("highScore", highScore);
do, in ur mind?Ok
In my mind, it gets the highscore and stores the highscore to variable highscore thats how I understand it
which highscore tho?
The highscore that is saved
That was saved
which one?
When i did
u have easy, medium and hard highscores
right?
you mean variables?
no
yes i have
each difficulty has its own highScore right?
three highscore
yes
how does it know which highScore difficulty to load from ur disk
So, it doesn't actually. What I am thinking is for sure that I have to use an if statement and inside it to check which difficulty corresponds to each difficulty button and I check when it get clicked then I do save highscore and all of that happen
you already did use an if statement
yes
That's what I ve made acutally
u have
highScore = PlayerPrefs.GetInt("highScore", highScore)
in ur 3 if conditions
all of them will load the same highScore
for example if you got score 10,000 in hard mode
then u restarted the game and played easy
it will load 10,000 as the highscoreyes
That's what it does
So, let me check
yes, u are only saving 1 single highscore in your disk, and all the bodies of ur if conditions load from that same single highscore
What I did was to make three different variables and then i check in the if conditions when it is higher than the score then save it to highscore something like that
I can just add three texts for highscore
I can remove that highscore i have right now
and make three highscore texts in my canvas with these variables i have made
3 different variables?
yes
can u show
I can do that
three highscore texts? are u trying to display the 3 at the same time?
no
You didn't understand
then why did you make three highscore texts?
I didn't make
I am just thinking to make
ok, why?
I will make three checksavemethods for these variables
ok lets talk about the 3 variables first, can u show it?
So, like you see in checksavehighscore
I ve made three more methods
those are methods not variables
Yes i have also variables that i will use
I have also these variables
that i will use in these methods
I wanna remove the first method
no need to do the 3 variables
1 is enough
and just check save the highscore with the other three methods i ve made
u have no reason to load the 3 highscores into the memory
ok
only load what is needed
So, I still didn't find the problem. I know like you said it is in the loadhighscore method because I am using player prefs in every if condition
the same highscore for every difficulty
I set int
and then get the highscore the same highscore from it
a video game doesn't load all of the levels in the game when u are only playing the selected level
waste of memory
I know
it only loads the selected level
same with highscore, u only load one of the three
like ur highScoreText
u dont have highScoreTextEasy, highScoreTextMedium, highScoreTextHard
right?
yes
Thats why I wanted to make three variables
wrong
I need three texts?
no,
I dont think so
same with the variable
you only need one
I just have to use it outside of the if conditions and once?
I don't have to use them in every if condition?
the player prefs i mean
you do, but you have to specify which data to load
I am thinking when i use player prefs in conditions that it does everything separately but it doesn't actually works like that. It loads the same highscore for every diff
ud have something like that
no
I have highscore variable a single one for every diff
oh wait
you dont need to
load only whats needed
if ur playing on easy, no need to load the medium and hard difficulty highscore
oh so i actually dont need the variable highscore next to GetInt("highScore")?
the 2nd parameter is the default value that it returns if it couldnt find the specified data in ur pc
the second parameter in dfficulty button you mean?
as mentioned in Unity's API https://docs.unity3d.com/ScriptReference/PlayerPrefs.GetInt.html
in the GetInt method
say u did
highScore = PlayerPrefs.GetInt("highScore_Easy",500);
if highScore_Easy
data wasnt saved in ur computer
highScore's value will be 500yes
so when I use set int I basically save that data in my computer and if I don't use get int then I will never retrieve that data that was saved right?
yes
You said above about my code. I have it like that
highScore = PlayerPrefs.GetInt("highScore", highScore);
I actually don't need the isclicked bool so I removed it
in this method
is the same for each difficulty
yes
so it wont differentiate the highscores
Up there you made some changes in the string inside player prefs
you did
yup thats the correct way, PlayerPrefs there knows which HighScore to load depending on the difficulty
in player prefs without the int value
nexto it
next to it
oh really
yeah if the key
highScoreEasy
wasnt saved in ur computer
0 will be returnedYou have used UpdateHighScore() outside of the conditions why you did that?
because it will get called anyway
and will update the highscore
ok
the cleaner the code the better programmer u are
But I could have the UpdateHighScore() inside the conditions right?
It wasn't a problem I mean?
yeah
but still, make ur code cleaner
Can you please explain to me what that code does?
what code?
I got confused especially with this block of code.
When we use haskey?
Why we use it?
to check if
highScore
data is saved in ur pcDon't we do that with that
no, ur saving the highscore there
I don't check it?
u are not checking if
highScore data is stored in ur pc
thereI am just checking the values of the variables or what?
in
if (score > highScore)
yesSo it has a difference when I check values of variables and those values on my pc
?
values of variables is what is stored in ur current game
yes
PlayerPrefs is what is stored in your computer, your hard disk
in a file somewhere
Oh it is more understandable right now
you can imagine a file looks like this
yes
So, that code actually saves the highscore somewhere in a file right?
no, it loads it
GetInt loads, SetInt saves
oh, ok
brb
back
I removed highscore next to string in player prefs in loadhighscore method but what happens is that in start game it starts with 0 in highscore
I did something like
you did
but with that highscore remains 0 in start game
for every diff
but it saves the highscore
because i see in console that the values is 40 for example
ofcourse theyre 0 u never saved them in the first place
But it works
it saves every highscore separately for each diff
like i see in console
and?
when u restart the game
when i restart everything goes to 0 and appears also
in highscore text
I will have to save it like you said
so highscore is 0 in text, right?
yes
when i restart and play again
well u need to save it
how does the code that save the highscore look like?
thats wrong
Is it?
yes, u should see why
ok
if u saved, u can imagine ur file will look like this
that highscore is neither for difficulty easy, or medium, or hard
š
I know why
I don't know why for one moment I thought I could do it but no š
You said that this highscore is neither for difficuly or any difficulty? You mean this highscore that I save in CheckSaveHighScore()?
yes
it doesnt represent any of them
if you start the game and pressed one of the difficulty buttons, the game will run this method
say u chose Easy
it will try to find the value of
highScoreEasy
in the playerPref's file
but it couldn't find it because, only
is stored in the fileUnderstood
Check what I did
I did something like that but it doesn't work like I wanted
what do u mean?
oh yea
game doesnt know what difficulty ur on
go back to the startgame method
see this variable other there
oh
the GameManager's variable, field
Yes i have it in game manager
you never set it
What do you mean I ve set it
?
never set it
you never assigned a value to itoh yes
I know that
Do i really have to assign a value?
What values does it take?
you take the value from the argument difficulty in the StartGame method
I got that argument
note why i put
this
keyword
because the method has another variable called difficultyI dont know what is actually I have never used it before and is there a difference between the parameter difficulty and field difficuly?
difficulty = difficulty;
would just set a value to its own
and we want to change the field one, not the parameter oneI cant separate them I cant understnad the difference
big difference
the parameter passes a value to another method or the main program but field what actually do
?
the field is a variable that belongs to the class
not the method
ok
Yes i know that
But for which purposes we use them?
to store data?
when u run this
Here I am using the field difficulty variable
it takes the
difficulty
field oneCheck
no need to add
this
because there is no parameter or local variable called difficulty
in this method
so C# automatically knows u are refering to the field oneok
but its optional, u can still add it
Maybe I have to use the parameter and not the field
in the checksave()?
I am using the field in it and in loadhighscore the parameter diff
go ahead, but where would u get the value of the difficulty that u chose from? š
Thats a good question XD.
You said where I would get the value of difficulty that I chose from. What do you mean?
if u used the parameter and not the field in the checksave
yes
where would you pass the value to the method from?
I have some trouble to work with parameters, we have in school as well an assumed programming language and I have some difficulties in working with them sorry but I will try XD
I ve added the parameter in checksavehighscore
now the difficulty is a parameter and not the field that was before
I ve added public difficulties difficulty in target script so that I can pass the value in the checksave highscore where i call it in destroy out of bounds method
Where it goes game over if target is out of bound
I don't know if that's correct but I am gonna try it
in that
In that sorry
why does target have a field called difficulty?
it doesnt make sense for each target to have a difficulty value
Because it says that
It doesnn't make sense why?
the game has a difficulty, not a target
can each target have different difficulties in easy game mode?
XD no
my point
you dont need a parameter to the method when the class already has a field
then why I am using parameter in checksave method?
ask yourself that
Stelios why are you using the parameter? Ok, anyway I will sit and think again what am i doing wrong
TheRanger#3357
REPL Result: Success
Result: int
Compile: 534.390ms | Execution: 31.081ms | React with ā to remove this embed.
a parameter is just a variable that you pass when ur calling a method
Oh, so i have to return a value
if I am not mistaken
no
TheRanger#3357
REPL Result: Success
Console Output
Compile: 608.393ms | Execution: 33.619ms | React with ā to remove this embed.
point is, when u pass 2 and 3 when u called the sum method
a's value became 2, b's value became 3
then u sum them
Yes it does the sum i know that
then a and b gets deleted when method finishes its execution
u really need to learn the basics before u even code unity
ur just wasting time
I have to I know but I have started very quickly this year
because of exams like i said
continue later then
I just want to finish this I cant leave it here.
why?
I will have it in my mind that I didn't finished a certain task
reprogram ur brain
finishing a task with cheating is not helpful
it wont let you learn
I know that š
learn to move on
I don't cheat in school, for example when I write a test or something I am trying my best I have never cheated only when I was in high school
well now dont start cheating or it will become a habit that will really get be hard to get rid of
I am not cheating XD
Who ever told you that??
here i mean
im just giving you code
I didn't tell you to give me any answers, just some tips and maybe some hints like you did and do
thats not how i see it
The basics of C# I have learned bro
I haven't learned OOP in this programming language
not sure what basics mean to u
I just started
OOP is one of the basics of C#
Only functional programming i ve learned
so whats left to u in ur game?
loops, if else statements, data types, data structures lists and more
So, only the highscores to be saved in each difficulty
remove the parameter from CheckSaveHighScore method
Can I ask why do i have to do that?
because you already have the difficulty field in the class
Just give me an explantation
oh ok
You said in game manager to remove it?
Or in target script?
the field? target script yeah
I removed it
it says no context
?
no argument
sorry
well thats normal
it doesnt exist anymore
this
i did say this
seems u dont read all of my messages
I read
no
I told you in game manager or target
you said target
what u removed there is the argument
parameter is where it sits in the definition of the method
so in game manager
public void CheckSaveHighScore()
it is in game manager
was talking about the field
ok
I did that
how does
CheckSaveHighScore
method and StartGame
method look like now?replace
public void CheckSaveHighScore(Difficulties difficulty)
with public void CheckSaveHighScore()
u forgot to add this.difficulty in StartGame like i mentioned earlierYou said that i dont have to
where?
here
that was for
CheckSaveHighScore
not StartGame
ok
so i have to add in start game this.difficulty
this.difficulty what actually do?
pretty sure i said earlier,
it refers to the field
difficulty
insteadok
I don't need the playerprefs.has key in if condition in start game?
do u know what it does?
It loads the highscore in the start of the game if its true
yes but which highscore? easy or medium or hard?
that which is in the loadhighscore method that I call it in start game so when difficulty is difficulty.easy then it loads the highscore of the current difficulty
??
forget that
im asking u what
does
I told you
forget the code around it
pretend loadhighscore doesnt exist
this piece of code is not in the loadhighscore method
its in StartGame
It says in haskey that it returns true if the given key exists in player prefs
but which of player prefs i dont know
and why do u need it?
So, I dont need it for that reason right?
I dont have an existing key that is in player prefs there?
you never saved
highScore
in ur player prefs file
u only save highScoreEasy
, highScoreMedium
, and highScoreHard
in which method
you say that I didn't saved highscore
in the checksavehighscore
so just delete this part, u dont need it
+ u are already loading the highscore from the loadhighscore method
ok i did a change
I ve added highscore that i forget like you said
this
for which difficulty?
delete it
ur saving a highscore that has no difficulty, which is pointless
ok
You said i have a highscore there that is never saved
yes?
I dont see a highscore that is never saved, at least I cant notice it
highscore saved to highscore easy
hgishcore saved to highscore mid
and hard as well
I did it bro
It works
I just test the game it works like I wanted
There is nothing missing like you said
Let me check again and confirm that
Yes it works
In easy it saves the highscore so i did 265 it save that
dont start a task again until u learn the basics first
in mid i did 240 it save that when i restart it is the same
so every highscore in each difficulty is different
I am not gonna start any task again I am going off till 12 june so thanks for the help you gave me, I learned something from you i think. For example how to use player prefs I understood the logic of how they save and how the load highscore for example. I still need like you said practice in basics of OOP in C# and also the basics in functional programming for example data structures, data types, for loops, while loops, break, continue and all of that. This summer I am gonna work with C# and Game Dev 5-6 hours per day that's my goal for this summer, so that when I will start college I will be ready.
Do you have to say anything about that?? Can you close this thread?
sure