#Pause

1 messages · Page 1 of 1 (latest)

warped pumice
#

there, a thread

rough ice
#

ok

#

why is this there

#

public static Pause pause

#

also don't name your classes lowercase

warped pumice
#

ah nevermind that, it's nothing

rough ice
#

classes should always be PascalCase

#

capitalize the first letter always

#

anyway ok You added the event here. You just have to Invoke it when you pause with the appropriate value

#

also you should probably store the state inside a bool

warped pumice
rough ice
#

link isn't going anywhere

warped pumice
#

what?

warped pumice
#

is this an example of invoke?

rough ice
#

I just told you the Link isn't working

#

what dont you get lol

#

first store the bool of isPaused

crude ginkgo
rough ice
warped pumice
rough ice
#

that would be a boolean yes

warped pumice
warped pumice
rough ice
#

ok do you know how to toggle it true/false?

#

show your Pause method

rough ice
#

ok good

#

so do you know where to switch this bool to true and false

warped pumice
#

like isPaused = true;?

rough ice
#

now you have to Invoke that value on the event

#

any time it changes to a new value

warped pumice
#

like Invoke("PauseGame")?

rough ice
#

Monobehavior.Invoke
and event.Invoke are completely different

#

You have to Invoke the value on the event

#

do you see OnGamePaused passes a bool value around

warped pumice
#

yeah

rough ice
#

So invoke this event with the bool value, when the value changes

warped pumice
#

OnGamePaused?.Invoke(paused);?

rough ice
warped pumice
#

I mean isPaused

rough ice
#

ok so put that

#

given wat I said earlier, where do you plan on putting this

warped pumice
#

hmmmmm.... in.... PauseGame?

rough ice
#

is PauseGame the only time isPaused changes ?

#

You dont want to track it when its not paused?

warped pumice
#

PauseGame and ResumeGame changes because PauseGame have true and ResumeGame and false

rough ice
#

right so based on that, I ask again. Where do you think should put the invoke you showed

warped pumice
#

in PauseGame and in ResumeGame?

rough ice
#

not so hard is it ?

warped pumice
#

ok, now let me test it

rough ice
#

wait

#

part 1 is done

#

you still didn't do the second part

#

You are "Broadcasting" this Event

#

but is anyone that cares listening ?

warped pumice
#

huh? second part?

rough ice
#

yes.

#

You only announced the event

#

but something needs to "listen" to this event

#

dont you want to do something with isPaused ?

#

in another script

warped pumice
#

back to Player2D script?

rough ice
#

that would be one of them interested in that bool, sure

warped pumice
#

ok, I am back in the Player2D script

rough ice
#

I would also suggest to clean it up in the OnDestroy or OnDisable method

#

with the -=

warped pumice
#

moving -= !paused; or pause.OnGamePaused -= HandlePause;?

rough ice
#

so basically

#

in awake Subscribe +=

#

and in OnDestroy -= UnSubscribe

#

or you can do OnEnable +=
and OnDisable -=

#

thats what I typically do

rough ice
#

think of it like Youtube or Twitch or anything that has Subscribers

#

+= means you are SUBSCRIBING to the events
-= means you are NOT-SUBSCRIBING to the events anymore

#

its good practice to cleanup subscribing especially when object gets destroyed, we don't keep subscribed to something that is destroyed

warped pumice
#

so like... should i put pause.OnGamePaused -= HandlePause; in..... ? since pause.OnGamePaused += HandlePause; is in void awake

rough ice
#

read carefully the first one

warped pumice
rough ice
#

also what is moving do

warped pumice
rough ice
#

that doesnt explain what i asked

rough ice
warped pumice
#

in player2D script

rough ice
#

com on

#

simple logic

#

you are changing moving bool. What does moving bool do.

#

again

warped pumice
warped pumice
rough ice
#

but where?

#

its literally not doing anything

#

just receiving the value

warped pumice
#

idk, I got the example from fren

rough ice
#

its not affecting any other code at all

#

You need to start applying the examples to make sense

#

thats why they are examples

#

obviously its not doing anything in your code how could it possibly affect it?

#

you should know this by now..

#

OK make this very simple

#

is Player2D supposed to do ANYTHING while paused

warped pumice
#

no

rough ice
#

Ok so disable the Entire script

#

instead

#

do you know how to do that?

warped pumice
#

GetComponent<Player2D>().enabled = false in Pause Game and GetComponent<Player2D>().enabled = true in ResumeGame?

rough ice
#

do you have another scripts that has bother player2d scripts referenced?

warped pumice
#

no?

#

I mean

#

the scripts the player have is muon interaction, scroll interaction, hitpoints, player components, player projectile

rough ice
#

anyway thats fine

#

since you know how to disable it just use that instead

#

all MonoB scripts have the enabled

#

so link the enabled to the value from event

#

inside HandlePause

warped pumice
#

all the scripts?

rough ice
#

how many do you want disabled?

#

start with the player2D

warped pumice
rough ice
#

also why you hardcoded false 🤔

#

which script is this?

warped pumice
#

Player2D

rough ice
#

HandlePause is inside Player2D ?

warped pumice
#

yes

rough ice
#

then you don't need GetComponent, since you already are In the component you need

crude ginkgo
#

The this keyword is for self referencing. this.enabled

rough ice
#

its implied you don't really need it

crude ginkgo
#

And yeah, instead of false, use the bool you are passing in

crude ginkgo
rough ice
#

if you had another variable named enabled in a method parameter then yea

#

i use this often when my method parameter has the same name as my fields

warped pumice
#

idk wha tto write in HandlePause

rough ice
#

adhd moment

#

try a little more so I don't have to spoon feed you every line

#

Handle Pause is giving you the Value of the Paused event

#

enabled property determines if script is enabled or not

#

its a 2+2 moment

warped pumice
#

but even if the Player2D is disable, this script line that is inside Player2D script would also be disable, won't it?

rough ice
#

no because it still subscribed to the event

#

enabled only disables Unity specific events

#

like Update

#

Only Disabling a GAMEOBJECT would prevent any lines from running

warped pumice
rough ice
#

wtf is going on here

#

you literally overcomplicated the shit out of this

#

enabled needs to be set with a value.. do you not see its red?

#

think for a moment how the flow goes

#

HandlePause gets paused from the event OnGamePaused

#

what do you think you should do with paused

crude ginkgo
#

As nav said, remove the entire line at 101. You are IN that component, no need to get it.
Then the other line of course needs.... an =, right?
You can's just tell the compiler "enabled"
You want to tell it "set enabled to [true/false]"

#

Ah, that's what I get for jumping in and out. Yeah, the getcomponent is for the other script

rough ice
#

Its not like they dont know how to assign values thats whats most notlikethis

#

@warped pumice Did you get it working?

warped pumice
#

wait

rough ice
#

About to run out

warped pumice
#

huh?

rough ice
#

I have to run to work 😛

rough ice
#

what do you think goes on the Red line

#

its an easy test

#

based on everything me and Aethenosity wrote

warped pumice
rough ice
#

......why did it take this long to write this

warped pumice
#

the player is not moving when pausing, so it's working

rough ice
#

hopefully you understood 10% of whats going on

warped pumice
#

yeah, man I have autism and ADHD that is a big disadvantage for me to understand all coding, especially i always forget

rough ice
#

Yeah I mean, have adhd as well so I can understand, but really repetition is key, you haven't practiced what you learned enough you're gonna forget if you don't instill it

#

doing something once or twice isnt enough

#

I literally had to do it for years before it really becomes muscle memory

#

also have big memory problems from injury so believe me, If I can do it so can you lol

#

just practice what you learned, if you learned about event. Do it in other projects or use it more, keep practicing until it make sense

#

The MAJOR benefit of the Event , also called the Observer pattern. Is your Pause Script doesn't care about Player scripts or anything, it just says "I was paused/unpaused"

#

other scripts that listen to that Event react in their own way, in your case Player2D script disables itself etc.

#

it can be used for other use cases, you don't have to put a dozen references in the Pause script when it doesn't need to

#
Unity Learn

By implementing common game programming design patterns in your Unity project, you can efficiently build and maintain a clean, organized, and readable codebase. Design patterns not only reduce refactoring and the time spent testing, but they also speed up onboarding and development processes, contributing to a solid foundation that can be used t...

#

Very important to learn these concepts ^^