[Beta] Individual Gravity system

Started by Razor., Aug 01, 2021, 05:58 AM

Previous topic - Next topic

Razor.

Individual Gravity system v0.2b

VC:MP itself has the possibility to change the gravity, however this function when used affects the gravity of all players, but today I bring a practical way to solve this.


Here's a video showing how it works for someone who wants to see it before testing the code: https://www.youtube.com/watch?v=-Jg6U0nmjNs&feature=youtu.be



Repository (v0.2b)
https://github.com/Razorn7/Individual-Gravity-VCMP


Install:
  • Download the script files from the repository above and past in scripts/ folder.
  • Load the script using dofile("scripts/i_gravity.nut");.
  • Adept this to some events:
function onPlayerJoin(player) {
CGravity.join(player);
}

function onPlayerMove(player, lastX, lastY, lastZ, newX, newY, newZ) {
CGravity.processMove(player);
}


    How to use:
    • -Simple, after you have added the script, execute the player.SetGravity(some value here, must be number) in any part of you code you want, then gravity will take effect on the entity.
    Added functions: player.SetGravity(number gravity)
    player.GetGravity(void)

    Example:
    [spoiler]function onPlayerCommand(player, cmd, text)
    {
    if ( cmd == "setgravity" ) {
    if (!text) PrivMessage(player, "Use: /" + cmd + " <player/ID> <value>");
    else {
    local plr = FindPlayer(GetTok(text, " ", 1));
    if (!plr) PrivMessage(player, "Invalid player.");
    else {
    local value = GetTok(text, " ", 2);
    if (value == null) PrivMessage(player, "You must input a value.");
    else if (!IsNum(value)) PrivMessage(player, "The value type must be integer|float.");
    else {
    plr.SetGravity(value.tointeger());
    PrivMessage(player, "You've set " + plr.Name + "'s gravity to: " + value.tointeger() + ".");
    }
    }
    }
    }
    }

    /* GetTok() by KAKAN
       https://pastebin.com/fLTXmNb6 */

    function GetTok(string, separator, n, m = 0) {
    n = n - 1;
    m = m > 0 ? m : n + 1;
    local tokenized = split(string, separator),
    text = "";
    if(n > tokenized.len() || n < 0 || m > tokenized.len()) return null;
    tokenized = tokenized.slice(n, m);
    return tokenized.reduce( function(prev, next) { return (prev + separator + next) });
    }
    [/spoiler]


    Notes:
    • This won't work as own VCMP gravity.
    • The default gravity value is 0.
    • This will just work if the player's gravity is != 0.
    • Negative numbers will work too.


    Thanks to S.L.C for clarifying a doubt so that the player.SetGravity() return would work the way I wanted.

    ℛḝξ☂

    Wow it's such an amazing snippet, I cannot wait to have a try
    I may be a slow walker but I am always walking.

    Luis_Labarca

    My server RP
    IP: 51.222.28.159:8194