"Change var by num" that reports itself

I'm trying to make a "change [var] by [num]" just like the built-in one, except it reports the final value.

If I make var a special variables list, then use set, I'm just reassigning the var reference rather than the reassigning the actual var's value. If I make var an upvar, that doesn't seem to be what I want at all.

Can anyone help me? Thank you!

[scratchblocks] (change (variable) by (number) :: variables) :: control hat script variables (a) @delInput @addInput:: grey set [a v] to (primitive [var_get(name) v] (variable) @delInput @addInput:: grey) change [a v] by (number) report (a) :: control cap [/scratchblocks]

now the block in action:

[scratchblocks] script variables (ff) @delInput @addInput:: grey set [ff v] to (10) report (change [ff v] by (10) :: variables) :: control cap[/scratchblocks] would report 20

untitled script pic(64)

untitled script pic(65)
untitled script pic(66)
untitled script pic(67)
untitled script pic(68)

like this? you can change the one to whatever

Thank you both. This makes sense to me, but I don't see primitive anywhere. Where do I find that? Thank you!

image
you need to enable it in settings, it will then be at the bottom of the variables category
image

Here's how I did it (without the use of the primitive blocks)

The only downside to using this is, it only works for global and local variables.

If you need this to work with script vars, then just do this
untitled script pic (56)


although, I prefer to do this instead

untitled script pic (60)

as that is the only real way to do it without extension blocks, and being able to work with all variables.

But actually, the best way might just be this

You do need the input variable to be in a ring, otherwise snap will throw an error saying the variable doesn't exist in this context.

heres another

untitled script pic(69)
untitled script pic(70)
untitled script pic(71)
untitled script pic(72)
untitled script pic(73)

since i like your question so much, fun challenge

This is the most officially approved version -- it's more or less why Jens invented block variables -- but it has a particular variable built in. The original question was how to make a block that takes the variable as input. So I think @ego-lay_atman-bay's solution is best.

This solution, using the Create Variables library, is best, I think. It's a good example of why Jens shouldn't hate that library. :~)

But why is it defined by SET rather than by Make-a-block?

I didn't use the create variables library in that solution. Maybe you quoted the wrong one?

I just didn't think of doing that.

edit: well, here's the version that is defined by using a custom block

Oops,. no, I quoted the right one but was looking at the other one when writing the comment. :blush: Even better, just using the regular Snap! capabilities.

That was my goal, since I knew it was possible. (And since I already figured out the amazing capabilities of ringed variables)

Would it be possible to do it with an unevaluated input instead? Or is it already one

image

The lambda symbol means it's an un-evaluated input

Oh I see now. Thanks.
EDIT: Tested it and it doesn’t work as an unevaluated input. Meaning it’s a reporter.

That is a really cool method of doing that.

I wonder if this would compile in turbosnap. Probably not.

I used a ring input, but it does work with unevaluated input. I just think using a ring makes more sense, since you can't type in text.

Thanks to everybody who gave me answers. This was the one I used. That was very nice of you. I like your user name. :smile: . I like the Lego Batman movie too.

(This may be a coincidence but I was working on this same thing today without even noticing this topic)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.