#score machine broke
1 messages · Page 1 of 1 (latest)
so, Awake does five things
Instance = this;
this sets the static field so that anyone can find us
scoreText.text = "Souls: " + score.Tostring();
this sets up the score display
aside: you can just do this
"Souls: " + score;
ToString() is implicitly used here
oh okay
PlayerPrefs.SetInt("PlayerScore", score);
this sets PlayerScore to the current value of score
...but
what is score?
score is set to a 0 at the start
every time the game starts it automatically set the value to 0? sorry for bad english Im from Croatia
oh so I need to remove it from awake?
Yes, there's no reason to set the score in Awake
you should probably get the score, though!
so that you can display the correct value at the start
PlayerPrefs does not automatically do anything for you. It won't "remember" that score had a value of 20 last time you played
It just stores values for you.
So if you want to get that number back, you need to do score = PlayerPrefs.GetInt("PlayerScore");
at awake?
You should get the score before you use it to set the score counter's text
This will show 0 until you get a point
then it will update to show the correct score
Im sorry I dont get it what to do
let me phrase this another way
score = 0;
scoreText.text = score;
score = 10;
what will the score text be?
0
right
but you probably want it to be the player's score, not zero.
score = PlayerPrefs.GetInt("PlayerScore");
scoreText.text = "Score: " + score;
this will get the saved score, then display it.
okay so this should work
That looks reasonable, yes.
yea it works for score, but deaths are not working
does the death counter go up correctly when you die?
oh nvm, i fixed it by doinf deathScoreText.text = "Deaths: " + deathScore;
thank you so much, sorry for wasting your time
could I do the same for saving player position
no problem! (:
and yeah, it'll just be a bit more annoying, because you can't directly save a Vector3
You could just save three floats
cause Im trying to do metroidvania style so when I got to a place to save and click "E" I want to save position and score he had
okay
So you should only save your score if you make it to a save point?
If so, you should not set PlayerScore every time the score goes up
yea but I dont know how to do this
I would add a trigger collider to the save point
It should use OnTriggerEnter and OnTriggerExit to detect when the player enters and exits the save point.
While the player is present, it should display some text telling you to push E to save
and if that key is pushed, it tells CounterManager to save the game.
I have this bench with box collider 2d, when I click "E" OnTriggerEnter2D nothing happens but I want to save like data
are you using OnTriggerEnter2D to display the text?
You shouldn't be checking if the player pushed E in that function
they would only get a single frame to do it
no
I dont have benchSave implemented cause I dont know how to do it
don't check if the player is pushing E in OnTriggerEnter2D
Right now I have pause menu in which is text to display score and deaths, so when I pause it tell me score and how many times I died
they will have to push E at the same instant they touch the collider
OnTriggerStay2D?
yeah, that works
well, you know how to update the score and death counters, right?
just do that in a function called BenchSave
same thing we did right?
and then call that function
I would probably just save a number that tells the game which bench to spawn the player at
but it will be easier to store the player's position instead
well, where do you decide if the player wants to save?
more specifically, in that if statement
yes I did that
but what should I do in BenchSave
I will save data like we did before?
well, what do you want it to do?
save score points and death count and also player transform
so, you already know how to save the player's score and death count
as for the transform, I would just save the player's X and Y position
I can just copy paste?
and then, when you respawn the player, you just set the player's position
not exactly
the code you wrote was in CounterManager
can I do a public Transform playerPos; and somehow store it
so it could just access score directly
now, you will need to go find the CounterManager and ask it for the score
no, you can only store ints, floats, and strings in PlayerPrefs
by instance?
oh okay
so, just store the transform.position.x and transform.position.y floats
right, since that's how you find the single CounterManager object
why are you reading a preference when saving?
you should be setting things when saving.
also, score and deathScore are private
so there is no way for anyone else to see them
perhaps there should be a method that returns the current score (or they should just be public)
you need to slow down and think about what each line does
BenchSaving needs to save the current score
So there is no reason for it to be changing the current score
set is doing to set value and get will retrieve value
erase what you have and write it from scratch; don't try to copy paste from other places
start by just writing a function that sets PlayerScore and PlayerDeaths to zero
show it?
that does not set PlayerScore and PlayerDeaths to zero
you've just declared two fields
I mean that BenchSaving should set those two PlayerPrefs values to be zero.
Okay, that works.
Now, instead of just setting them to zero, you should set them to the current values the CounterManager has
Right!
okay what should I do right now
well, what else do you need to save?
you know how to save the score and death count, right?
yes
PlayerPrefs.SetFloat and PlayerPrefs.GetFloat can save and load floats
yea but I dont know how to do like I did private int score
idk how to do it for axes, is it like private float x;?
you need to get the player's x position
I only know how to do it with public Transform player; never tried to get x pos
or y
You wouldn't want to do it in Start()
That would get the player's position when the BenchSave component turned on
I'd just do PlayerPrefs.SetFloat("PlayerXpos", player.transform.position.x);
That sounds reasonable.
Now you just need to use those saved values when you load the game
one thing: you can't do this
player.transform.position.x = 1;
it's because position is a struct: it's just a value, like 2 or 1.3 or true
so player.transform.position gives you a copy of the player's position
and setting the x value on the copy wouldn't do anything
so, you do this instead:
player.transform.position = new Vector3(savedX, savedY, 0);
where savedX and savedY have the values of those saved x and y positions
wait sorry where should I put this: player.transform.position = new Vector3(savedX, savedY, 0);
well, what does that line of code do?
It sets the player's position by creating a new vector
that vector is made from two variables, savedX and savedY
we want to read the saved x and y positions
so, how do you think we're going to do that?
with playerPrefs?
right
no, because that's setting PlayerXpos and PlayerYpos to zero
or whatever savedX and svaedY are
you need to get the values.
ok so i set it as GetFloat
you'd do savedX = PlayerPrefs.GetFloat("PlayerXPos");, yes
That will set the player's position when the bench component gets created
Probably not the right timing.
You'll want to do that when you respawn the player
so at the start of the game
cause I want him to save at bench, and when player turn off the game and go back in he will be at the last bench he saved with data he saved last
Yeah
so here when I click continue and I dont have save it wont do anything but if I saved I want it to load last bench
What should I do in here
the opposite of what you do in the save function, pretty much
so, this
also, this is wrong: savedX and savedY have not been set!
so they will be zero
are you unclear on the difference between SetFloat and GetFloat?
SetFloat stores a number. GetFloat gives you a number.
Oh I always mix it sorry
Ok so same thing but GetFloat
That will get the saved value
But this is not useful if you're trying to save the player's current position
Wait so first thing Im trying to do is to save a position and score when I click E at the bench
And this script doesnt do that yet
yes, in BenchSaving
So I need to do something different with savedX and savedY to make it work
you do not get the player's position in BenchSaving, so it is wrong
you read it in Start(), but the player's position is almost certainly going to change by the time you reach the bench!
They should be updated with the player's current position, yet.
You can just use player -- the Transform field will still be valid
okay so savedX = player.transform.position.x wont work
no, that's fine, but you just have to do it at the right time
if you only do it in Start(), then you will have an old position
do it while you're saving the game
So script is now all good for saving part?
It looks reasonable, yes.
But to make it work
I need to make button work
To load me there with last saved data and pos
hmm
I would make a static field called "loadSave" or something
set it to true if you hit Continue and false if you hit New Game
use that to decide if you should load
you need to declare that field somewhere
I guess I would put it on the CounterManager?
public static bool myField; will create a field that can be accessed from anywhere
Right.
and what should I do in my continueButton void
set that field!
so CounterManager.myField = true;
this is the script so far
is this good
its not good it does not work this way hahaha
that looks fine, but you now need to use that field to make a decision
do you load the save data, or do you just start the player at the default spot with 0 score and 0 deaths?
so in update I do if(myField == true) do all of the things in continue
no, you should not check this in Update
that would do something every frame
when the scene loads, you need to decide if you're going to load the saved game or not
?
@terse lantern pls help me just fix this, I know Im anoying, Im just trying to get it work. Im 15 years old and I dont know to do that much of a coding so thats why I dont get it what to do sometimes
You need to run this after the new scene loads
All of that is going to run on the title screen.
I would add a component to the game scene that, on start, checks that field and decides if it needs to load
so I need to create new scrišt
script
add it to a gameobject in my game scene
and in start function just copy paste ContinueButton
well, copy most of it
just the part that checks if you clicked the continue button
I would rename myField to continueGame
one issue: = is assignment
you want == to compare
or, you can just omit it entirely
if (CounterManager.continueGame)
oh yea I accidentally put one =
it does not work
it didnt load my player position at the bench
I would check that the Start() function is entering the if statement at all
by logging when that part runs
also, hang on
you're not doing anything with the results of GetInt and GetFloat
The second argument for those is the default value (if it cannot find something with that name)
you need to save the results, then set the player's position
No.
You are not setting anything.
You are loading things.
You need to do something with the values you load.
remember this?
yea
that will set the player's position, based on the saved X and Y values
so I need to use that in my load code?
Right.
Okay
well yes
you are still not doing anything with those numbers
None of these lines do anything.
They get a number and throw it away.
If you do not understand what's wrong here, then you need more experience with the basics.
they dont save it?
They do nothing.
They get a number and then the number goes away.
it's like saying 3;
you need to store those numbers in a variable, then do something with that variable
PlayerXpos and PlayerYpos u mean right
All four of them.
in those i need to store numbers
again, PlayerPrefs.GetInt("PlayerScore", CounterManager.Instance.score); does not get the value of PlayerScore and save it into anything
public static int GetInt(string key, int defaultValue);
the second argument is just the default value
string key being here "PlayerXpos" and int defaultValue is not given
Right.
no, you need to store the number it gives you
GetFloat("PlayerXpos") returns a float
e.g.
float result = PlayerPrefs.GetFloat("PlayerXpos");
no, that is overwriting the saved data
you should not be calling SetFloat anywhere when you are loading a save.
This changes the position of the player.
Yeah, that seems reasonable.
That looks correct.
?
?
bro
my PC crashed
idk how i sent so many ?
haha
so this is good
It does not work
this are all scripts
do you see whats wrong in any of them
Okay I set it to Set
is that only problem?
It doesnt work still, also what I noticed is at the button when I click "continue" at the start of the game it still loads the game and sets the CounterManager.continueGame = true; still, and I dont have save yet
?
Pls, do you see anything wrong in the scripts cause idk why it wont work and ive been trying whole day, i change it to SetFloat the thing u said to change
So what does your code look like now?
Which code u want to see, I sent u BenchSave
BenchSave looks okay. It saves your score, death count, and position.
What about Load?
You should add some Debug.Log() statements to the Load class's functions to see if they are running
This still doesn't do anything.
CountManager's Awake function will read the score and death count, though
I would suggest moving that into a function called "Load" on CountManager, that you call if you want to load the saved game
That way, it will not load the score if you start a new game
okay I will do that rn just a sec
This should be setting the player's position. Does anything else change the player's position?
You could also try changing it to just put you in a weird position
like player.transform.position = Vector3.up * 1000;
and seeing if that happens
all I would say to change is to copy the two lines in Awake
that set .text
into LoadGame
and then remove the lines that called GetInt in Awake
So, when Awake runs, it will set your score and death counters to 0
then, if LoadGame is called, it will update them
This is still wrong.
You are doing nothing with those numbers.
I want you to check every place you use GetInt and GetFloat and make sure that they are all being stored in a variable.
should I remove them
You can remove the second arguments from GetInt. They aren't useful.
but otherwise, yes, that should work.
It seems like it should work. If it is still not behaving, I would suggest logging the values of savedX and savedY and seeing if they make sense
hmm still wont work, do you want me to record my game so u understand better what is happening in game maybe
no, I want you to try this
look at those numbers and compare them to where the player appears when the game starts
sorry what does logging mean
haha cool we learned that in school first time using something
Debug.Log(saveX);
oh okay
I use it to make better-formatted logs though
Debug.Log($"Player position: {savedX} {savedY}");
Right.
When the game starts, compare those numbers to where the player winds up
I'm wondering if something else is setting the player's position
is this after you saved at a bench?
nope, but it is the same
if so, then your saving isn't working right
it saves score and death
but player is spawning at the start
but in health script I have that when player dies his rigidbody freeze all rotation and position
maybe this causes a problem
do you think this messes up with load script?
ah, that's possible
If you have a Rigidbody2D, then you should avoid setting transform.position
okay so I can do that at the start of health script I set rigidbody to not kinematic and dont freeze
I know, I set that up cause when my player died he kept moving left idk why
well, you should avoid it for anything that should actually use physics
it would still make sense here, since the player is just teleporting
however, as long as you're getting (0,0) here, something is wrong
Add a log statement to the place that saves your position, and make sure that the numbers are right
so, in BenchSave, log savedX and savedY
well I have good news
this did a trick BUT camera does not follow a player
this is for my camera follow
and this is a trigger that player needs to past so camera follows to a next room
oh ####...do I need to save camera position aswell?
ah, yes, you'll need to remember that, too.
well, it should be pretty easy to add
figure out a way to assign a number to every room
store that number
cant i just save camera's x and y pos as I did with player
you need to figure out which room is the next one, though
it looks like you have that stored in the NextRoomObj
yea but I followed an tutorial for that camera movement so I dont really know what is happening, but I do get it that I have NextRoomObj which tells me where will camera go
Would you want to partner with me in making of my game, you will be doing all the things that need playerprefs only when needed so u dont need to work on a game full time, just when I need playerprefs, and I will do everything else. We publish it on Steam, I will do 100% paying and I will make socials for a game so I present it to the world and people start to recognize it and wishlist. The game revenue will be 80% to me and 20% to you. Are u in for that deal?
This is a serious offer, I really want you to think cause with your help I think we can make great game. Imagine we get big numbers on a game on steam, you will get 20% of it for working an hour or two a day on it. Please think about my offer cause I really think it will going to be great
@terse lantern
?
sorry, I am not interested (and I don't think that it's a good idea for a minor to try to make contracts for revenue-splitting like this).
ahh its okay, I asked u for that cause when I think about it I would need to implement a lot of playerprefs
cause Im stuck at a camera
you need to learn how to implement this stuff yourself!
and if I want to for example make a shop in which you can buy a lot of different helmets that would make your health bigger I would need to save players sprite renderer and health too
im trying
for example, for gear
i tried to make a camera myself and I did it wrong
imagine giving each piece of gear a separate name
then you can use SetInt to store a 1 if you have that item
so if GetInt("Gear-HelmetBasic") returns a 1, you have that item unlockced
I thought about like having for example 5 different helmets, first helmet is number 1, second is number 2 and etc. And I save that number and if I bought helmet number two, that int will be set to an 2 and it will change sprite renderer to a different sprite
That also works if the helmets are bought in sequence
but still I think that is easier than to do a camera, cause I dont know how to make a camera remember in which room it was before I quit game. Would you want to help me with camera and then I wont bother you anymore please.