Posted by
Scott Wallace on
Mar 26, 2011; 10:10am
URL: https://forum.world.st/Re-SQ-903-Object-displayed-name-does-not-always-match-name-used-to-refer-to-object-in-scripts-tp3404688p3407333.html
On Mar 25, 2011, at 12:34 AM, Steve Thomas wrote:
> Scott,
>
> Thanks, using this works fine (mostly),
>
> On Wed, Mar 2, 2011 at 5:28 AM, Scott Wallace <
[hidden email]> wrote:
> catPlayer := ActiveWorld presenter reallyAllExtantPlayersNoSort detect:
> [:p | p costume externalName = 'Cat'] ifNone: [nil].
> catPlayer ifNotNil: [ catPlayer forward: 5 ]
>
> Except when I have multiple Players with the same name in the project. For example if I have a Player with the ExternalName 'Cat' on page 1 and another Player with the ExternalName 'Cat' on page 2. Then execute the script on Page 2 the 'Cat' on Page 1 moves forward. (Note: in playing around with this at one point the 'Cat' on page 2 did move, but when I renamed the 'Cat' and made a new one it went back to the behavior where the 'Cat' on page 1 moved again.
>
> I changed the script to refer to 'Page' instead of 'ActiveWorld' and that seemed to solve the problem, until I duplicated the page (which causes 'Cat' to become 'Cat1') and then renamed 'Cat1' to 'Cat' then the 'Cat' on the previous page moved.
>
> So is there anyway to specify the 'search order' for players where it looks in its current ?World/Playfield? first?
> Of course the simplest answer may be for me to avoid the problem and not use books ;)
>
> Stephen
Hi, Stephen,
Yes, the technique we've spoken of will find *some* player of a given name -- not necessarily the one you're hoping for. The "reallyAllExtantPlayersNoSort" method makes an extreme effort (as its extreme name suggests) to include players that may not currently be "in the world," so name collisions of the sort you mention will arise when "books" involving multiple pages holding players of the same name are used.
If you expect that one and only one player of a given name is actually going to be "in the world" at the moment a particular script is running, and you only want something in the script to happen if such a player *is* found, the following approach should work:
foundPlayer := ActiveWorld presenter reallyAllExtantPlayersNoSort
detect: [:p |(p costume externalName = 'Cat') and: [p costume isInWorld])]
ifNone: [nil].
foundPlayer ifNotNil:
[ <proceed to do something to or with foundPlayer...> ]
Cheers,
-- Scott
_______________________________________________
etoys-dev mailing list
[hidden email]
http://lists.squeakland.org/mailman/listinfo/etoys-dev