Skip to content

Latest commit

 

History

History
168 lines (120 loc) · 4.86 KB

7. Gangzones.md

File metadata and controls

168 lines (120 loc) · 4.86 KB

Gangzones

Gangzones are highly recommended for Gang/TDM gamemodes. In this tutorial we will expand the gangzones.

Writing Code

Preparing

We need to import on_gamemode_init and the module callbacks. Since we will be working with gangzones, we import Gangzone() class from the gangzone module and the class Player() from the player module. Also dialogs will be used in commands, so import cmd from commands

from pysamp import (
    on_gamemode_init,
    on_gamemode_exit
)
from pysamp.gangzone import Gangzone
from pysamp.player import Player
from pysamp.commands import cmd

I also suggest using add_player_class() to keep your player from falling underground.

from pysamp import add_player_class
@on_gamemode_init
def your_function():
    add_player_class(0, 2484.0, -1660.0, 1.5781, 265.4560, 0, 0, 0, 0, 0, 0)

We are done with the preparations, now let's start creating gangzones.

Creating gangzones

First things first, lets talk about how a gangzone created, gangzones are a 4-sided shape.

        MinY
            v
    MinX > *-------------
            |            |
            |  Gangzone  |
            |   center   |
            |            |
            -------------* < MaxX
                        ^
                        MaxY

Useful software

There are several means to create gangzones:

Creating Gangzones

You can create a class instance and use it, or you can create a dictionary and store the gangzones there.

# python/zones.py
gangzone = Gangzone(1572.0, -2323.0, 1739.0, -2245.0)

Or another way.

# python/zones.py
gangzones = {}
gangzones[0] = Gangzone(1572.0, -2323.0, 1739.0, -2245.0)

In the future we will use the first method, as it is more obvious.

Methods

Each gangzone has its own unique ID. To get it there is a method get_id(). Let's look at an example of a simple command.

@cmd
def zoneid(player: Player):
    print(gangzone.get_id())

So now you know how to get an ID. There is also the destroy() method. This method will destroy our gangzone.

@cmd 
def destroyzone(player: Player):
    gangzone.destroy()

That's it, now the gangzone will not be visible, because it has been removed.

Often, you will show gangzones to the player when he connects, for this we will use the @Player.on_connect event.

The show_for_player() method is used for this. It takes two arguments: player and color. The player is the player to whom the gangzone will be shown, color is the color of the gangzone.

@Player.on_connect
def on_player_connect(player: Player):
    gangzone.show_for_player(player, 0xFF0000FF)

There is also a similar method hide_for_player() that hides the gangzone for the player. Let's use it as an example of a command. This method takes the player argument, so this is the player for whom we want to hide the gangzone.

@cmd
def hide(player: Player):
    gangzone.hide_for_player(player)

You can also show gangzones for everyone, there is a method show_for_all() for this. You'll probably use it when loading a mode, but here we'll break it down with an example command.

@cmd
def showall(player: Player):
    gangzone.show_for_all(0xFF0000FF)

Accordingly, to hide gangzones from all players, use the hide_for_all() method.

@cmd
def hideall(player: Player):
    gangzone.hide_for_all(0xFF0000FF)

The flash_for_player() method and another similar method (which will come later) will be used almost every time you create an interaction between players and gangzones. This method forces the gangzone to flash with color. So it takes to arguments: player and color.

@cmd
def flash(player: Player):
    gangzone.flash_for_player(player, 0xFFFFFFFF)

The stop_flash_for_player() method makes the gangzone stop changing color.

@cmd
def stopflash(player: Player):
    gangzone.stop_flash_for_player()

The flash_for_all() method will make the gangzone flash color for all players (all players will see it).

@cmd
def flashall(player: Player):
    gangzone.flash_for_all(0xFF0000FF)

The stop_flash_for_player() method will make the gangzone stop changing color for all players.

@cmd
def stopall(player: Player):
    gangzone.stop_flash_for_player()

Notice:

  • Methods like: show_for_all(), hide_for_all(), flash_for_all(), stop_flash_for_all() they works for players who are connected to. That means that if you call show_for_all() and then the player joins the server after calling this function, the gangzone will not be visible to the player.

Conclusion

In this tutorial you learned how to work with gangzones, in the next tutorial you will learn how to work with menus.