Accessing another classes attributes

Questions about GP commands and how to do things

Moderator: MSandro

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

Accessing another classes attributes

Post by SimpleSi » Jul 6th, '17, 07:14

One thing I can't seem to find is to be able to access, for example, the x/y postition of another object
I can see this block
gpobj.PNG
gpobj.PNG (1.1 KiB) Viewed 12155 times
but I'm unable to work out how to place anotherobject into the obj field

Any ideas on how to achieve the end result of accessing another objects x/y postions

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

Re: Accessing another classes attributes

Post by JohnM » Jul 9th, '17, 00:46

There are several ways to get references to other objects:
  • The neighbors block returns a list of nearby objects of a given class.
  • Shared variables can be used to refer to particular objects (for example, the goals in a soccer games).
  • Instance variables can be used to remember references to objects instantiated earlier.
How do you get an object reference into a shared variable? One way is to view the scripts for that object and invoke "set shared <variable> to (this)". A quicker way it to select the Variables category then drag and drop the instance into the readout for the variable you want to set. The instance will go back to where it was on the stage and the variable will get set. In addition, an "eyeball" icon will appear next to that variable. Clicking on the icon will show a temporary arrow pointing to the instance to which it refers.

However you do it, once you have a variable that refers to a particular object you can use that variable in the "get" block to read it's x, y, and instance variables.

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

Re: Accessing another classes attributes

Post by SimpleSi » Jul 9th, '17, 12:44

This is where I'm going to have to dig in and get my brain working - I'll be back later :)

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

Re: Accessing another classes attributes

Post by SimpleSi » Jul 9th, '17, 22:04

The neighbours block returns a list of nearby objects of a given class.
I can't seem to get this block to return anything other than an empty list -even with radius set to 1000 and two objects on top of each other :(

How do you get an object reference into a shared variable? One way is to view the scripts for that object and invoke "set shared <variable> to (this)".
If just working with a class and not creating an extra instance of it then there is no (this) AFAICT
A quicker way it to select the Variables category then drag and drop the instance into the readout for the variable you want to set.
I tried lots of times and was getting anywhere and then I re-read your advice and it did work when I moved it over the empty variable readout box :)

So I'm in the object properties business now :)

Could we have access to all object properties not just the x/y positions - e.g direction, scale, width, height,costume# etc? (direction is main requirement) - I like the fact that we get access to the objects local variables - that is a VERY nice feature :)
Instance variables can be used to remember references to objects instantiated earlier.
I think I'm not up to playing at this level yet as I'm not yet creating extra instances of classes!

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

Re: Accessing another classes attributes

Post by JohnM » Jul 10th, '17, 09:49

Here's an example of using the neighbors block:
Screen Shot 2017-07-10 at 5.38.51 AM.png
There are two overlapping instances of "MyClass". One instance is asking for its neighbors within 0, which means the block returns all other instances (of it's own class) that overlap with its "bounding box". If you extend the block, it has an asterisk "*" in the "class" field by default; that means it will return all instances, of any class, that overlap with the caller. You can specify a particular class to get instances of only that class, and you can increase the sensing distance to detect instances that are further away (i.e. not actually touching). For example, in an ecology simulation, a rabbit might sense foxes within a certain distance and move away from the closest one.

Incidentally, if you just need to detect touches, the "touching" block with a class selected in the dropdown returns true when touching *any* instance of the given sprite.
Screen Shot 2017-07-10 at 5.57.30 AM.png
Screen Shot 2017-07-10 at 5.57.30 AM.png (8.79 KiB) Viewed 12114 times
This is often sufficient when you only have a single instance of a class in your project. For example, in your pool challenge simulation, the PoolRim class would only have a single instance, so you can just select "PoolRim" in the touching block's menu to sense collisions with the edge of the pool. In more complex situations, when you need to detect touching a specific instance, you can put a variable or reporter block in the argument field of the touching block.

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

Re: Accessing another classes attributes

Post by JohnM » Jul 10th, '17, 10:09

Could we have access to all object properties not just the x/y positions - e.g direction, scale, width, height,costume# etc? (direction is main requirement)
A sprite has a LOT of properties (e.g. pen size, pen color, is draggable, ...) and I don't want that menu to get too long. Note, however, that you can always create an instance variable and store a given property into it to make it available via the "get" block.

That said, I agree that "direction" might be useful often enough (like x and y) to be worth having in the menu.

SvenJ
Posts: 25
Joined: Jul 6th, '17, 08:03

Re: Accessing another classes attributes

Post by SvenJ » Jul 10th, '17, 10:16

On this note, is it still possible to directly reference another class?
It used to be possible via the extended (shift+right-click) menu, but I don't see the option anymore.

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

Re: Accessing another classes attributes

Post by SimpleSi » Jul 10th, '17, 13:48

If you extend the block, it has an asterisk "*" in the "class" field by default; that means it will return all instances, of any class, that overlap with the caller.
I've been try and trying and not getting anywhere and now it works all the time :) The gremlins inside my machine mare playing with me! :)

I hadn't realised that in default mode it only finds instances of same class but i was SURE i'd tried expanding and changing parameter :) Never Mind.Re

A sprite has a LOT of properties (e.g. pen size, pen color, is draggable, ...) and I don't want that menu to get too long.
Well - my thoughts are that GP should be a superset of Scratch unless (like the touching colour issue) there is a good tech reason for not being so.

But, in practice, I've never accessed another sprites attributes apart from x,y,direction and costume (I did a logic gate simulator once and used costumes as alias for gate types and needed that when joining them up)

Post Reply