Discuss Scratch

1kbrother
Scratcher
44 posts

I need a simple way to detect variable change!

I am making a script that uses a variable as the pen size
setpensizetoY1
and I want it to clear every time the variable/pen size changes. I don't want to have to make anymore variables, and I can't find a way to do it without making another variable.

Thanks for your help!

Th3_C0d3r
Scratcher
100+ posts

I need a simple way to detect variable change!

Unfortunately your going to need 1 more variable with my script…
So here it is

whenclickedsetY1to3This is your main var.setY1 LogtoY1This checks if it changedforeverifnotY1=Y1LogthenclearsetY1 LogtoY1So we can check for further changesIf it changed, then

Fairly simple script. Hope you find it useful.

-Scratch On!
1kbrother
Scratcher
44 posts

I need a simple way to detect variable change!

Th3_C0d3r wrote:

Unfortunately your going to need 1 more variable with my script…
So here it is

whenclickedsetY1to3This is your main var.setY1 LogtoY1This checks if it changedforeverifnotY1=Y1LogthenclearsetY1 LogtoY1So we can check for further changesIf it changed, then

Fairly simple script. Hope you find it useful.

-Scratch On!

I came up with a fairly similar script but I need a way to have it use only one variable, but thanks anyway!
Th3_C0d3r
Scratcher
100+ posts

I need a simple way to detect variable change!

1kbrother wrote:

I came up with a fairly similar script but I need a way to have it use only one variable, but thanks anyway!
Could you please explain why you can't make another variable? Because the only way to detect a change is to compare it to something else. If you can't use a variable, perhaps you can use a List.

-Scratch On!
1kbrother
Scratcher
44 posts

I need a simple way to detect variable change!

Th3_C0d3r wrote:

1kbrother wrote:

I came up with a fairly similar script but I need a way to have it use only one variable, but thanks anyway!
Could you please explain why you can't make another variable? Because the only way to detect a change is to compare it to something else. If you can't use a variable, perhaps you can use a List.

-Scratch On!

I want to make my program as compact as possible and I want to try to limit the variables as much as possible.
asivi
Scratcher
1000+ posts

I need a simple way to detect variable change!

You don't need another variable acting as a monitor.
Where are you changing such variable? put your block for clear there.
TheLogFather
Scratcher
1000+ posts

I need a simple way to detect variable change!

asivi wrote:

You don't need another variable acting as a monitor.
Where are you changing such variable? put your block for clear there.
Yes – though it did occur to me that maybe 1kbrother has the Y1 variable showing as a slider that the user can change (and changing that changes the pen size). In that case the simplest solution really is to use another variable in a polling loop, as Th3_C0d3r said.

If you're not using any sounds in your project then, it's a bit weird, but you *could* use the “volume” for the comparison (maybe compare and set the volume set to a quarter of Y1, so you can get up to pen size of 255, if necessary).

Otherwise, if you *really* want to avoid seeing that extra variable appear in the Data palette, then you can use a trick with a clone which creates and uses the temporary variable when the clone gets created (but you delete the var from the original sprite so that it doesn't appear there).

Last edited by TheLogFather (Feb. 4, 2017 00:48:56)

asivi
Scratcher
1000+ posts

I need a simple way to detect variable change!

If as @TheLogFather said you are changing the pen size with a variable slider and your pen is not visible you can use the direction reporter to detect it.

Last edited by asivi (Feb. 4, 2017 01:37:36)

Howerd1
Scratcher
5 posts

I need a simple way to detect variable change!

Just store the previous value!

whenclickedforeverifvariable=previousthenbroadcastchangedsetprevioustovariable
duckboycool
Scratcher
1000+ posts

I need a simple way to detect variable change!

Adding one measly variable to the project will barely change your space at all. The script you would make would be larger itself, so with whatever you need it to be compact for, this will work fine.

(I mean Th3_c0d3r's one BTW)

Last edited by duckboycool (Feb. 4, 2017 05:24:19)

1kbrother
Scratcher
44 posts

I need a simple way to detect variable change!

duckboycool wrote:

Adding one measly variable to the project will barely change your space at all. The script you would make would be larger itself, so with whatever you need it to be compact for, this will work fine.

(I mean Th3_c0d3r's one BTW)

The reason I only want one variable is because I have to repeat this script all over my program, and I don't want to have to create an extra variable for no reason.

Anyways I found a way so thanks anyway.
asivi
Scratcher
1000+ posts

I need a simple way to detect variable change!

If you are going to have a large bunch of variables a list could be useful.
MasterDiamondMan
Scratcher
13 posts

I need a simple way to detect variable change!

I just can't find a way…
Maybe this?

I haven't checked…

whenclickedscratchblocksforeverscratchblocksifthen/scratchblocks

Last edited by MasterDiamondMan (March 7, 2020 16:13:52)

Maarten_
Scratcher
100+ posts

I need a simple way to detect variable change!

Th3_C0d3r wrote:

Unfortunately your going to need 1 more variable with my script…
So here it is

whenclickedsetY1to3This is your main var.setY1 LogtoY1This checks if it changedforeverifnotY1=Y1LogthenclearsetY1 LogtoY1So we can check for further changesIf it changed, then

Fairly simple script. Hope you find it useful.

-Scratch On!


IMHO using if blocks inside a repeat to *wait* for a change is dumb.
Not that it should not be used, but it's inefficient.

Please take a moment to consider using this way instead:

whenclickedsetY1to5 Your variablesetY1 LogtoY1 Check if changedforeverwaituntilnotY1=Y1Log Wait until the variable changessetY1 LogtoY1 Updateclear Clear the screen
LuvGoats_5
Scratcher
13 posts

I need a simple way to detect variable change!

I'm not sure if this is necroposting, but I found a simple way to detect a variable change that is a little easier to understand.

whenclickedforeverwait0.000000000000000000001secssetCopy of YOUR VARIABLEtoYOUR VARIABLEifnotCopyofYOURVARIABLE=YOUR VARIABLEthenbroadcastVariable Change
LuvGoats_5
Scratcher
13 posts

I need a simple way to detect variable change!

Sorry, the YOUR VARIABLE parts were supposed to be the variables!!
deck26
Scratcher
1000+ posts

I need a simple way to detect variable change!

LuvGoats_5 wrote:

I'm not sure if this is necroposting, but I found a simple way to detect a variable change that is a little easier to understand.

whenclickedforeverwait0.000000000000000000001secssetCopy of YOUR VARIABLEtoYOUR VARIABLEifnotCopyofYOURVARIABLE=YOUR VARIABLEthenbroadcastVariable Change
That is no different - you're saving the value so you can compare the variable to the value it had. BUT you're setting the copy of the variable immediately before you check it so it is highly unlikely to have changed before the if block.

Not worth necroposting for!
cavemanster
Scratcher
100+ posts

I need a simple way to detect variable change!

repeatuntilpen size=Y1DostuffsetY1topen size

Last edited by cavemanster (Aug. 22, 2020 15:36:43)

aarizkamran
Scratcher
100+ posts

I need a simple way to detect variable change!

what is your default value
LuvGoats_5
Scratcher
13 posts

I need a simple way to detect variable change!

I'm sorry, I tested it. Waiting only 0.000000001 seconds makes no difference. If you made it longer, as in 1 second, it would have time to broadcast the following more than once. Broadcasting is almost instant, but takes some time. By making it 0.00000000001 seconds, it only gives the variable time to broadcast once. It works, do not tell me I'm wrong about this please. It makes sense, and any way you want to change it, go ahead.

Powered by DjangoBB