[Home] [Downloads] [Search] [Help/forum]

GetWorldById

Script function

world.GetWorldById

Read about scripting

Type

Method

Summary

Gets an object reference to the world given its unique ID

Prototype

IDispatch* GetWorldById(BSTR WorldID);

View list of data type meanings

Description

Returns an object which can be used to refer to another world.

You should be very cautious about storing the object reference of the world in a global variable, because if the world is closed then that reference becomes invalid, which will very likely lead to an access violation (program crash). You are better off using "world.getworldbyid (id)" every time you need to get a reference to the world, and checking if it "is nothing" as in the example.

This does not apply to Lua which checks the world object every time it is used.

You could use a small variation on the examples above to write your own "send to all worlds" or something similar.


Available in MUSHclient version 3.39 onwards.



VBscript example

' --------------------------------------------------
' Example showing sending a message to all worlds
' --------------------------------------------------

sub SendToAllWorlds (message)

dim otherworld

for each id in GetWorldIdList
  set otherworld = GetWorldById (id)
  otherworld.Send message
  set otherworld = nothing
next

end sub

SendToAllWorlds "sigh"


Jscript example

// --------------------------------------------------
// Example showing sending a message all worlds
// --------------------------------------------------

function SendToAllWorlds (message)
{
 worldlist = new VBArray(GetWorldIdList()).toArray();

 if (worldlist)  // if not empty
  for (i = 0; i < worldlist.length; i++)
    GetWorldByID (worldlist [i]).Send (message);

}

SendToAllWorlds ("say Hi there");


PerlScript example

# --------------------------------------------------
# Example showing sending a message to another world
# --------------------------------------------------

sub SendToAllWorlds {
 my ($message) = @_;

 my $otherworld;

 foreach $item (Win32::OLE::in ($world->GetWorldIdList))
 {
 GetWorldById ($item)->Send ($message);
 }

}

SendToAllWorlds ("say Hi there");


Python example

# --------------------------------------------------
# Example showing sending a message all worlds
# --------------------------------------------------

def SendToAllWorlds (message):
  worldlist = world.GetWorldIdList
  if (worldlist):
    for w in worldlist : world.GetWorldByID (w).Send (message)

SendToAllWorlds ("say Hi there")


Lua example

-- --------------------------------------------------
-- Example showing sending a message all worlds
-- --------------------------------------------------

function SendToAllWorlds (message)

  for k, v in pairs (GetWorldIdList ()) do 
    GetWorldById (v):Send (message)
  end
end

SendToAllWorlds ("say Hi there")


Lua notes

See the description for GetWorld for more discussion about using "world" variables in Lua.


Return value

An object reference to the named world, if it was found.
Otherwise NULL.

In Vbscript use the test "is nothing" to see if the reference is to a valid world.
In JavaScript use the test "== null" to see if the reference is to a valid world.
In PerlScript use the test "defined ()" to see if the reference is to a valid world.
In Lua use the test "if ref == nil" to see if the reference is to a valid world.


See Also ...

Topics

Scripting
World functions

Functions

(GetWorld) Gets an object reference to the named world
(GetWorldIdList) Gets the list of open worlds - returning their world IDs
(GetWorldList) Gets the list of open worlds - returning their world names
(Open) Opens a named document
(OpenBrowser) Opens a supplied URL in your default web browser

(Help topic: function=GetWorldById)

Documentation contents page


Search ...

Enter a search string to find matching documentation.

Search for:   

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]