Jump to content

Recommended Posts

Posted

Tutorial

Random Spawns

 

Deci este un tutorial foarte simplu.

Pentru a crea Random Spawns, exista 2 metode la care te poti gandi.

1. Switch() statement

2. Multi-Dimensional Arrays

 

Sunt foarte multi incepatori care nu reusesc sa inteleaga conceptul de Multi-Dimensional Arrays, asa ca in acest tutorial le voi arata cum sa creeze un sistem de Random Spawns si lucruri similar fara a utiliza Multi-Dimensional Arrays. Avand in privire ca Multi-Dimensional Arrays este explicat in wiki o sa o explic pe a doua.

Uitati un cod care arata callback ul OnPlayerSpawn(), de exemplu, daca doriti sa generati jucatorul in una dintre cele 5 pozitii aleatorii, acest cod va functiona :

public OnPlayerSpawn (playerid)
{
    new selected_spawn = random (5);

    switch (selected_spawn)
    {
        case 0: { SetPlayerPos (playerid, 0.0,   0.0,   0.0);  SetPlayerFacingAngle (playerid, 0.0); }
        case 1: { SetPlayerPos (playerid, 10.0, 10.0, 10.0); SetPlayerFacingAngle (playerid, 10.0); }
        case 2: { SetPlayerPos (playerid, 20.0, 20.0, 20.0); SetPlayerFacingAngle (playerid, 20.0); }
        case 3: { SetPlayerPos (playerid, 30.0, 30.0, 30.0); SetPlayerFacingAngle (playerid, 30.0); }
        case 4: { SetPlayerPos (playerid, 40.0, 40.0, 40.0); SetPlayerFacingAngle (playerid, 40.0); }
    }
    return 1;
}

Explicatie:

1. Code:

new selected_spawn = random (5);

Aici puteti vedea o noua variabila de tip integer fiind create urmata de un assignment operator si de o functie numita `random()`. Aceasta semantica a acestei linii de cod functioneaza astfel:

-Se creeaza o variabila integer.

-Apoi genereaza o valoare random intre 0 si 4 (5 este dimensiunea intervalului sau, deoarece in codificare numaratoarea incepe de la 0, deci 0, 1, 2, 3, 4 = 5)

-Atribuie valoarea generata Random variabilei nou create.

------------------------------------------------------------------------------------------------

2. Code:

switch (selected_spawn) { }

Aceasta se numeste switch statement, mai usor este o alternativa la if-statement cu cateva limitari, dar poate fi folosita cu grija in acest caz. Deci va executa o functie sau un set de functii in functie de valoarea variabilei "selected_spawn".

Dupa cum puteti vedeam, i am oferit 2 functii de indeplinit in fiecare caz, de exemplu, daca valoarea lui "selected_spawn" se dovedeste a fi 1, acesta va indeplinii urmatoarele functii si, prin urmare, pozitia jucatorului va fi setata la coordonatele date.

Code: 

switch (selected_spawn)
    {
        case 0: { SetPlayerPos (playerid, 0.0,   0.0,   0.0);  SetPlayerFacingAngle (playerid, 0.0); }
        case 1: { SetPlayerPos (playerid, 10.0, 10.0, 10.0); SetPlayerFacingAngle (playerid, 10.0); }
        case 2: { SetPlayerPos (playerid, 20.0, 20.0, 20.0); SetPlayerFacingAngle (playerid, 20.0); }
        case 3: { SetPlayerPos (playerid, 30.0, 30.0, 30.0); SetPlayerFacingAngle (playerid, 30.0); }
        case 4: { SetPlayerPos (playerid, 40.0, 40.0, 40.0); SetPlayerFacingAngle (playerid, 40.0); }
    }

Asta a fost tot, daca doriti sa mai adaugati un alt random spawn, puteti creste intervalul functiei random() si sa adaugati coordonatele acesteia in switch statement.

De asemenea, puteti creea o functie din ea, asa ca daca doriti sa setati pozitii random de mai multe ori in cod, va fi util. 

Uite un exemplu de cod: 

Code: 

public OnPlayerSpawn (playerid)
{
    SetRandomPosition (playerid); // You can use this multiple times in other parts of code too.
    return 1;
}

stock SetRandomPosition (playerid)
{
    new selected_spawn = random (5);

    switch (selected_spawn)
    {
        case 0: { SetPlayerPos (playerid, 0.0,   0.0,   0.0);  SetPlayerFacingAngle (playerid, 0.0); }
        case 1: { SetPlayerPos (playerid, 10.0, 10.0, 10.0); SetPlayerFacingAngle (playerid, 10.0); }
        case 2: { SetPlayerPos (playerid, 20.0, 20.0, 20.0); SetPlayerFacingAngle (playerid, 20.0); }
        case 3: { SetPlayerPos (playerid, 30.0, 30.0, 30.0); SetPlayerFacingAngle (playerid, 30.0); }
        case 4: { SetPlayerPos (playerid, 40.0, 40.0, 40.0); SetPlayerFacingAngle (playerid, 40.0); }
    }
    return 1;
}
Bafta!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. For more details you can also review our Terms of Use and Privacy Policy.