Virtual Worlds

From SA-MP Wiki

Jump to: navigation, search

Contents


Definition

Virtual worlds are a special feature in SA-MP, where a player can be separated against other players by putting him in another world. A player in a different world may not see checkpoints, players or even vehicles, which helps create PAWN scripts like house or business systems by assigning each house a different world.

Consider a situation, where we want races for 10 players, but the race must be only between two people and that too, on the same track. This would be accomplished by setting the virtual worlds of the first two players as same, the second pair of players as different, etc. This would make the contestants see each other but they would not see any other players.

  • Player 1 and Player 2 in virtual world 555.
  • Player 3 and Player 4 in virtual world 556.

There are a number of implementations of this on the forums.

Limits

The maximum number of usable virtual worlds are changed with a version. Can be checked on the Limits page. As of now, 2,147,483,647 virtual worlds can be used.

Functions

There are two functions that help us manage virtual worlds, namely GetPlayerVirtualWorld and SetPlayerVirtualWorld.

Regarding vehicles, there are two functions that help us manipulate virtual worlds, namely GetVehicleVirtualWorld and SetVehicleVirtualWorld.

Examples

A piece of code that allows two pairs of players to have a duel at the SAME place.

static i = 555; // Base world ID
 
COMMAND:duel(playerid, params[])
{
    new target;
    if(sscanf(params, "u", target)) return 0; // The player's ID to be dueled with is to be supplied in the parameters.
 
    if(!IsPlayerConnected(target)) return 0;  // The player to be dueled with is not online.
 
    SetPlayerPos(playerid, 5, 5, 5); // Set their position to the duel place.
    SetPlayerPos(playerid, 5, 5, 5);
 
    GivePlayerWeapon(playerid, 31, 100); // Give them weapons to duel.
    GivePlayerWeapon(target, 31, 100);
 
    SetPlayerVirtualWorld(playerid, i); // Set their virtual world so that they won't be interrupted by other players.
    SetPlayerVirtualWorld(target, i); 
 
    i++; // The next virtual world for the next set of players who may want to duel.
 
    return 1;
}

This is a very vague example, but it shows the usage of virtual worlds.

For an example on how to use them to make businesses or houses, can be view [here]. Note that there are better ways of doing the same.