Fast reload the server

Started by 2b2ttianxiu, Jun 30, 2023, 08:48 PM

Previous topic - Next topic

2b2ttianxiu

Hey, everyone. If you don't want to Ctrl+C to shutdown your server and then click server.exe to start server.
This script is your best choose.
But It's only support the server reload code to run. client-side must restart the server.

// launch.nut
Vehicles <- []
Pickups <- []
Objects <- []
Checkpoints <- []
Markers <- []
BindInstanceKeys <- {} // your globals keys. if put in the main.nut, more register keys didn't work(i tested.)
dofile("main.nut") // your script main
And the server.cfg the line: 'sqgamemode' the value change to this: launch.nut

In main.nut, add these code:
function addVehicle(vehicle) {
    if (vehicle != null) Vehicles.append(vehicle)
    return vehicle
}
function removeVehicles() {
    foreach (i, a in Vehicles) {
        if (a != null) a.Delete();
    }
    Vehicles.clear()
}
function removeVehicle(id) {
    foreach (i, a in Vehicles) {
        if (a != null && (a.ID == id || a == id)) {
            Vehicles.remove(i)
            a.Delete()
            return null
        }
    }
    return null;
}
function VehicleFind(id) {
    foreach (i, a in Vehicles) {
        if (a.ID == id) return a
    }
    return null;
} // the function is not tested. To be tested.
function addCheckpoint(checkpoint) {
    Checkpoints.append(checkpoint)
    return checkpoint
}
function removeCheckpoints() {
    foreach (i, a in Checkpoints) {
        if (a != null) a.Delete();
    }
    Checkpoints.clear()
}
function removeCheckpoint(id) {
    foreach (i, a in Checkpoints) {
        if (a != null && (a.ID == id || a == id)) {
            Checkpoints.remove(i)
            a.Remove()
            return null
        }
    }
    return null;
}
function CheckpointFind(id) {
    foreach (i, a in Checkpoints) {
        if (a != null && a.ID == id) return a
    }
    return null;
} // the function is not tested. To be tested.
function addObject(object) {
    Objects.append(object)
    return object
}
function removeObjects() {
    foreach (i, a in Objects) {
        if (a != null) a.Delete();
    }
    Objects.clear()
}
function removeObject(id) {
    foreach (i, a in Objects) {
        if (a != null && (a.ID == id || a == id)) {
            Objects.remove(i)
            a.Delete()
            return null
        }
    }
    return null;
}
function ObjectFind(id) {
    foreach (i, a in Objects) {
        if (a != null && a.ID == id) return a
    }
    return null;
} // the function is not tested. To be tested.
function addMarker(marker) {
    Markers.append(marker)
    return marker
}
function removeMarker(marker) {
    foreach (i, a in Markers) {
        if (a != null && a == marker) {
            Markers.remove(i)
            DestroyMarker(marker)
            return null
        }
    }
    return marker
}
function removeMarkers() {
    foreach (i, a in Markers) {
        DestroyMarker(a)
    }
    Markers.clear()
}
function removePickup(obj) {
    foreach (i, a in pickups) {
        if (a != null && (a.ID == obj || a == obj)) {
            pPickups.remove(i)
            a.Remove()
            return null
        }
    }
    return null;
}
function PickupFind(obj) {
    foreach (i, a in pickups) {
        if (a != null && (a.ID == obj || a == obj)) return a
    }
    return null;
} // the function is not tested. To be tested.
function addPickup(pickup) {
    Pickups.append(pickup)
    return pickup
}
function removePickups() {
    foreach (i, a in Pickups) {
        a.Remove();
    }
    Pickups.clear()
}

In onPlayerCommand function, you add these code (can edit):
if (cmd == "reload") {
    local loaded = []
    foreach (v in getOnlinePlayers()) {
        if (getPlayer(v.ID) != null && getPlayer(v.ID).Spawned) loaded.append(getPlayer(v.ID))
    }
    onServerStop()
    dofile("main.nut") // your main script.
    onScriptLoad()
    foreach (b in getOnlinePlayers()) {
        onPlayerJoin(getPlayer(b.ID))
    }
    foreach (plr in loaded) {
        onPlayerRequestClass(plr, plr.Class, plr.Team, plr.Skin)
        if (plr.IsSpawned) onPlayerSpawn(plr)
    }
    AnnounceAll("Reloaded.", 0)
    return 1;
}
if (cmd == "reloader") {
        local loaded = []
    foreach (v in getOnlinePlayers()) {
        if (getPlayer(v.ID) != null && getPlayer(v.ID).Spawned) loaded.append(getPlayer(v.ID))
    }
    onServerStop()
    dofile("main.nut") // your main script.
    onScriptLoad()
    foreach (b in getOnlinePlayers()) {
        onPlayerJoin(getPlayer(b.ID))
    }
    foreach (plr in loaded) {
        onPlayerRequestClass(plr, plr.Class, plr.Team, plr.Skin)
        if (plr.IsSpawned) onPlayerSpawn(plr)
    }
    AnnounceAll("Reloaded.", 0)
    return 1;
}
other code:
function getOnlinePlayers() {
    local players = []
    for (local i = 0; i < GetMaxPlayers(); i++) if (FindPlayer(i) != null) players.append({Name = FindPlayer(i).Name, ID = i});
    return players;
}
function getPlayer(object) {
    if (type(object) == "integer") return FindPlayer(object)
    else foreach (v in getOnlinePlayers()) if (v.Name.tolower() == (object + "").tolower()) return FindPlayer(v.ID)
    return null
}

(Source code, 'loaded' array value is state[player.ID].Loaded to get player is loaded. you can change it.)
(The 'Loaded' value set bool in 'onPlayerRequestClass' function)
(Belike: state[player.ID].Loaded = true)
(That's okay)
I have many utils code. If u want to use it. contect me

2b2ttianxiu

They are using in my servers. ERC(Extreme Racing Community) and Extreme Hide And Seek

PSL

onPlayerRequestClass(getPlayer(v.ID), getPlayer(v.ID).Class, getPlayer(v.ID).Team, getPlayer(v.ID).Skin)
v does not exist
Thank you very much for can it be repaired?

2b2ttianxiu


2b2ttianxiu

Quote from: PSL on Jul 09, 2023, 07:35 AMonPlayerRequestClass(getPlayer(v.ID), getPlayer(v.ID).Class, getPlayer(v.ID).Team, getPlayer(v.ID).Skin)
v does not exist
Thank you very much for can it be repaired?
Thx, PSL

PSL

You're welcome. I'll retest it. Thanks for the fix