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
set pen size to (Y1)
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

when green flag clicked
set [Y1 v] to [3]//This is your main var.
set [Y1 Log v] to (Y1)//This checks if it changed
forever
if <not <(Y1) = (Y1 Log)>> then//If it changed, then
clear
set [Y1 Log v] to (Y1)//So we can check for further changes
end
end

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

when green flag clicked
set [Y1 v] to [3]//This is your main var.
set [Y1 Log v] to (Y1)//This checks if it changed
forever
if <not <(Y1) = (Y1 Log)>> then//If it changed, then
clear
set [Y1 Log v] to (Y1)//So we can check for further changes
end
end

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!

when green flag clicked
forever
if <(variable) = (previous)> then
broadcast [changed]
set [previous] to [variable]
end
end
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…

when green flag clicked
[scratchblocks]
forever
end
[scratchblocks]
if <> then
end
[/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

when green flag clicked
set [Y1 v] to [3]//This is your main var.
set [Y1 Log v] to (Y1)//This checks if it changed
forever
if <not <(Y1) = (Y1 Log)>> then//If it changed, then
clear
set [Y1 Log v] to (Y1)//So we can check for further changes
end
end

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:

When green flag clicked
set [Y1 v] to [5] // Your variable
set [Y1 Log v] to (Y1) // Check if changed
forever
wait until <not<(Y1) = (Y1 Log)>> // Wait until the variable changes
set [Y1 Log v] to (Y1) // Update
clear // Clear the screen
end
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.

when green flag clicked
forever
wait (0.000000000000000000001) secs
set [Copy of YOUR VARIABLE v] to [YOUR VARIABLE]
if <not <(☁ Copy of YOUR VARIABLE) = [YOUR VARIABLE]>> then
broadcast [Variable Change v]
end
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.

when green flag clicked
forever
wait (0.000000000000000000001) secs
set [Copy of YOUR VARIABLE v] to [YOUR VARIABLE]
if <not <(☁ Copy of YOUR VARIABLE) = [YOUR VARIABLE]>> then
broadcast [Variable Change v]
end
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!

repeat until <<[pen size] = (Y1)>>
Do stuff
set [Y1 v] to [pen size]
end

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

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.
LuvGoats_5
Scratcher
13 posts

I need a simple way to detect variable change!

The ‘if’ block is always, it doesn't matter if it is before or after the variable change. I used this method in my latest project, it worked. Don't doubt me, when I post, I do it right.

Powered by DjangoBB