Dear forum users! In compliance with the new European GDPR regulations, we'd just like to inform you that if you have an account, your email address is stored in our database. We do not share your information with third parties, and your email address and password are encrypted for security reasons.

New to the forum? Say hello in this topic! Also make sure to read the rules.

[Guide] Making a Universal weapon spawn system script w. String

Share questions, scripts and tutorials related to the ScriptAPI in SFD.
Forum rules
By using the forum you agree to the following rules.
Post Reply
User avatar
Mighty Spirit the 2
Superfighter
Superfighter
Posts: 180
Joined: Mon Jun 25, 2018 5:02 pm
Title: Wasted potential
SFD Account: ake004
SFD Alias: Retired SFD player
Started SFD: When melee was good
Location: SFD Veteran trauma hospital
Gender:
Age: 21

[Guide] Making a Universal weapon spawn system script w. String

Post by Mighty Spirit the 2 » Sun Dec 11, 2022 3:58 am

Blacklisting/Whitelisting weapons via. String

This is a bit more complex script, but it is useable in older and the current version of the game. My attempt at a universally cross compatible script that could theoretically still be updated for the future.

This whole thing started as an experiment, after my SupplyCrate [SC] script turned out to be a failure I began to think how people had regulated the weapon spawn system in the past. If you look at my old script I would get the current weapon item [wp_i] inside a SC and remove that from the game if it was a Bazooka or another OP weapon. Now the ability to read wp_i inside a SC, was only added from Beta 1.0.2c onwards, so I knew they must have done something different back then. I remembered that while looking through some ancient scripts I saw that they would create a string list for removing every Iobject that was found in the Game world [foreach( Iobject Delete in Game.GetObjectsByName(listName) ) Delete.Remove();] for example to remove all makeshifts from maps.

It was easy enough to find and remove wps, but how about replacing them with something else, else my SC would just contain nothing but air. Okay, just make a list of Wpns that you can replace them with, so fast that they player would not even be able to notice that a Bazooka was replaced with a Pistol for example.

The next challenge was writing the script so It could work with many versions. I knew I could not include terms like {WeaponItem.WHIP or WeaponItem.SHURIKEN} in the script or else it would crash (as older versions can't recognize them). So I thought of using String, since that works regardless so {WpnWhip, WpnShuriken….}. But the problem was then if Pre-Alpha 1.8.4 would try to create [Game.CreateObject("WpnShuriken");], which would obviously not work and create a "error" block. And thus I came up with my other script, "Remove error block". Which I would also recommend for others to use in their Object creation scripts. [Guide] Removing the "error" block w. Script

So I came up with the idea of scanning what enums of wp_i where available for the specific game version the script was being run in, right then and there. Then take those enums and place them in another list with "Wpn" included so you could create physical objects with the list, that wouldn't result in "error".
I also wanted to include a Blacklist and Whitelist function.
Blacklist = Weapons that aren't permitted and will be replaced with any other wpns not on the Blacklist.
Whitelist = The only weapons that are permitted. Any other weapon will be replaced with one from this list.

After a lot of fails and finally reading myself up on how to properly use lists and switch functions and ignore upper lower case (Thank God for the internet), I knew how I could code my idea into txt.
I had to return to using timer trigger because it's for very old versions of the game.
At the very end I decided to try and throw in a quick CheckPlayer inventory as well.
I had to create a Makeshift list, to remove them from Available_enums list.

The script was written and compiled in the Steam map editor and was working 100% fine. When I went to test my script in Pre-Alpha 1.8.4 it contained errors. A lot of the list options I had used didn't work. I had to find a workaround to that which took longer, but now it is confirmed to work.

At the end I am very happy with how it turned out. Take a look at it if you like.
Note, this script is very obsolete for using solely for Steam as you can use Dictionary short to update weapon spawn chances, the script was merely intended as a demonstration.

Code: Select all

/*
* author: Mighty Spirit The 2.
* modes: Versus
* description: Script to implement a custom universal WeaponSpawnSystem for SFD versions Pre-Steam (2015) -> to today and the FuTuRe.
*/

/*Below is where you write which Objects you want the game to look out for.
Find out the names of the Weapon in ScriptAPI. I have added in obvious false ones to check if it works.
*/
String[] Custom_List = {

    "CARBINE",
    "TOMMYGUN",
    "SMG",
    "SHOTGUN",
    "SAWED_OFF",
    "ASSAULT",
    //"M60",
"KATANA",
    "SNIPER",
"BAZOOKA",
"KATANA",
"Whip",
"WHIP",
"Chainsaw",
"Shuriken",

    "GRENADE_LAUNCHER",
    "PISTOL45",
    "SILENCEDPISTOL",
    "REVOLVER",
    "FLAREGUN",
    "UZI",
    "SILENCEDUZI",
    "MAGNUM",
"GRENADES",
"LAZER"

                                                  };

String[] MakeShifts = { "CHAIR", "CHAIR_LEG", "BOTTLE", "BROKEN_BOTTLE", "CUESTICK", "CUESTICK_SHAFT", "SUITCASE", "PILLOW", "FLAGPOLE", "TEAPOT", "TRASHCAN_LID", "TRASH_BAG", "BASEBALL" };

bool Blacklist_On = false;
bool Whitelist_On = true;
bool CheckPlayerInventory = true;
Random rnd = new Random();

List <String> Available_enums = new List<String>();
List <String> WPNs_ToSpawn = new List<String>();


    

public void OnStartup()
{
//foreach(IObject Wpn_Item in Game.GetObjectsByArea(Game.GetCameraArea()) ) { Game.RunCommand("/MSG " + Wpn_Item.Name); }  -> Test stage.


if(Whitelist_On) { Blacklist_On=false;	} //Only 1 list active at a time

foreach (WeaponItem e in Enum.GetValues(typeof(WeaponItem))) { Available_enums.Add(e.ToString()); }

List<string> WhitelistBaby = new List<String>();

foreach(String c in Custom_List)
if(Available_enums.Contains(c) ) { WhitelistBaby.Add(c); }

//**********Note, none of the options below worked for Pre-Alpha 1.8.4, had to replace them.
//[var WhitelistBaby = Custom_List.Where(i => Available_enums.Contains(i)).ToList(); or List<string> WhitelistBaby = Available_enums.Except(Custom_List).ToList();]



if(WhitelistBaby.Count>0) {
//Game.RunCommand("/MSG " + WhitelistBaby.Count);

if(Whitelist_On) {
Available_enums.Clear();
Available_enums.AddRange(WhitelistBaby);
Success();
//Game.RunCommand("/MSG " + "Success Whitelist");
}
if(Blacklist_On) {
//var BlacklistBaby = Custom_List.Where(i => !Available_enums.Contains(i)).ToList();  -> See above.

foreach(String R in WhitelistBaby)
Available_enums.Remove(R);
foreach(String MKS in MakeShifts) //You don't want to look for makeshifts.
Available_enums.Remove(MKS);
Success();
//Game.RunCommand("/MSG " + "Success Blacklist");

}}
}

//***************This is where we find exceptions to weapons and physical Game WPN-Objects and add them so they can spawn.
public void Success() { 
if(CheckPlayerInventory==true) { Available_enums.Add("NONE"); } 	//Adds exception if player inventory is empty.
for (int i = Available_enums.Count-1;i>=0;i--) {

String AV_e = Available_enums[i];
//DON'T EDIT ANYTHING BELOW, except if you need to in the Future.... ;)
switch(AV_e)
{
case "BAZOOKA":
//Game.CreateObject("Crate01"); Game.CreateObject("WpnBAZOOKA");
WPNs_ToSpawn.Add("WpnBazooka"); break;
case "NONE": break;
case "C4DETONATOR": break;
case "ASSAULT": WPNs_ToSpawn.Add("WpnAssaultRifle"); break;
case "GRENADE_LAUNCHER": WPNs_ToSpawn.Add("WpnGrenadeLauncher"); break;
case "SHOTGUN": WPNs_ToSpawn.Add("WpnPumpShotgun"); break;
case "SAWED_OFF": WPNs_ToSpawn.Add("WpnSawedOff"); break;
case "SNIPER": WPNs_ToSpawn.Add("WpnSniperRifle"); break;
case "PIPE": WPNs_ToSpawn.Add("WpnPipeWrench"); break;
case "SMG": case "SUB_MACHINEGUN":  WPNs_ToSpawn.Add("WpnSMG"); break;
case "SHOCK_BATON": WPNs_ToSpawn.Add("WpnShockBaton"); break;
case "MACHINE_PISTOL": WPNs_ToSpawn.Add("WpnPipeWrench"); break;
case "LEAD_PIPE": WPNs_ToSpawn.Add("WpnLeadPipe"); break;
case "FIREAMMO": WPNs_ToSpawn.Add("ItemFireAmmo"); break;
case "BOUNCINGAMMO": WPNs_ToSpawn.Add("ItemBouncingAmmo"); break;
case "STRENGTHBOOST": WPNs_ToSpawn.Add("ItemStrengthBoost"); break;
case "SPEEDBOOST": WPNs_ToSpawn.Add("ItemSpeedBoost"); break;
case "SLOWMO_5": WPNs_ToSpawn.Add("ItemSlomo5"); break;
case "SLOWMO_10": WPNs_ToSpawn.Add("ItemSlomo10"); break;
case "PILLS": WPNs_ToSpawn.Add("ItemPills"); break;
case "MEDKIT": WPNs_ToSpawn.Add("ItemMedkit"); break;
case "LAZER": WPNs_ToSpawn.Add("ItemLaserSight"); break;
case "STREETSWEEPER": WPNs_ToSpawn.Add("StreetSweeperCrate"); break;
default: 
WPNs_ToSpawn.Add("Wpn"+AV_e);  //Game can create CapsLocked wpns.
break;

}
	}
    IObjectTimerTrigger UniversalWpnList_Timer = (IObjectTimerTrigger)Game.CreateObject("TimerTrigger");   //Use TimerTrigger like the oldest scripts
    UniversalWpnList_Timer.SetIntervalTime(0);
    UniversalWpnList_Timer.SetRepeatCount(0);
    UniversalWpnList_Timer.SetScriptMethod("CheckWorld");
    UniversalWpnList_Timer.Trigger();
//Game.RunCommand("/MSG " + "Wpnstospawn =" + WPNs_ToSpawn.Count + "  " + WPNs_ToSpawn[0] +  "          CustomList =" + Custom_List.Length + "        AV_e= " + Available_enums.Count + "       " + Available_enums[4] ); //
WPNs_ToSpawn = WPNs_ToSpawn.ConvertAll(d => d.ToUpper());	//Had to convert everything to UpperCase to ignore case sensitivity later on. StringComparer.OrdinalIgnoreCase didn't work.
	}



//***********This is what monitors the in-game world and replaces weapons with those from list.
public void CheckWorld(TriggerArgs args) { 
if(WPNs_ToSpawn.Count>0) {
foreach(IObject Wpn_Item in Game.GetObjectsByArea(Game.GetCameraArea()) ) {
if(Wpn_Item.Name.Contains("Thrown") || Wpn_Item.Name=="WpnGrenadePin" || Wpn_Item.Name=="WpnC4Detonator") { Wpn_Item.CustomID="Exception";   } //Here we write the "Wpn" Exceptions...

if( Wpn_Item.Name.Contains("Wpn") || Wpn_Item.Name.Contains("Item") ){
if(!WPNs_ToSpawn.Contains(Wpn_Item.Name.ToUpper()) && Wpn_Item.CustomID==String.Empty ) {
Game.CreateObject(WPNs_ToSpawn[rnd.Next(WPNs_ToSpawn.Count)], Wpn_Item.GetWorldPosition(), 0 ); Wpn_Item.Remove(); /*Game.PlayEffect("CFTXT", new Vector2(10,20), Wpn_Item.Name); used for debug*/ } } 
if(Wpn_Item.Name=="error") { Wpn_Item.Remove(); } //Failsafe in case of creating a non-existend IObject.
}}


//CheckPlayerItems
if(CheckPlayerInventory==true) {
foreach(IPlayer ply in Game.GetPlayers()){
if(ply !=null || !ply.IsRemoved) {
	Vector2 Here = ply.GetWorldPosition();
	String MSG = "RemovedWpn";
if(!Available_enums.Contains(ply.CurrentPrimaryWeapon.WeaponItem.ToString()) ) { Game.PlayEffect("CFTXT", Here, MSG); ply.RemoveWeaponItemType(WeaponItemType.Rifle); } 
if(!Available_enums.Contains(ply.CurrentSecondaryWeapon.WeaponItem.ToString()) ) { Game.PlayEffect("CFTXT", Here, MSG); ply.RemoveWeaponItemType(WeaponItemType.Handgun); } 
if(!Available_enums.Contains(ply.CurrentMeleeWeapon.WeaponItem.ToString()) ) { Game.PlayEffect("CFTXT", Here, MSG); ply.RemoveWeaponItemType(WeaponItemType.Melee); } 
if(!Available_enums.Contains(ply.CurrentThrownItem.WeaponItem.ToString()) ) { Game.PlayEffect("CFTXT", Here, MSG); ply.RemoveWeaponItemType(WeaponItemType.Thrown); } 
if(!Available_enums.Contains(ply.CurrentPowerupItem.WeaponItem.ToString()) ) { Game.PlayEffect("CFTXT", Here, MSG); ply.RemoveWeaponItemType(WeaponItemType.Powerup); } 
}}}
//Game.PlayEffect("CFTXT", new Vector2(0,0), "One cycle past"); Game.PlayEffect("Electric", new Vector2(0,30), "One cycle past");  -> For testing.
	}

0 x
🎶I will tell your story if you die
I will tell your story and keep you alive the best i can
...
But I've always had the feeling we would die young
Some die young
🎵

Post Reply