GetWorld
Script function

world.GetWorld

DOC_scripting Read about scripting

Type

Method

Summary

Gets an object reference to the named world

Prototype

IDispatch* GetWorld(BSTR WorldName);

DOC_data_types 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.getworld (name)" every time you need to get a reference to the world, and checking if it "is nothing" as in the example.

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

If more than one world has the same name, only the first one matching that name will be returned. If this is likely, use GetWorldIDList to get a list of unique IDs (identifiers) and then use GetWorldById instead of GetWorld.



VBscript example

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

sub SendToWorld (name, message)
dim otherworld

  set otherworld = world.getworld (name)

  if otherworld is nothing then
    world.note "World " + name + " is not open"
    exit sub
  end if

  otherworld.send message

end sub



Jscript example

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

function SendToWorld (name, message)
{
 var otherworld

   otherworld = world.getworld (name);

  if (otherworld == null)
    {
    world.note("World " + name + " is not open");
    return;
    }

  otherworld.send(message);

}

SendToWorld ("MyOtherWorld", "say Hi there");



PerlScript example

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

sub SendToWorld {
 my ($name, $message) = @_;

 my $otherworld;

 $otherworld = $world->getworld ($name);

  if (!defined ($otherworld))
    {
    $world->note("World " . $name . " is not open");
    return;
    }

  $otherworld->send($message);

}

SendToWorld ("MyOtherWorld", "say Hi there");



Python example

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

def SendToAllWorlds (message):
  worldlist = world.GetWorldList
  if (worldlist):
    for w in worldlist : world.GetWorld (w).Send (message)

SendToAllWorlds ("say Hi there")



Lua example

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

function SendToWorld (name, message)

local otherworld

  otherworld = GetWorld (name)

  if otherworld == nil then
    Note ("World " .. name .. " is not open")
    return
  end

  Send (otherworld, message)

  -- alternative syntax:   otherworld:Send (message)

end -- of SendToWorld

SendToWorld ("MyOtherWorld", "say Hi there")



Lua notes

Since Lua does not use the COM (Component Object Model) interface getting world 
variables works differently than for other languages.

The function GetWorld returns a "userdata" item which contains the address of the other world.

You can pass this as the first argument to any MUSHclient function and that 
function will then be applied to the target world.

eg.

otherworld = GetWorld ('pennmush')

Send (otherworld, "sigh")  --> send "sigh" to 'pennmush' world


An alternative syntax is to put the world name first, followed by a colon (not a dot), 
like this:

otherworld:Send ("sigh")  --> send "sigh" to 'pennmush' world

Every function call that is passed a different world name is automatically checked to see
if the world is still open, so it is safe to use the world variable some time after
obtaining it (although an error will be raised if you do so).

You can protect function calls using pcall, so you might do this to see if the
world is still valid:

-- try to call WorldName for world 'otherworld'

ok = pcall (WorldName, otherworld)

if ok then
  Note "otherworld is OK"
else
  Note "otherword has closed"
end



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 Python use the test "if (ref)" 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

DOC_scripting Scripting
DOC_world World functions

Functions

FNC_GetWorldById GetWorldById (Gets an object reference to the world given its unique ID)
FNC_GetWorldIdList GetWorldIdList (Gets the list of open worlds - returning their world IDs)
FNC_GetWorldList GetWorldList (Gets the list of open worlds - returning their world names)
FNC_Open Open (Opens a named document)
FNC_OpenBrowser OpenBrowser (Opens a supplied URL in your default web browser)

(Help topic: function=GetWorld)

DOC_contents Documentation contents page