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
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
Accessing another classes attributes
Moderator: MSandro
Re: Accessing another classes attributes
There are several ways to get references to other objects:
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.
- 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.
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.
Re: Accessing another classes attributes
This is where I'm going to have to dig in and get my brain working - I'll be back later :)
Re: Accessing another classes attributes
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 :(The neighbours block returns a list of nearby objects of a given class.
If just working with a class and not creating an extra instance of it then there is no (this) AFAICTHow 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)".
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 :)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.
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 :)
I think I'm not up to playing at this level yet as I'm not yet creating extra instances of classes!Instance variables can be used to remember references to objects instantiated earlier.
Re: Accessing another classes attributes
Here's an example of using the neighbors block:
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.
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.
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.
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.
Re: Accessing another classes attributes
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.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)
That said, I agree that "direction" might be useful often enough (like x and y) to be worth having in the menu.
Re: Accessing another classes attributes
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.
It used to be possible via the extended (shift+right-click) menu, but I don't see the option anymore.
Re: Accessing another classes attributes
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! :)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 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
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.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.
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)