#score machine broke

1 messages · Page 1 of 1 (latest)

terse lantern
#

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

turbid cradle
#

oh okay

terse lantern
#

PlayerPrefs.SetInt("PlayerScore", score);

this sets PlayerScore to the current value of score

#

...but

#

what is score?

turbid cradle
#

score is set to a 0 at the start

terse lantern
#

correct

#

so, when that line runs...

turbid cradle
#

every time the game starts it automatically set the value to 0? sorry for bad english Im from Croatia

terse lantern
#

Exactly.

#

You're setting PlayerScore to zero.

turbid cradle
#

oh so I need to remove it from awake?

terse lantern
#

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");

turbid cradle
#

at awake?

terse lantern
#

Right.

#

Awake runs the instant that CounterManager is created.

turbid cradle
#

so this should work now?

terse lantern
#

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

turbid cradle
#

Im sorry I dont get it what to do

terse lantern
#

let me phrase this another way

#
score = 0;
scoreText.text = score;
score = 10;
#

what will the score text be?

turbid cradle
#

0

terse lantern
#

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.

turbid cradle
#

okay so this should work

terse lantern
#

That looks reasonable, yes.

turbid cradle
#

yea it works for score, but deaths are not working

terse lantern
#

does the death counter go up correctly when you die?

turbid cradle
#

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

terse lantern
#

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

turbid cradle
#

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

terse lantern
#

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

turbid cradle
terse lantern
#

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.

turbid cradle
#

I have this bench with box collider 2d, when I click "E" OnTriggerEnter2D nothing happens but I want to save like data

terse lantern
#

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

turbid cradle
#

I dont have benchSave implemented cause I dont know how to do it

terse lantern
#

don't check if the player is pushing E in OnTriggerEnter2D

turbid cradle
#

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

terse lantern
#

they will have to push E at the same instant they touch the collider

turbid cradle
terse lantern
#

yeah, that works

terse lantern
#

just do that in a function called BenchSave

turbid cradle
#

same thing we did right?

terse lantern
#

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

turbid cradle
#

where should I call it from

terse lantern
#

well, where do you decide if the player wants to save?

turbid cradle
#

OnTriggerStay

#

so i will call it there

terse lantern
#

more specifically, in that if statement

turbid cradle
#

yes I did that

#

but what should I do in BenchSave

#

I will save data like we did before?

terse lantern
#

well, what do you want it to do?

turbid cradle
#

save score points and death count and also player transform

terse lantern
#

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

turbid cradle
#

I can just copy paste?

terse lantern
#

and then, when you respawn the player, you just set the player's position

terse lantern
#

the code you wrote was in CounterManager

turbid cradle
terse lantern
#

so it could just access score directly

#

now, you will need to go find the CounterManager and ask it for the score

terse lantern
turbid cradle
#

by instance?

terse lantern
#

so, just store the transform.position.x and transform.position.y floats

terse lantern
turbid cradle
#

it wont work like this

terse lantern
#

why are you reading a preference when saving?

#

you should be setting things when saving.

terse lantern
#

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)

turbid cradle
#

i need to make them public right

#

okay i did

#

should I use get then somehow?

terse lantern
#

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

turbid cradle
#

set is doing to set value and get will retrieve value

terse lantern
#

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

turbid cradle
#

okay

#

i did

terse lantern
#

show it?

turbid cradle
terse lantern
#

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.

turbid cradle
terse lantern
#

again, that is just setting some fields

#

this function set the PlayerScore value

turbid cradle
terse lantern
#

Okay, that works.

#

Now, instead of just setting them to zero, you should set them to the current values the CounterManager has

turbid cradle
#

so this is when I use instance to set it as the same value counterManager has

terse lantern
#

Right!

turbid cradle
#

okay what should I do right now

terse lantern
#

well, what else do you need to save?

turbid cradle
#

position but I never saved x and y axes

#

so i dont really know how to do that

terse lantern
#

you know how to save the score and death count, right?

turbid cradle
#

yes

terse lantern
#

PlayerPrefs.SetFloat and PlayerPrefs.GetFloat can save and load floats

turbid cradle
#

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;?

terse lantern
#

you need to get the player's x position

turbid cradle
#

I only know how to do it with public Transform player; never tried to get x pos

#

or y

terse lantern
#

Transform has a position field

#

and that's a Vector3

turbid cradle
#

could I do like private float x; and then set x = player.position.x

terse lantern
#

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

turbid cradle
terse lantern
#

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

turbid cradle
#

wait sorry where should I put this: player.transform.position = new Vector3(savedX, savedY, 0);

terse lantern
#

well, what does that line of code do?

turbid cradle
#

i dont really know sorry

#

player position is set as vector

#

idk

terse lantern
#

It sets the player's position by creating a new vector

#

that vector is made from two variables, savedX and savedY

turbid cradle
#

but how is savedX set

#

what is savedX set as

#

and savedY

terse lantern
#

we want to read the saved x and y positions

#

so, how do you think we're going to do that?

turbid cradle
#

with playerPrefs?

terse lantern
#

right

turbid cradle
#

so this is good?

terse lantern
#

no, because that's setting PlayerXpos and PlayerYpos to zero

#

or whatever savedX and svaedY are

#

you need to get the values.

turbid cradle
#

ok so i set it as GetFloat

terse lantern
#

you'd do savedX = PlayerPrefs.GetFloat("PlayerXPos");, yes

turbid cradle
#

Yes

#

is this good

terse lantern
#

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

turbid cradle
#

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

terse lantern
#

right

#

or maybe when the player dies and respawns

turbid cradle
#

oh yea so I need to put it on two places?

#

at death and start of the game?

terse lantern
#

Yeah

turbid cradle
#

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

terse lantern
#

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

turbid cradle
#

how should I set savedX snd savedY?

#

savedX = PlayerPrefs.SetFloat?

terse lantern
#

are you unclear on the difference between SetFloat and GetFloat?

#

SetFloat stores a number. GetFloat gives you a number.

turbid cradle
#

Oh I always mix it sorry

turbid cradle
terse lantern
#

That will get the saved value

#

But this is not useful if you're trying to save the player's current position

turbid cradle
#

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

terse lantern
#

yes, in BenchSaving

turbid cradle
#

So I need to do something different with savedX and savedY to make it work

terse lantern
#

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!

turbid cradle
#

so I need to change savedX and savedY

#

To store x position of a player and y

terse lantern
#

They should be updated with the player's current position, yet.

#

You can just use player -- the Transform field will still be valid

turbid cradle
#

okay so savedX = player.transform.position.x wont work

terse lantern
#

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

turbid cradle
#

So when pressing E

#

Like this

terse lantern
#

I would do it in BenchSaving itself

#

but that's fine

turbid cradle
#

So script is now all good for saving part?

terse lantern
#

It looks reasonable, yes.

turbid cradle
#

But to make it work

turbid cradle
#

To load me there with last saved data and pos

terse lantern
#

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

turbid cradle
terse lantern
#

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

turbid cradle
#

so like this

terse lantern
#

Right.

turbid cradle
#

and what should I do in my continueButton void

terse lantern
#

set that field!

turbid cradle
#

so CounterManager.myField = true;

turbid cradle
#

this is the script so far

#

is this good

#

its not good it does not work this way hahaha

terse lantern
# turbid cradle

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?

turbid cradle
#

so in update I do if(myField == true) do all of the things in continue

terse lantern
#

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

turbid cradle
#

doesnt work

#

do u know whats wrong with a script

turbid cradle
#

?

#

@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

terse lantern
#

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

turbid cradle
#

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

terse lantern
#

well, copy most of it

#

just the part that checks if you clicked the continue button

#

I would rename myField to continueGame

turbid cradle
terse lantern
#

one issue: = is assignment

#

you want == to compare

#

or, you can just omit it entirely

#

if (CounterManager.continueGame)

turbid cradle
#

oh yea I accidentally put one =

#

it does not work

#

it didnt load my player position at the bench

terse lantern
#

I would check that the Start() function is entering the if statement at all

#

by logging when that part runs

#

also, hang on

terse lantern
# turbid cradle

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

turbid cradle
#

so I need to use set first*

#

?

terse lantern
#

No.

#

You are not setting anything.

#

You are loading things.

#

You need to do something with the values you load.

turbid cradle
#

damn sorry idk what to do

#

can u tell me

terse lantern
#

remember this?

turbid cradle
#

yea

terse lantern
#

that will set the player's position, based on the saved X and Y values

turbid cradle
#

so I need to use that in my load code?

terse lantern
#

Right.

turbid cradle
#

Okay

turbid cradle
#

still nothing

terse lantern
#

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.

turbid cradle
#

they dont save it?

terse lantern
#

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

turbid cradle
terse lantern
#

All four of them.

turbid cradle
#

in those i need to store numbers

terse lantern
#

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

turbid cradle
terse lantern
#

Right.

turbid cradle
#

so I need to give it a value

#

which is x pos of a player

terse lantern
#

no, you need to store the number it gives you

#

GetFloat("PlayerXpos") returns a float

#

e.g.

#
float result = PlayerPrefs.GetFloat("PlayerXpos");
turbid cradle
terse lantern
#

Right.

#

now you need to set the player's position

turbid cradle
terse lantern
#

no, that is overwriting the saved data

#

you should not be calling SetFloat anywhere when you are loading a save.

terse lantern
turbid cradle
#

okay

#

Is that good

terse lantern
#

Yeah, that seems reasonable.

turbid cradle
#

?

#

?

terse lantern
#

That looks correct.

turbid cradle
#

?

#

?

#

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

terse lantern
#

you are still using GetFloat here

#

BenchSaving should be setting things.

turbid cradle
#

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

#

?

turbid cradle
#

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

terse lantern
#

So what does your code look like now?

turbid cradle
#

BenchSave script

#

I dont see anything wrong

turbid cradle
terse lantern
#

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

turbid cradle
#

debug "load" is working everytime I click continue button in main menu

terse lantern
#

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

turbid cradle
#

okay I will do that rn just a sec

terse lantern
#

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

turbid cradle
#

Load Script

#

CounterManager Script

terse lantern
#

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

turbid cradle
#

like this

terse lantern
#

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.

turbid cradle
terse lantern
#

You do need to keep them.

#

You need to load the score and death count.

turbid cradle
#

will this fix it, also I think this is only place where I use GetInt

terse lantern
#

You can remove the second arguments from GetInt. They aren't useful.

#

but otherwise, yes, that should work.

turbid cradle
#

ok I removed it

#

do you reckon it will work now???

terse lantern
#

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

turbid cradle
#

hmm still wont work, do you want me to record my game so u understand better what is happening in game maybe

terse lantern
#

look at those numbers and compare them to where the player appears when the game starts

turbid cradle
#

sorry what does logging mean

terse lantern
#

use Debug.Log

#

you can pass pretty much anything you want to Debug.Log

turbid cradle
#

so how to do it exactly with Debug Log

#

Debug.Log($"{saveX}"});

#

likee this?

terse lantern
#

that works

#

i see you found string interpolation

#

you can also just do

turbid cradle
#

haha cool we learned that in school first time using something

terse lantern
#

Debug.Log(saveX);

turbid cradle
#

oh okay

terse lantern
#

I use it to make better-formatted logs though

#
Debug.Log($"Player position: {savedX} {savedY}");
turbid cradle
#

okay like this

terse lantern
#

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

turbid cradle
#

i died and clicked continue to restart

terse lantern
#

is this after you saved at a bench?

turbid cradle
#

nope, but it is the same

terse lantern
#

if so, then your saving isn't working right

turbid cradle
#

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

turbid cradle
terse lantern
#

ah, that's possible

#

If you have a Rigidbody2D, then you should avoid setting transform.position

turbid cradle
#

okay so I can do that at the start of health script I set rigidbody to not kinematic and dont freeze

turbid cradle
terse lantern
#

well, you should avoid it for anything that should actually use physics

#

it would still make sense here, since the player is just teleporting

terse lantern
# turbid cradle

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

turbid cradle
#

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?

terse lantern
#

ah, yes, you'll need to remember that, too.

turbid cradle
#

omg im gonna kill myself hahahaha

#

i didnt think it trough

#

damn it

terse lantern
#

well, it should be pretty easy to add

#

figure out a way to assign a number to every room

#

store that number

turbid cradle
#

cant i just save camera's x and y pos as I did with player

terse lantern
#

you need to figure out which room is the next one, though

#

it looks like you have that stored in the NextRoomObj

turbid cradle
#

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

turbid cradle
#

where should I start with that

#

what should I do firs

#

first

turbid cradle
#

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

turbid cradle
#

@terse lantern

turbid cradle
#

?

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

turbid cradle
#

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

terse lantern
#

you need to learn how to implement this stuff yourself!

turbid cradle
#

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

terse lantern
#

for example, for gear

turbid cradle
#

i tried to make a camera myself and I did it wrong

terse lantern
#

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

turbid cradle
#

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

terse lantern
#

That also works if the helmets are bought in sequence

turbid cradle
#

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.