Passing arguments without a broadcast

Questions about GP commands and how to do things

Moderator: MSandro

Post Reply
Damien
Posts: 2
Joined: Sep 6th, '17, 18:30

Passing arguments without a broadcast

Post by Damien » Sep 6th, '17, 18:37

Hello everyone!
I'm currently trying to build logic gates (like AND, OR, NOT, ...) in GPBlocks. So far, I created 3 classes, an AND-class, an Input class and an output class. Now my AND class uses variables to save instances of the input and output classes, because every input and output always only belongs to one logic gate.
And this is where I'm stuck right now:
It's no problem to get values from the input instances to my AND instance. But I don't know how to pass a value to my output instance without using a broadcast. What I need would be a set-method. But I don't know exactly how to implement that. Can anyone help?
I attached my progress so far.

Thanks in advance!
Attachments
sim.gpp
(9.89 KiB) Downloaded 305 times

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Passing arguments without a broadcast

Post by SimpleSi » Sep 6th, '17, 21:35

I've used broadcast but only to the output instance

The output instance can then act on it without any other object being effected so I think that is functionally the same?
simsw.gpp
(10.98 KiB) Downloaded 312 times

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Passing arguments without a broadcast

Post by SimpleSi » Sep 6th, '17, 22:19

I think this is might be closer to what you want

I've defined a method in output that takes an instance reference and a value and then copied and pasted that block into the AND class

Seems to work - if you add another AND class (and sort out what sprite belongs to what by moving things around) you can have two independant AND gates

Simon
simsw2.gpp
(11.08 KiB) Downloaded 302 times

Damien
Posts: 2
Joined: Sep 6th, '17, 18:30

Re: Passing arguments without a broadcast

Post by Damien » Sep 7th, '17, 04:44

Thanks a lot, that was actually what I was looking for!

I didn't know that you could simply copy and paste that block into the AND class. That makes is so much easier.
Of course this is still a raw version and I will 'beautify' this a little. But it was more important to get the functionality right.

Again, thanks for the help!

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Passing arguments without a broadcast

Post by SimpleSi » Sep 7th, '17, 19:22

While looking for somethign else today I found a primitive function called setField which will let us change the value of of an instance variable

I wrapped it into a class just to try it out
If it was made a bit user friendlier like the get function (with menus for the obj and variable_name)- it could be quite useful to everyone

Code: Select all

defineClass setFieldClass morph


script 'setFieldClass' 449 105 {
setField 'obj' 'variable_name' 'value'
}
setFieldClass.gp
(109 Bytes) Downloaded 344 times

JohnM
Posts: 379
Joined: Sep 11th, '15, 14:42

Re: Passing arguments without a broadcast

Post by JohnM » Sep 26th, '17, 17:38

While 'setField' can be useful for debugging, it's best to avoid using it in code.

Why? Because if you're trying to figure out why some instance variable got changed, you'd need to search all scripts in the entire project in case one of them is using 'setField' to change it. If, by convention, everyone avoids using 'setField' then you can safely assume that the only scripts that can change a given instance variable are methods in the same class. That assumption allows you to safely ignore the scripts in all the other classes.

What are some better ways to handle your situation?

One way is to have the "Output" object continuously watch the output of the "AND" gate and respond accordingly (e.g. by changing it's costume). The "get" block (in Sensing) can be used to read an instance variable from another object. In this case, I added a "gate" instance variable and made it point to the AND gate, added the "my gate" variable block to the get block, and selected "output 1" from the menu:
Screen Shot 2017-09-26 at 1.49.50 PM.png

An alternate way is to create a method in the "Output" class that sets it's state:
Screen Shot 2017-09-26 at 1.53.39 PM.png

You can then export that block to the palette, drag it into the AND gate's scripting area, and call it like this:
Screen Shot 2017-09-26 at 1.56.10 PM.png

While the second alternative still changes the Output instance from outside, it is better than using 'setField' because the fact that the block has been exported to the palette indicates that it may be called by other classes. Also, GP will eventually have tools that allow you to find all the calls to a given method. Assuming nobody is using 'setField', the only scripts outside of the Output class that could be relevant are ones that contain the "set output" block.

Of the two alternative solutions, I prefer the first one, in which the Output script watches the AND gate and no object's instance variables are changed from the outside. That allows you (or someone else reading your code!) to understand how and when the "Output" class's instance variables change without looking at any scripts outside of the Output's class.

As programs get larger, being able to reason locally about code becomes increasingly important.

SimpleSi
Posts: 330
Joined: Jul 2nd, '17, 13:47

Re: Passing arguments without a broadcast

Post by SimpleSi » Sep 26th, '17, 19:22

While 'setField' can be useful for debugging, it's best to avoid using it in code.
OK - I'll forget I ever found it
Simon

Post Reply