Recent posts

#41
Script Showroom / Squirrel Dependency Injection ...
Last post by PSL - Oct 27, 2025, 03:07 AM
This Squirrel code implements a simple dependency injection (DI) framework with the following core features:

1. Provides a Module class for defining dependency bindings, supporting two binding types: single (singleton) and factory (factory). Register types and corresponding providers through the single() and factory() methods respectively.
2. Sqoin serves as the framework entry point and provides singleton context access (getInstance()), module loading (loadModules()), and instance acquisition (get()) functions through static methods.
3. SqoinContext is the core of the context, responsible for managing modules, storing singleton and factory registration information, and obtaining instances by type through the get() method (the singleton is created and cached by the provider when it is first obtained, and the factory creates a new instance each time it is called)

The overall dependency registration and resolution mechanism is implemented, which can be used to manage object dependencies in the server.

class Logger {
    function log(message) {
        print("Log: " + message);
    }
}

class UserRepository {
    logger = null;

    constructor(logger) {
        this.logger = logger;
    }

    function getUser(id) {
        this.logger.log("Getting user with id: " + id);
        return {
            id = id,
            name = "User " + id
        };
    }
}

class UserService {
    repo = null;
    logger = null;

    constructor(repo, logger) {
        this.repo = repo;
        this.logger = logger;
    }

    function getUserInfo(id) {
        this.logger.log("Fetching user info");
        return this.repo.getUser(id);
    }
}

This Squirrel code defines three business classes, forming a simple user information processing chain:
1. Logger class: Provides basic logging functionality, printing messages via the log method.
2. UserRepository class: Responsible for acquiring user data. Its constructor relies on Logger. It uses the getUser method to query user information based on ID and log it.
3. UserService class: Serves as the business service layer, relying on UserRepository and Logger. It encapsulates the user information acquisition process via the getUserInfo method, logging before calling repository layer methods.

The three collaborate through dependencies, embodying the layered design concept (log component, data access layer, business service layer), and can be combined with the dependency injection framework to manage dependencies.

local userModule = Module(function(module) {
        module.single("Logger", function(sqoin) {
            return Logger();
        });

        module.single("UserRepository", function(sqoin) {
            return UserRepository(sqoin.get("Logger"));
        });

        module.factory("UserService", function(sqoin) {
            return UserService(sqoin.get("UserRepository"), sqoin.get("Logger"));
        });
    });

This Squirrel code defines a dependency injection module userModule, which is used to configure the instance creation rules of each business class:
1. The configuration function is passed to the Module constructor, and the Logger and UserRepository are registered as singletons using the single method:
    1. The Logger instance is created directly using the no-argument constructor.
    2. The UserRepository depends on the Logger and constructs itself after obtaining the Logger instance through the dependency injection container.
2. The UserService is registered as a factory using the factory method: Each time a new UserService is constructed, the container retrieves the UserRepository and Logger instances.

This module declares dependencies and provides instance creation and dependency management rules for the previously defined Logger, UserRepository, and UserService, allowing them to be loaded and used by the injection framework.

local modules = [userModule];

Sqoin.loadModules(modules);

local userService1 = Sqoin.get("UserService");
local userService2 = Sqoin.get("UserService");

userService1.getUserInfo(123);
userService2.getUserInfo(456);

This Squirrel code example demonstrates the use of the dependency injection framework:
1. First, the previously defined userModule is added to the modules array. Sqoin.loadModules is used to load the module, completing dependency registration.
2. Sqoin.get("UserService") is called twice to obtain service instances (because UserService is registered as a factory, two different instances are generated).
3. The getUserInfo method is called on each UserService instance, passing in different IDs (123 and 456) to retrieve user information.

Running this code triggers logging and user data querying, demonstrating how the dependency injection framework manages object creation and dependency propagation. It also demonstrates the factory pattern's characteristic of generating a new instance each time a request is made.

#42
Support / Re: any built-in Solution?
Last post by (vG)DesTroYeR^ - Oct 26, 2025, 10:38 PM
Quote from: vitovc on Oct 26, 2025, 06:36 PMflooding PlaySound(id) some blank sound would help to stop current sound (in theory, not tested)

my friend gave me empty sound(id 255), but i will give it a try, i dont think it would work unless it is a flag which can be customizable
#43
Videos & Screenshots / HerKulano : The Legend Who Ref...
Last post by herqules - Oct 26, 2025, 10:33 PM
#44
Support / Re: any built-in Solution?
Last post by vitovc - Oct 26, 2025, 06:36 PM
flooding PlaySound(id) some blank sound would help to stop current sound (in theory, not tested)
#45
General Discussion / Re: The VC-MP 📖 reading starte...
Last post by vitovc - Oct 26, 2025, 06:34 PM
#46
Support / any built-in Solution?
Last post by (vG)DesTroYeR^ - Oct 26, 2025, 12:06 PM
anyflag <- FindPlayer(0).PlaySound(id) // EXAMPLE Cant be stopped using:
anyflag = null;
or
anyflag.Stop();
anyother solution?

I want to stop it once playermove is being called under some conditions
#47
General Discussion / The VC-MP 📖 reading starter pa...
Last post by kyber7 - Oct 26, 2025, 08:26 AM
#48
Support / Re: onPlayerMove but in client...
Last post by [R3V]Kelvin - Oct 23, 2025, 03:49 PM
Quote from: (vG)DesTroYeR^ on Oct 23, 2025, 09:50 AM
Quote from: Xmair on Oct 22, 2025, 08:53 PMYou could use Script::ScriptProcess

this doesn't have the desired parameters
And you don't need any! All you gotta do is use your imagination. Just refer to the wiki for reference when coding: https://wiki.vc-mp.org/wiki/Scripting/Squirrel/Client_Functions

localPlayerPos     <- null;
lastLocalPlayerPos <- null;

function Script::ScriptLoad()
{
    ::localPlayerPos = ::World.FindLocalPlayer().Position; // By reference (auto updated)
    ::lastLocalPlayerPos = ::Vector(::localPlayerPos); // We need a copy, not a reference
}

function Script::ScriptProcess()
{
    // Because we made a copy these will effectively differ from each other
    if ((::localPlayerPos.X != ::lastLocalPlayerPos.X) ||
        (::localPlayerPos.Y != ::lastLocalPlayerPos.Y) ||
        (::localPlayerPos.Z != ::lastLocalPlayerPos.Z))
    {
        ::OnLocalPlayerMove();
        ::lastLocalPlayerPos = ::Vector(::localPlayerPos);
    }
}

// -----------------------------------------------------------------------------

// No need for parameters at all! Just make use of the globals we already defined
function OnLocalPlayerMove()
{
    Console.Print("lastPos: (" + lastLocalPlayerPos.X + ", " + lastLocalPlayerPos.Y + ", " + lastLocalPlayerPos.Z +
        ")\t|\tnewPos: (" + localPlayerPos.X + ", " + localPlayerPos.Y + ", " + localPlayerPos.Z + ")");
}
#49
Script Showroom / How to detect a falling or jum...
Last post by (vG)DesTroYeR^ - Oct 23, 2025, 02:54 PM
----------> JUMP DETECTION <----------

function IsJumping(p) {
if(p.Action == 41) return 1; // (true)
else return 0; // (false)
}

----------> JUMP EXAMPLE CODE USAGE <----------

function onPlayerMove(player, lastX, lastY, lastZ, newX, newY, newZ){
if(IsJumping(player)){
player.Speed.z += 1;   // E---X---A---M---P---L---E
}
}


_________________________________________


----------> FALL DETECTION <----------

function IsFalling(p, Znew, Zlast) {
if(IsJumping(p)) return false;
if(Znew == Zlast) return false; // rarely happens if the player is continually moving, since you can barely find a straightforward surface so Z positions are almost always dynamic
local newZlower = (Znew < Zlast ? true : false);
if(newZlower) {
if(Zlast - Znew < 0.15) return false; // Not Falling (e.g, 11.3 - 11.2 = 0.1 is not a fall)
else return true; // > 0.15 is a fall (Approximate numbers - I didn't really calculate it precisely)
}
else {return false;} // Znew is bigger -> so climbing or going up somehow
}

----------> FALLING EXAMPLE CODE USAGE <----------
function onPlayerMove(player, lastX, lastY, lastZ, newX, newY, newZ){
if(IsFalling(player, newZ, lastZ)) player.Immunity = 32; // E---X---A---M---P---L---E
}


Lastly, what you have to do (The code's user) is to make sure about the value (0.15) accuracy by testing it out in ur server, so I will adjust it here accordingly, or I might test it later and find out the precise value.
#50
----------> JUMP DETECTION <----------

function IsJumping(p) {
if(p.Action == 41) return 1; // (true)
else return 0; // (false)
}

----------> JUMP EXAMPLE CODE USAGE <----------

function onPlayerMove(player, lastX, lastY, lastZ, newX, newY, newZ){
if(IsJumping(player)){
player.Speed.z += 1;   // E---X---A---M---P---L---E
}
}


_________________________________________


----------> FALL DETECTION <----------

function IsFalling(p, Znew, Zlast) {
if(IsJumping(p)) return false;
if(Znew == Zlast) return false; // rarely happens if the player is continually moving, since you can barely find a straightforward surface so Z positions are almost always dynamic
local newZlower = (Znew < Zlast ? true : false);
if(newZlower) {
if(Zlast - Znew < 0.15) return false; // Not Falling (e.g, 11.3 - 11.2 = 0.1 is not a fall)
else return true; // > 0.15 is a fall (Approximate numbers - I didn't really calculate it precisely)
}
else {return false;} // Znew is bigger -> so climbing or going up somehow
}

----------> FALLING EXAMPLE CODE USAGE <----------
function onPlayerMove(player, lastX, lastY, lastZ, newX, newY, newZ){
if(IsFalling(player, newZ, lastZ)) player.Immunity = 32; // E---X---A---M---P---L---E
}


Lastly, what you have to do (The code's user) is to make sure about the value (0.15) accuracy by testing it out in ur server, so I will adjust it here accordingly, or I might test it later and find out the precise value.