#The Great Refactoring

1 messages · Page 1 of 1 (latest)

hollow jewel
#

@trim dragon Let's move into here

#

Your game looks good. It's got good graphics and personality and whatnot. The code is just a mess. So let's start seriously combing through it and see if we can untangle it

trim dragon
#

ok

hollow jewel
#

Let's start with the Checkpoint script. Just to make sure we have all the info, can you show the inspector of the Checkpoint object, and the code for any of the scripts on it?

trim dragon
#

starcheck is for the checkpoint sprite when it is not passed. while the sprite starpassed is when you have passed the checkpoint which you can respawn from.
public bool checkpointReached; is to check is the checkpoint is passe dby any of the players or not
private SpriteRenderer checkpointSpriteRenderer; is so that the sprite can be changed to being passed

while i don't know about private GameObject _gameOverScreen; but it is used for if the player lost all lives down to 0 lives and when you die agian, it goes to a gameover screen

hollow jewel
#

So, it looks like the purpose of the checkpoint currently is to:

  1. Detect when a player has touched it
  2. Change sprite based on whether it's "active" or not
  3. Spawn the player when they die
#

I'd argue that 3 doesn't need to be the responsibility of the checkpoint. If there's a thing that tracks the last known checkpoint, it can ask for things like position from that checkpoint.

I notice a lot of code that is designed around pulling the player objects out of the ether to get references to various scripts. Perhaps it'd be better to have a sort of "Player Manager" that handles the respawning and referencing? The checkpoint would simply identify if a player touches it, and let that player manager know to update the most recent checkpoint.

#

Does this seem like something that makes sense?

trim dragon
hollow jewel
#

Ah, so there is one already

#

It's even already a singleton

hollow jewel
#

There's a good bit of ambiguously named variables here.

public static PlayerManager instance = null;
public bool p2 = false;
public bool _nextScene = false;

[field: SerializeField] public int X { get; private set; } = 1920;
[field: SerializeField] public int Y { get; private set; } = 720;
[field: SerializeField] public bool P2 { get; private set; } = false;

private int _playerLives = 3;

[SerializeField] private GameObject _gameOverScreen;

The static instance makes sense. _playerLives is straightforward as well.

The X and Y are a bit less concrete. Are they the size of the screen? What values do these represent? There's probably better names than just "X" or "Y".

I'm unsure about the two separate booleans called just P2, what are those supposed to be? Also _nextScene, what does this bool represent? And another reference to _gameOverScreen? Why does everything reference this, it'd probably be better for the game over screen to have a script of its own that checks data. We'll get to that later.

trim dragon
#

and that it have playermanager in every scene so that you can choose player 1 or 2 players

hollow jewel
#

So, P2 is true when there's two players? A better name for that would be isMultiplayer. And you have two copies of it, you probably don't want both.

#

_nextScene doesn't seem to be used in this script at all, so it's a candidate for being removed to go somewhere else later. For now, just move it somewhere else and we'll put in a comment that it's potentially to be removed later. So, let's start from a base that looks like this:

public static PlayerManager instance = null;

[field: SerializeField] public int screenWidth { get; private set; } = 1920;
[field: SerializeField] public int screenHeight { get; private set; } = 720;
[field: SerializeField] public bool isMultiplayer { get; private set; } = false;

private int _playerLives = 3;

//TODO: Determine if these variables need to be here
[SerializeField] private GameObject _gameOverScreen;
public bool _nextScene = false;
trim dragon
#

i use visual studio

hollow jewel
# trim dragon i use visual studio

Okay, so the Ctrl+R, Ctrl+R shortcut should pop up a box where you can bulk rename a variable. Using readable, expressive variable names goes a long way to making the code more maintainable and easier to fix down the line. Then you won't need to guess what a variable might mean

#

Or accidentally use it thinking it means something different. This is a small fix, but we're gonna make big changes by stacking a buncha small ones on top of each other

trim dragon
#

aren't we gonna fix that with respawn thing?

hollow jewel
# trim dragon aren't we gonna fix that with respawn thing?

Like I said, at this point, fixing it with another bandaid is just going to make the next fix take exponentially longer. The stuff I'm suggesting right now isn't going to directly fix the problem but is going to give you and anyone reading your code a much better understanding of what's happening and make the problems easier to solve.

#

If you don't want to go through with this refactor, that's entirely on you but it is at this point simply too much of a mess for anyone to reasonably be able to help at this point.

#

Or you could just slap this warning at the top of your code files

hollow jewel
#

All right cool beans. Now, I think a good job for the player manager to have is to be the thing that holds the one true canonical reference to the player objects, don't you think? Before we add code for that, let's take a look at the player objects. Can you send a screenshot of the entire player inspector, with all the components and tags and whatnot on them? Multiple screenshots is fine if it can't all fit on screen at once.

trim dragon
#

ok

trim dragon
hollow jewel
hollow jewel
trim dragon
hollow jewel
#

It can't be modified from outside of this script

trim dragon
hollow jewel
#

If this variable is intended to be changed outside of this class it should not have a private setter.

trim dragon
#

oh wait..

#

broguht the public bool p2 = false back

hollow jewel
hollow jewel
#

Do not

#

It was deleted for a reason

trim dragon
# hollow jewel No

but that's the only way so the red underline is no longer there anymore

hollow jewel
trim dragon
hollow jewel
trim dragon
hollow jewel
#

did you change the private setter yet

trim dragon
#

how can i change get; private set into public while i will only get red underline for this?

hollow jewel
#

You seem to be absolutely unwilling to change this

#

Why is it so important to you that the setter is private

#

do you even know what that means

trim dragon
hollow jewel
#

Instead of marking it as private, simply don't

trim dragon
hollow jewel
#

Again I ask, do you know what private set; means

#

private means "Cannot be accessed by other classes".
private set; means "This variable cannot be changed by other classes".

If you want another class to change this variable, why did you make the setter private. That's literally the entire point of the private access modifier

trim dragon
hollow jewel
#

That doesn't just happen. If that access modifier is there it's because you put it there. Why put it there if you didn't actually want it to be private

trim dragon
hollow jewel
# trim dragon

So, the first one doesn't need to use p2 and p2 should not exist any more. Delete it if you put it back.

#

The isMultiplier being a normal variable without getters and setters is fine. That would also resolve the problem. You can remove the [field: SerializeField] since it's no longer a property

trim dragon
hollow jewel
#

While you're on that line, ditch the Find. PlayerManager is a singleton and instance exists purely to avoid having to use find

#

PlayerManager.instance.isMultiplayer =

trim dragon
#

ok, there

trim dragon
hollow jewel
#

Okay. Now we can move on to making the PlayerManager much more useful. It looks like your player object has two scripts of your own creation, Player2D and HitPoints. Everything else is built-ins. Correct?

hollow jewel
#

The only actual scripts on the player are Player2D and HitPoints, right?

trim dragon
#

while the 5th image is checkpoint and 6th image is playermanager

hollow jewel
#

Just wanted to make sure I didn't miss anything.

So, it seems reasonable to make the Player2D script the "Primary" script. Whenever you want to reference a player object, that variable should be of type Player2D. You can use that script to get references to the others. The alternative would be creating a brand new script whose sole purpose is to just hold references to each of these components on the player. Either one would work and both have benefits. Do you have a particular attachment to either design?

trim dragon
#

uhhh..... idk

#

also, how do i make that type of script?

hollow jewel
#

Like, how would we make one that holds references?

#

It'd just be a component with a bunch of public variables

trim dragon
hollow jewel
#

We don't have to do that. Having that means your Player2D script would be simpler, but it means you have an extra .Something every time you want to use a reference

#

So, it's up to you. We can do either.

trim dragon
hollow jewel
#

We are nowhere near that yet

trim dragon
hollow jewel
#

We can get to the how later. For now, just pick one design. Smaller Player2D or simpler reference chains

trim dragon
hollow jewel
# trim dragon uhh.. what? one design? smaller player2D? simpler reference chains? I don't get ...

Okay, let me back up a bit.

We need to pick a component that is the single core representative of "Player". This script is the thing that you will be making variables to hold in order to reference a player. This can either be Player2D which you already have, and doing something like getting the player's animator would mean another script would be doing PlayerManager.instance.player1.animator.

The alternative would be having a script whose sole purpose is to just have public variables holding the components. This would mean that inside of your Player2D script, instead of having a variable animator and collider and whatnot, you'd have one field holding your new script (let's call it PlayerComponents) and you'd do this.playerComponents.animator instead of this.animator. This means your Player2D script no longer needs to have all those variables, making it shorter, but increasing the length of each line that tries to reference another component

#

This is purely your own choice. There is no "right" answer. Which one do you want to do

trim dragon
#

hmm... let's.... try the alterantive, i don't knwo what ot decide

hollow jewel
hollow jewel
# trim dragon

So, for every component on this object (other than Transform which all scripts have access to already) make a public variable of that type

#

public Rigidbody2D rb, etc.

trim dragon
#

like this?

hollow jewel
# trim dragon like this?

Yes. For every component type. And minor nitpick but public variables don't start with a _ in the C# style guide, but that's not actually a problem, just against the usual norms of C# code.

trim dragon
#

there you go

hollow jewel
#

Include Player2D and HitPoints as well

hollow jewel
# trim dragon

Okay cool. Now, this is going to serve as the main script through which you're going to reference a "Player". No matter which component you want from the player, you'd reference this PlayerComponents script to get it.

As an example, we're gonna make the PlayerManager better, add these two variables to it:

public PlayerComponents playerOne;
public PlayerComponents playerTwo;
#

Now, we can make a decision. Currently, you are destroying and respawning the players when they die which is breaking referencing. As suggested before, we can just teleport and reset the player whenever they die instead of spinning up a new one, or we can fix the referencing whenever a player respawns. Again, there is no "right" answer. Do you want to re-use the same player objects, or spawn new ones?

hollow jewel
# trim dragon

No, these two would be in PlayerManager. They'd reference this script

#

but you made this script so your PlayerManager can reference them

trim dragon
hollow jewel
trim dragon
#

uhh... about the player2d script. since player2D is for the player like move around, jump, climb, groundpund and stuff

#

also, i can't drag the play prefobso nto the player one and player two components

hollow jewel
hollow jewel
trim dragon
#

should the player prefab object have the player components script?

hollow jewel
#

so let's go add on a PlayerComponents script to them and setting all these references

trim dragon
#

there we go

hollow jewel
#

And then do the same for your Player2 prefab if it's different

hollow jewel
# trim dragon

Yep. Just like that. Once you have it on the Player 2 prefab (if there even is a player 2 prefab) let me know and we can move on

trim dragon
hollow jewel
# trim dragon

Okay, now, back to the PlayerManager. We'll have this script be responsible for holding the references to the prefabs as well. Note that these are different than the ones we already added:

public PlayerComponents playerOnePrefab;
public PlayerComponents playerTwoPrefab;
#

And then drag in your prefabs (not the instances in the scene! The actual prefab files from the assets window) into those two slots

trim dragon
#

okie

hollow jewel
# trim dragon

Keep the playerOne and playerTwo variables we made earlier. Those ones refer to the object that currently exists in the scene, that will be changed when the player respawns

trim dragon
#

hm, what?

hollow jewel
#

I said to add them to PlayerManager.

#

Since the PlayerManager is responsible for holding both the prefabs and the current instances, it'd make sense for this script to be responsible for spawning them.

public PlayerComponents SpawnPlayer(int playerNumber, Vector3 position, Transform parent = null){
  if (playerNumber == 1) {
    playerOne = Instantiate(playerOnePrefab, position, Quaternion.identity, parent);
    return playerOne;
  } else {
    playerTwo = Instantiate(playerTwoPrefab, position, Quaternion.identity, parent);
    return playerTwo;
  }
}
trim dragon
hollow jewel
#

playerOnePrefab is the template file that you copy into the scene to make a new player one
playerOne is the actual object in the world that can move around and do stuff

hollow jewel
# trim dragon

I updated the thing I posted. Gonna make some minor mistakes since I'm typing directly in Discord without autocomplete

trim dragon
#

oh

trim dragon
hollow jewel
#

Once that's taken care of, the next question: Do the players start in the scene already, or do you want to spawn them in after the game starts?

trim dragon
#

uhh... what's the difference?

hollow jewel
#

Well, one of them starts with the players already in the scene.
The other one spawns them in after the game starts.

#

Again, no wrong answers. You just have to decide on one.

trim dragon
#

players already in the scene

hollow jewel
# trim dragon players already in the scene

Okay, so the SpawnPlayer function will only be called when a player dies. This means you should drag in those objects already in the scene to the playerOne and playerTwo variables on your PlayerManager

trim dragon
hollow jewel
# trim dragon

Okay, and just to make sure, the two prefab variables are referencing the actual prefabs, right? Dragged in from somewhere in this window?

hollow jewel
# trim dragon yes

Okay good. Now, let's take a moment and look over what we've got, and how it might be used.

PlayerManager now has a canonical reference to the player one and player two objects at all times. Whenever you want to reference the player one object, you'd do PlayerManager.instance.playerOne

trim dragon
#

yet when after i playtested it and hwen i die and respawn, i'm still unable to move and the health bar doesn't go back to full

hollow jewel
#

If you ever want to respawn player one, you'd call PlayerManager.instance.SpawnPlayer(1, [ ], [null probably])

hollow jewel
hollow jewel
# trim dragon in the playermanager script?

The PlayerManager script is now what we call the "Source of truth". Other objects might need to reference the player, and rather than keeping their own reference around which might become "stale" when the player dies, they should always go through the PlayerManager. As a practical example, let's fix the health bar. Can you show the code for where you set that health bar?

trim dragon
hollow jewel
trim dragon
#

PlayerManager.instance.SpawnPlayer(1, [ ], [null probably])

hollow jewel
#

I haven't asked you to put anything anywhere yet

hollow jewel
#

You'd call that wherever you want to respawn a player

#

and just to be clear that's psuedocode

#

Just an example to show how you'd call it

trim dragon
hollow jewel
#

Then, inside of PlayerHealthUI, instead of using _player, use PlayerManager.instance.GetPlayer(playerNum).hitPoints

#

You following so far? You figuring out how this is gonna work?

trim dragon
#

but what about public HitPoints?

hollow jewel
#

Absolutely nothing but PlayerManager should have direct references to any component on the player objects.

#

This means that whenever a player respawns, only the player manager needs to be notified. Everything else will just work with no extra effort

hollow jewel
hollow jewel
#

And here. The operative word is using. You are using the variable inside of UpdateGraphics. Not when you declare the variable.

#

That should be gone

#

And instead of using _player, use what I said

trim dragon
#

playerNum instead?

hollow jewel
trim dragon
hollow jewel
#

Think this through

#

Do you know what PlayerManager.instance.GetPlayer(playerNum) does

trim dragon
hollow jewel
#

what

#

why

#

I've gone though so much trouble explaining all of this

#

why are you just

#

ignoring it all

#

Are you just waiting for me to write code and not taking any effort at all to read the words I'm saying to explain it all

trim dragon
# hollow jewel

i am doing what oyu said instead of using _player use PlayerManager.instance.GetPlayer(playerNum).hitPoints

hollow jewel
#

Why would checking if i is greater than your player number make any sense

hollow jewel
#

Why are you checking if the index of the image is greater than the player number

#

why would that make sense

#

Why are you just making changes without thinking about what any of this means

#

Do you know what this line does

#

Do you know what you are trying to check for

#

Going back to your own code, can you even tell me what this is doing

trim dragon
hollow jewel
# trim dragon

That's closer but why did you change the one that was already working into something that doesn't

#

Also you still aren't checking what the hit points are

#

Stop guessing

#

Take a moment

#

think about what you want to check

#

and then think "how do I get that value?"

trim dragon
hollow jewel
#

We're not touching code for a bit until you actually understand what you are doing

#

From your old code, what does this line do

#

What is this checking for

trim dragon
#

all you said to me is that instead of _player, use PlayerManager.instance.GetPlayer(playerNum).hitPoints. nothing more

hollow jewel
#

You did a whole bunch of other shit that didn't make any sense

#

So we're backing up

#

Because you've missed something very very fundamental

#

so we're going back to explain whatever it was that was missed

#

I need to know where you've missed something so please give your best answer to that question

trim dragon
hollow jewel
#

What is it checking

trim dragon
#

i don't know, it was a long time ago since last time i checked on the heaalth ui script

hollow jewel
#

what is it checking

trim dragon
#

i don't know....
_imglist is how much health you have you can choose

#

and check if there's.... no life on the health bar left?

hollow jewel
#

Do you know what an if statement is

trim dragon
#

will... a decision statement which... will execute if the condition evaluates to true?

hollow jewel
#

That leaves
i >= _player.hitPoints

#

What does that mean

trim dragon
#

..... i don't know, i don't even know anymore what it means. that... if the player takes hit.. the health heart get's broken image to show you lost a health?

hollow jewel
#

What does literally just this line mean

#

What is this checking

#

Code is not magic.

#

Every line means a thing

#

in plain english

#

What does this line mean

trim dragon
#

I DON'T KNOW! I don't know what it means anymore! i don't remember!😭

hollow jewel
#

I'm asking you to read

#

what do you not understand

#

Do you know what >= means

#

Is that it? Do you not know what that symbol means?

#

Is that why this is hard for you to explain?

trim dragon
hollow jewel
trim dragon
#

idk

hollow jewel
# trim dragon idk

Listen, and I mean this with the no disrespect. If you cannot combine these two thoughts into one, then you should not be making a game

hollow jewel
#

You just need to synthesize both of these into one statement

#

and if that's not something that you're capable of, then this is probably not a good hobby to try.

trim dragon
#

......greater than or euqal?

hollow jewel
#

correct

#

So, knowing that >= means "is greater than or equal to", we go back again:

if (i >= _player.hitPoints)

What does this sentence mean? What is it checking for?

trim dragon
#

so... i (0) >= (greather than or equal) _player.hitpoints (player's health of 5)?

hollow jewel
#

So, _player was previously something you dragged in, right?

#

and no longer exists

trim dragon
hollow jewel
#

Because we're trying to avoid having anything reference a player that you've dragged in. Since we're trying to make sure PlayerManager is the only object the player gets dragged to

#

This means that whenever PlayerManager changes the object it's using, then this object will use that new one, without us having to change this object as well

#

Does that make sense so far? You understand why we need to change this?

trim dragon
#

yeah..

hollow jewel
# trim dragon yeah..

Okay, so, since _player previously held the HitPoints component on the player and we dragged it in

trim dragon
#

yeah

#

previously called i >= _player.hitPoints

hollow jewel
#

How might you get that same component using our new PlayerManager system?

trim dragon
#

hmmm... i >= playerNum? since it already the the other thing called PlayerManager.instance.GetPlayer(playerNum).hitPoints somewhere?

hollow jewel
#

Player 1 or Player 2

#

Does i is greater than or equal to the player number mean the same thing as i is greater than or equal to the player's current hit points?

trim dragon
#

no

hollow jewel
# trim dragon no

So, we are trying to rewrite this line so that they both mean the same thing

#

It must end up meaning i is greater than or equal to the player's current hit points

#

in order to know that, we need to know what the player is

#

So, how do we get the player from PlayerManager?

#

If you don't know, let's start smaller: How would you get the current instance of PlayerManager?

trim dragon
#

get the... playercomponents from.. there? since it have both of the players, the current ones and the prefab ones

hollow jewel
#

And we added the GetPlayer function

#

that gets us the object for that player number

#

If you do GetPlayer(1) it gets the playerOne object

#

And GetPlayer(2) gets playerTwo

trim dragon
#

oh, so GetPlayer(playerNum) is either GetPlayer(1) or GetPlayer(2)?

hollow jewel
#

It'll call GetPlayer with whatever the current value of playerNum is

#

You'd set that in the inspector for the health bar. 1 for player 1s health bar. 2 for player 2s

trim dragon
#

so this is the first one

#

while i >= _player.hitpoints should bec alled something else instead of _player too

hollow jewel
#

So, this part is a bit confusing because your hit points script has a variable named hit points

#

You want to call .hitPoints on the HitPoints component of the current player

trim dragon
hollow jewel
hollow jewel
# trim dragon so this is the first one

Right here, you know how to get the HitPoints component of the current player.
You need to get the .hitPoints from that
Then check if i is greater than or equal to that

trim dragon
#

hmmm... instead of i >= _player.hitpoints.... i >= playerNum.hitpoints?

trim dragon
hollow jewel
hollow jewel
trim dragon
#

....no

#

i ran out of thoughts

hollow jewel
#

Let's break this into numbered steps.

  1. Get the HitPoints component of the current player.
  2. Get the .hitPoints from that
  3. Then check if i is greater than or equal to that

Let's answer these sequentially. What is the answer to question 1?

trim dragon
#

hmmm.... I don't know what's the answer... the hitpoints script?

hollow jewel
#

So we can access all the components on the player

hollow jewel
# trim dragon yeah

And remember how we added a function GetPlayer that gets a player for a given slot number?

trim dragon
#

yeah

hollow jewel
#

So, if GetPlayer gets the PlayerComponents instance, and that has a reference to the player's HitPoints component

#

How might you get the HitPoints component for the player with a given slot number?

trim dragon
hollow jewel
#

But you're skipping steps

#
  1. How do you get the current PlayerManager instance?
  2. How do you get the PlayerComponents for a player of a given number?
  3. How do you get the HitPoints component from a PlayerComponents?
  4. How do you get the hitPoints value from a HitPoints component?
#

Literally just answer these one at a time

#

Don't try to write the whole line

#

Start with 1. What is the answer to that one?

trim dragon
#

.... i don't know

delicate ginkgo
#

||Instance||

hollow jewel
#

So look back at the code I've had you write. What looks like it might be a way to get the instance from PlayerManager?

trim dragon
#

...get the playermanager script first? PlayerManager.instance?

hollow jewel
#

Now, question 2:
How do you get the PlayerComponents for a given player number from a PlayerManager?

delicate ginkgo
trim dragon
hollow jewel
trim dragon
#

hmm... no.. except for the four public to hold onto and.. the two lines with SpawnPlayer and GetPlayer

#

PlayerManager.instance.SpawnPlayer?

hollow jewel
#

Why is your thought going to "spawn" player

#

You're just guessing again

#

please, think it through

#

How many times have I said that GetPlayer gets a PlayerComponents for a specific playerNum

#

You just aren't reading the things I'm saying

#

you're randomly guessing wrong things until I get frustrated enough to write the answer

#

I've said this many many times

trim dragon
#

PlayerManager.instance.GetPlayer?

#

(I guess i am just a dumb person because of having a low iq mind...)

hollow jewel
#

Don't choose to be stupid.

#

If you say "I cannot understand this because I am dumb" then you're right

#

You have to actually try. The only way you end up stupid is if you decide to be

trim dragon
delicate ginkgo
#

Or impromptu (on the spot response), new to coding, not interested in learning to code etc
Investing in learning a bit can save you a lot of time and headache.

trim dragon
hollow jewel
#

They have all been failures in logical thinking and information synthesis

#

this is not something you can learn from a tutorial

delicate ginkgo
#

Pretty normal, as my old professor once said "you don't use it, you lose it". I prefer, "you don't use it, you'll not be so good at it".

hollow jewel
#

You are going to need to put conscious effort into breaking things down into systems of rules and recombining them into single ideas

#

Start with a collection of things known to be true. Make conclusions from them

trim dragon
trim dragon
delicate ginkgo
#

It's not wrong but incomplete

hollow jewel
#

GetPlayer is a function that gets a player with a given number

#

You need to tell it if you want player 1 or 2

trim dragon
#

PlayerManager.instance.GetPlayer(playerNum).hitPoints?

hollow jewel
#

Yes and yes. That's questions 2 and 3

#

Now, question 4

trim dragon
#

that one... is tricky, since there's no hitpoints value name in PlayerManager

hollow jewel
#

you just got the HitPoints component

delicate ginkgo
#

What is hit points? The type.

#

Consider evaluating it's members

hollow jewel
#
  1. How do you get the hitPoints value from a HitPoints component?
trim dragon
hollow jewel
delicate ginkgo
#

So it's not an int? Referring to the hit points member of the player component

hollow jewel
#

This is confusing because HitPoints has a hitPoints

trim dragon
hollow jewel
#

That'd make things easier

#

Remember the refactor thing?

#

Change that int variable's name to health

trim dragon
#

public int health

hollow jewel
hollow jewel
delicate ginkgo
#

So the refactored question would be

How do you get the health value from the HitPoints component

#

Where I'm assuming the player component had a reference to the HitPoint component, which you've gotten now but have yet to get the health value.

trim dragon
hollow jewel
#

questions 1-4

#

how would you get the health value of the current player

trim dragon
hollow jewel
#

Yes

#

Congratulations

#

Now, we go back to the original question we were trying to fix

#

How would you now write
if i is greater than or equal to the health of the current player

delicate ginkgo
trim dragon
trim dragon
hollow jewel
#

Now, you have finally learned what I meant when I said to replace _player with PlayerManager.instance.GetPlayer(playerNum).hitPoints

trim dragon
#

yeah

hollow jewel
#

Hopefully you've learned how all this worked because your homework is now to replace every single dragged in reference to a player with the one from PlayerManager in any of your scripts

#

If you need a refresher, just read through this thread again

#

I'm gonna go be somewhere else for a while

trim dragon
#

ok, while one reminder is that the checkpoint script have some erros after that

delicate ginkgo
#

Which are?

trim dragon
delicate ginkgo
#

So what do you think you need to do?

trim dragon
delicate ginkgo
trim dragon
#

replace _player with PlayerManager.instance.GetPlayer(playerNum)?

delicate ginkgo
#

I'm not sure what the stuff before the variable access is but you likely do not need any of it.

trim dragon
delicate ginkgo
#

You've got access to the player manager instance which has access to every player.

delicate ginkgo
#

Maybe you may need to create a set player method if anything

trim dragon
#

an on top of that, the enemy doesn't die

delicate ginkgo
trim dragon
delicate ginkgo
trim dragon
delicate ginkgo
trim dragon
#

what?

delicate ginkgo
#

left-hand-side = right-hand-side

delicate ginkgo
# trim dragon

You'd want to assign the hitPoints variable the new instantiated game object's component and not the health value.

trim dragon
delicate ginkgo
delicate ginkgo
delicate ginkgo
#

I'm not on the correct platform to be writing code. I can only direct you in a lazy fashion towards what needs to be changed. You're almost done with that statement.

delicate ginkgo
trim dragon
delicate ginkgo
#

I did not mention health UnityChanHuh

delicate ginkgo
#

So read the first part of the sentence

#

Don't skim it

trim dragon
#

I don't know what I'm doing,

delicate ginkgo
#

It's okay.

#

You'll pull through slowly if you just take things step by step

trim dragon
#

I don't know how to fix that line

delicate ginkgo
#

You'd want to assign the hitPoints variable the new instantiated game object's component

trim dragon
#

yeah, but idk how witht he new current code that's in there

#

and i don't know what else to write there

delicate ginkgo
#

Just focus on line 53

trim dragon
#

I. Don't. Know. How. Right. Now

delicate ginkgo
#

You've got the player now. When you type the dot after the Get Player method, what's available?

trim dragon
delicate ginkgo
trim dragon
delicate ginkgo
#

So you need to replace playerNum with something

trim dragon
delicate ginkgo
#

I'm guessing this is player 2 relative to the remainder of the code

#

I'm pretty certain it wants an Integer

trim dragon
trim dragon
delicate ginkgo
#

What does the GetPlayer method expect?

delicate ginkgo
trim dragon
delicate ginkgo
#

It takes a number

#

An integer

trim dragon
#

I am so stuck right now

trim dragon
delicate ginkgo
#

Well, Player1 is a bool.. and it doesn't seem to want a bool.
Maybe you ought to give it an integer instead.

#

These are integers: 0, 1, 2, 3 ...

trim dragon
#

aghhh. I am so stuck right now

delicate ginkgo
#

Try 1

trim dragon
delicate ginkgo
#

You'd write it where you're supposed to write - an argument to be passed to the Get Player method

#

Where the Get Player method is expecting an integer

delicate ginkgo
#

I'm assuming everything is working now? 😂

trim dragon
#

hang on

trim dragon
#

the first one is from my previous scrip,t and the second image is from the newe one

hollow jewel
#

The fact that the field there is missing is a feature, not a bug

trim dragon
hollow jewel
trim dragon
#

public int playerNum;?

trim dragon
# hollow jewel Please reread the sentence

the health images are working, but however. there problem is still the same that when i die and respawn, I am sitll unable to move and the health bar is not back to full

hollow jewel
trim dragon
hollow jewel
trim dragon
hollow jewel
trim dragon
hollow jewel
trim dragon
hollow jewel
trim dragon
hollow jewel
#

I've spent the better part of seven hours trying to get you to put two and two together and every single step along the way you've just taken random code snippets and either thrown them blindly into your own code or just posted random bullshit until I correct you out of exasperation. You've fought me every step along the way and have put in zero effort to solve your own problems. I'm done.

#

You have all the pieces here. If you need me to repeat it, scroll up and re read it instead of insisting I type it again

trim dragon
hollow jewel
#

Please think things through

#

Why would that make any sense

trim dragon
trim dragon
delicate ginkgo
delicate ginkgo
#

Can you show us where? Just to be certain.

trim dragon
delicate ginkgo
#

Where do you call SpawnNewPlayer?

trim dragon
delicate ginkgo
#

Well, that's a big problem.

#

If you're not calling the method, you're likely just having the program do what you had before writing this method - likely teleporting and becoming stuck.

#

I see your spawn point system above but somewhere in code (likely on player death) you're teleporting the player. This would be where you'd want to call the SpawnNewPlayer - assuming you aren't pooling but need to respawn-a-player/spawn-player-2 etc

trim dragon
delicate ginkgo
#

So you've set is dead to true which implies it's being polled (used) elsewhere.

#

Where do you evaluate is dead and teleport the player(s)?

delicate ginkgo
trim dragon
delicate ginkgo
#

You'd search through your scripts for code that uses the is dead variable.

trim dragon
delicate ginkgo
#

How are you teleporting?

delicate ginkgo
trim dragon
delicate ginkgo
trim dragon
delicate ginkgo
trim dragon
delicate ginkgo
#

I had assumed it were new code and that you had something else prior unrelated to this method

trim dragon
delicate ginkgo
#

Alright, so the method is being called.

delicate ginkgo
trim dragon
delicate ginkgo
#

So it's present, just not refilled?

trim dragon
#

yeah

delicate ginkgo
#

Can you show the updated code for heat refill?

trim dragon
#

also the red underline

delicate ginkgo
#

Using a paste site please !code

nimble sapphireBOT
trim dragon
delicate ginkgo
#

Methods usually (always) have parenthesis - ()

#

SpawnPlayer(...)

trim dragon
delicate ginkgo
#

You probably would. Can't expect immediate relief.

#

And what does the red underline state? (Likely invalid, incorrect number of arguments, etc)

delicate ginkgo
#

It says you're missing some arguments

#

At the time of the error, it said you did not provide a position argument.

trim dragon
#

how do i fix it?

delicate ginkgo
#

Provide a position argument

#

Maybe the position where you'd want the player to spawn

trim dragon
#

hmm... what do i write then?

delicate ginkgo
#

What have you written thus far?

trim dragon
#

GameObject go = PlayerManager.instance.SpawnPlayer();

delicate ginkgo
#

I'll give you an example (only an EXAMPLE)cs PlayerComponent go = SpawnPlayer(1, lastCheckPoint);

trim dragon
#

huh.. 1 for player 1?

delicate ginkgo
# trim dragon

Intellisense tells you what each argument is for - relative to naming convention (player number)

trim dragon
delicate ginkgo
#

Typo, missing an s

trim dragon
#

oh

delicate ginkgo
#

I'm typing on mobile-discord, don't expect valid working code without errors

trim dragon
#

ok

#

as long imma show of

delicate ginkgo
#

So, I'm sure you know what goes there but how you would get it would be questionable.

#

Which class has this variable?

#

I only used the variable name relative to some images above (the check point code)

trim dragon
delicate ginkgo
#

Do we happen to have a reference of player2d?

#

Maybe say, from Player?

#

Where hopefully the player is accessible from the manager?

trim dragon
#

it only have public checkpoint lastCheckpoint;

delicate ginkgo
trim dragon
delicate ginkgo
#

Does anything have a reference to the player2d component?

trim dragon
#

what?

delicate ginkgo
#

I think I saw digi tell you to drag and make a whole lot of references in the inspector somewhere above

#

Where you had referenced like.. every component on the Player object

#

Does this include the player 2d component?

delicate ginkgo
trim dragon
delicate ginkgo
#

So, just access the previous player, it's player 2d component and acquire the position of the last check point - assuming the player hasn't been destroyed yet.

delicate ginkgo
delicate ginkgo
#
  • access previous player
  • access player 2d component
  • access check point
  • access position of check point
#

Non code (pseudo code): GetPlayer(num).player2d.checkpoint.position

#

Where num would be whichever player is being spawned

trim dragon
#

where do i put that?

delicate ginkgo
#

It's not valid code

#

You'd put it where you had needed a position though

trim dragon
delicate ginkgo
trim dragon
#

you said i'm supposed to write in the checkpint script to access these 4

delicate ginkgo
# trim dragon

Your last check point is a reference to some component on an object in the scene. Every object has a position. I'm assuming that's why you've referenced this prior.

trim dragon
#

ok now i don't get what i'm doing now

delicate ginkgo
#

You aren't supposed to be creating new variables

#

You need to access the previous player, it's player 2d component, it's check point and the position of the check point.

trim dragon
#

then i don't know what I'm supposed to write

delicate ginkgo
#

Use your manager to acquire the previous player.

trim dragon
#

I don't get what you're saying now

trim dragon
delicate ginkgo
trim dragon
#

I know that

delicate ginkgo
#

How would you get the player object?

#

Well get it..

#

Then access it's player 2d component.

#

Then the last check point variable.

#

And finally the position of that object

trim dragon
#

there is already Player2D component in places

delicate ginkgo
#

It's going to ultimately look something like(not valid code)cs PlayerComponents go = PlayerManager.instance.SpawnPlayer(1, PlayerManager.instance.GetPlayer(1).player2d.lastCheckPoint.transform.position);

#

I'm on mobile discord and using swype. I cannot type the code for you.

trim dragon
delicate ginkgo
#

I'm not going to type everything Edited. Some stuff are implied.

delicate ginkgo
# trim dragon

You've been using GetPlayer for the last few hours now. It's implied above that you should be aware of what I'm suggesting.
#1237461875633950770 message
It's not anything new that you haven't done recently.

trim dragon
#

oh

trim dragon
delicate ginkgo
trim dragon
#

yeah

delicate ginkgo
#

I recall Digi asking you to reference all components on the player object

#

There should be a player 2d variable (possibly under a different name)

delicate ginkgo
#

If not, worst case scenario you can call GetComponent<Player2d>() in place of the player2d part.. but I'm pretty sure you had referenced all of your components.

delicate ginkgo
#

Relative to your case

delicate ginkgo
#

Did you resolve your issue?

trim dragon
delicate ginkgo
#

What's the current issue?

trim dragon
delicate ginkgo
delicate ginkgo
#

You'd used that variable name you had in code. The inspector is just for legibility (always capitalize first characters and space, which is objectively invalid)

delicate ginkgo
trim dragon
delicate ginkgo
#

And you'd get that with the first image - you're missing the 1, btw

trim dragon
#

yeah

trim dragon
delicate ginkgo
#

I'm not sure what you're talking about but the spawn player method wants to know which player is being spawned so you'd give it the player number as the first argument. The second argument requests for a position. You pass it the last check point's position using the manager instance to access the previous player object's player component (a component) to access the player 2d component to access the last check point reference (an object in the scene that was last reference) where yours acquire the position from it's transform property.

#

I've got to run, take care and good luck.

trim dragon
trim dragon
#

PlayerComponents go = PlayerManager.instance.SpawnPlayer(1, PlayerManager.instance.GetPlayer(1).player2D.lastCheckpoint.transform.position);
and it sent me over to this line of code