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.

Converting WeaponItem to WeaponItem object

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
phantom
Fighter
Fighter
Posts: 16
Joined: Sat Oct 22, 2022 2:36 pm
SFD Account: Phantom
Gender:
Age: 24
Contact:

Converting WeaponItem to WeaponItem object

Post by phantom » Tue Nov 07, 2023 2:12 pm

Edit to save your time: I need IObjectWeaponItem equivalent to WeaponItem I have.

So I am trying to make a mutant script in which I don't want it to equip any weapon.
Well take a look at this you will understand:

Code: Select all

      Events.PlayerWeaponAddedActionCallback.Start((IPlayer player, PlayerWeaponAddedArg arg) => {
      if (player != mutant) return;
      Game.ShowChatMessage(String.Format("{0}",arg.WeaponItem));
      player.RemoveWeaponItemType(arg.WeaponItemType);
   });
The problem here is I will create an object using Game.CreateObject() which takes a string and it's value is different.
E.g: arg.WeaponItem = "M60" (If I will convert it to string) and I need "WpnM60" for createobject func.
Just adding a Wpn before WeaponItem doesn't solves the problem either as WeaponItem "Rifle" == "WpnAssaultRifle"and "Sniper" == "WpnSniperRifle" and differences so on.

Sorry forgot to explain the actual purpose of this script block, so as soon as the mutant pick up any weapon it get's remove by his inventory and get's replaced at the mutant position (I want help in the replacing part)

Any suggestion for a different approach are also appreciated :)
0 x

User avatar
Mighty Spirit the 2
Superfighter
Superfighter
Posts: 188
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: 23

Post by Mighty Spirit the 2 » Mon Nov 20, 2023 4:56 am

Hi, sorry for the later reply, i don't know if you have figured this out yet....
I can contribute with very little, I ran into the exact same problem that you find yourself in, when i was trying to make my universal weapon spawn system script. I needed a way to make a script WeaponItem into the physical weapon item for the game world. I made a very hard process, you can see it here if you haven't yet (it was on forums for a long time): [Guide] Making a Universal weapon spawn system script w. String
I wrote quite extensively how the script works and what it does, I tried to comment it as good as possible, it was my first script that i wrote 100% alone... Maybe you can understand it. You just have to create a list that has all the string format of the WeaponItems. Call this list something like "Replacement_weapons". Basically i had to write all the "translations" down manually.

Another thing I started thinking of is using a IObjectOnDestroyedTrigger to monitor each in game "Wpn..." item, and then when it is taken by a player it simply respawns the weapon where it was taken, and re-adds (AddTriggerDestroyObject) the newly created weapon into it's list (GetTriggerDestroyObjects), but only if it can register that the mutant player is the one who picked up the weapon. However it doesn't seem to work using OnDestroyedTrigger, at least for Beta 1.0.2c I can't use AddTriggerDestroyObject, it just ignores the added object, doesn't trigger again.

I checked the API once more, maybe you can use the Events.PlayerWeaponAddedActionCallback to check what mutant player has picked up, and then respawn wpn via a "Replacement_weapons" list. Or even easier just make a IObject list and call it "CurrentWpns" and add all "Wpn..." that are spawned into the game, either with Game.GetObjectsbyArea(Game.GetCameraArea() ), or with Events.ObjectCreatedCallback. Now when Mutant picks up the weapon, check with Events.PlayerWeaponAddedActionCallback and get the UniqueID of the wpn. Now check the list "CurrentWpns" and see if the UniqueID matches one there, if yes spawn a new Weapon via the Name of the stored Object close to the mutant, then remove the old Object from list. Also check list regularly to remove all Wpns that other players pick up.
But this is just some ideas for you to try if you are stuck, I haven't checked if this works because it's late and I have no longer got the steam version installed.

But if you can find a good method to make it function, please come back and write it down in a guide.
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
🎵
https://i.imgur.com/D479VLi.png

phantom
Fighter
Fighter
Posts: 16
Joined: Sat Oct 22, 2022 2:36 pm
SFD Account: Phantom
Gender:
Age: 24
Contact:

Post by phantom » Mon Nov 20, 2023 2:25 pm

Well looping through the enums is a way.. but I thought there might be another way as this would run maybe a 100 or so loops when the mutant pickup a weapon I thought of this way earlier but didn't proceeded to do so. Thanks tho I can use your script :)
0 x

User avatar
Odex64
Superfighter
Superfighter
Posts: 173
Joined: Sat Jul 29, 2017 12:39 pm
Title: Content Creator
SFD Account: Odex64
Started SFD: PreAlpha
Location: Italy
Gender:
Age: 22

Post by Odex64 » Mon Nov 20, 2023 3:24 pm

A better way is to just store all the enums and the corresponding weapon's name in a dictionary (key/value pair).
Then you can access the correct weapon name with your enum value, easy as that.

If you can't figure it out I will post a snippet here.
1 x
Image

User avatar
Mighty Spirit the 2
Superfighter
Superfighter
Posts: 188
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: 23

Post by Mighty Spirit the 2 » Tue Nov 21, 2023 3:34 am

Dammit, I must have been very tired yesterday... My suggestions above are so complex, that's typical of me when I should try and find the easiest solutions.

Today I thought that an even better way to make your script work. Technically you only needed help for the replacing weapons of the mutant players inventory part, and didn't really need to translate WeaponItem to Wpn-Tiles.
Well first of I think you can try to use the IPlayer.Disarm (WeaponItemType weaponType, Vector2 velocity), to disarm the mutant every time he tries to pick something up via the Events.PlayerWeaponAddedActionCallback. I tried to use the Disarm function in 2021, and didn't like that the weapon goes flying away. Back then I didn't know you could manipulate the velocity of the disarmed weapon (at least the documentation seems to suggest this). I think you already know how to use this method. Thought I don't know how this will look like if the mutant picks up a weapon while rolling...
This would be the absolute best method. Why? Because if you just spawn a brand new weapon into the game, you will run into the problem of infinite ammo. Mutant picks up Magnum with 1 shot left, now there's a new magnum with full magazine.

So another method/fix is to just give the mutant a new weapon of the same category so he is forced to drop the original weapon. Now remove that weapon from his inventory. The only drawback with this is that it can look kind of ugly. This is something I already tried doing in a previous script, so that's why i knew this method, i just didn't remember it yesterday. The trick is to conceal the temporary weapon by giving a weapon with the shortest possible name, like Uzi or C4. However you can't hide these weapons as well, because there aren't any other weapons with short names to conceal them. Baton and Chain are perfect to conceal each other for example.
Here is some script i wrote you can see what i mean.

Code: Select all

	//Monitor Mutant inventory here
		if(Mutant.CurrentPrimaryWeapon.WeaponItem.ToString()!="NONE"){
			switch(Mutant.CurrentPrimaryWeapon.WeaponItem.ToString() ){
/*				For Beta 1.0.2c
				case "M60":
					Mutant.GiveWeaponItem(WeaponItem.CARBINE);
					Mutant.RemoveWeaponItemType(WeaponItemType.Rifle);
					Mutant.GiveWeaponItem(WeaponItem.M60);
					Mutant.RemoveWeaponItemType(WeaponItemType.Rifle);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
*/
				case "BAZOOKA":
					Mutant.GiveWeaponItem(WeaponItem.CARBINE);
					Mutant.RemoveWeaponItemType(WeaponItemType.Rifle);
					Mutant.GiveWeaponItem(WeaponItem.BAZOOKA);
					Mutant.RemoveWeaponItemType(WeaponItemType.Rifle);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
				default:
					WeaponItem CurrentWeapon = Mutant.CurrentPrimaryWeapon.WeaponItem;
					Mutant.GiveWeaponItem(WeaponItem.BAZOOKA);
					Mutant.RemoveWeaponItemType(WeaponItemType.Rifle);
					Mutant.GiveWeaponItem(CurrentWeapon);
					Mutant.RemoveWeaponItemType(WeaponItemType.Rifle);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
			}
		}

		if(Mutant.CurrentSecondaryWeapon.WeaponItem.ToString()!="NONE"){
			switch(Mutant.CurrentSecondaryWeapon.WeaponItem.ToString() ){
				case "UZI":
					Mutant.GiveWeaponItem(WeaponItem.PISTOL);
					Mutant.RemoveWeaponItemType(WeaponItemType.Handgun);
					Mutant.GiveWeaponItem(WeaponItem.UZI);
					Mutant.RemoveWeaponItemType(WeaponItemType.Handgun);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
				default:
					WeaponItem CurrentWeapon = Mutant.CurrentSecondaryWeapon.WeaponItem;
					Mutant.GiveWeaponItem(WeaponItem.UZI);
					Mutant.RemoveWeaponItemType(WeaponItemType.Handgun);
					Mutant.GiveWeaponItem(CurrentWeapon);
					Mutant.RemoveWeaponItemType(WeaponItemType.Handgun);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
			}
		}

		if(Mutant.CurrentMeleeWeapon.WeaponItem.ToString()!="NONE"){
			switch(Mutant.CurrentMeleeWeapon.WeaponItem.ToString() ){
				case "CHAIN":
					Mutant.GiveWeaponItem(WeaponItem.BATON);
					Mutant.RemoveWeaponItemType(WeaponItemType.Melee);
					Mutant.GiveWeaponItem(WeaponItem.CHAIN);
					Mutant.RemoveWeaponItemType(WeaponItemType.Melee);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
				default:
					WeaponItem CurrentWeapon = Mutant.CurrentMeleeWeapon.WeaponItem;
					Mutant.GiveWeaponItem(WeaponItem.CHAIN);
					Mutant.RemoveWeaponItemType(WeaponItemType.Melee);
					Mutant.GiveWeaponItem(CurrentWeapon);
					Mutant.RemoveWeaponItemType(WeaponItemType.Melee);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
			}
		}

		if(Mutant.CurrentThrownItem.WeaponItem.ToString()!="NONE"){
			switch(Mutant.CurrentThrownItem.WeaponItem.ToString() ){
				case "C4": //case "C4DETONATOR":
					Mutant.GiveWeaponItem(WeaponItem.MINES);
					Mutant.RemoveWeaponItemType(WeaponItemType.Thrown);
					Mutant.GiveWeaponItem(WeaponItem.C4);
					Mutant.RemoveWeaponItemType(WeaponItemType.Thrown);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
				default:
					WeaponItem CurrentWeapon = Mutant.CurrentThrownItem.WeaponItem;
					Mutant.GiveWeaponItem(WeaponItem.C4);
					Mutant.RemoveWeaponItemType(WeaponItemType.Thrown);
					Mutant.GiveWeaponItem(CurrentWeapon);
					Mutant.RemoveWeaponItemType(WeaponItemType.Thrown);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
			}
		}

		if(Mutant.CurrentPowerupItem.WeaponItem.ToString()!="NONE"){
			switch(Mutant.CurrentPowerupItem.WeaponItem.ToString() ){
				default:
					WeaponItem CurrentWeapon = Mutant.CurrentPowerupItem.WeaponItem;
					Mutant.GiveWeaponItem(CurrentWeapon);
					Mutant.RemoveWeaponItemType(WeaponItemType.Powerup);
					Game.PlayEffect("CFTXT", Mutant.GetWorldPosition() + new Vector2(10,5) , "Detected");
				break;
			}
		}

Here's a demonstration

@Odex64 Yes, at first i also thought that maybe it was possible to write it with Dictionary method, but i don't know how to do it (or how it works/logic behind it), and I assumed he also can't because he hadn't done so.
But if you know how to do it and have the time, you could write a Guide because such a translation can turn out to be useful for future Scripters.
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
🎵
https://i.imgur.com/D479VLi.png

phantom
Fighter
Fighter
Posts: 16
Joined: Sat Oct 22, 2022 2:36 pm
SFD Account: Phantom
Gender:
Age: 24
Contact:

Post by phantom » Fri Dec 29, 2023 2:29 pm

Sorry for a month late reply, I tried the @Odex64 method of using dictionary but it had just became a mess so I was demotivatived from that then I simply add a effect and completely remove the weapon from inventory and from world and made it look like mutant has crushed the weapon and I am happy how it turned out :)

Also, @Odex64 I would love to see the dictionary method, just wanna learn more about c# :)
0 x

User avatar
Odex64
Superfighter
Superfighter
Posts: 173
Joined: Sat Jul 29, 2017 12:39 pm
Title: Content Creator
SFD Account: Odex64
Started SFD: PreAlpha
Location: Italy
Gender:
Age: 22

Post by Odex64 » Sat Dec 30, 2023 1:39 pm

Ignore my previous message, you just need this line of code in your event.

Code: Select all

Game.SpawnWeaponItem(arg.WeaponItem, mutant.GetWorldPosition());
0 x
Image

phantom
Fighter
Fighter
Posts: 16
Joined: Sat Oct 22, 2022 2:36 pm
SFD Account: Phantom
Gender:
Age: 24
Contact:

Post by phantom » Tue Jan 02, 2024 12:47 pm

Odex64 wrote:
Sat Dec 30, 2023 1:39 pm
Ignore my previous message, you just need this line of code in your event.

Code: Select all

Game.SpawnWeaponItem(arg.WeaponItem, mutant.GetWorldPosition());
Thanks mate, this works like a charm.
But why can't I find this method listed in the api?
0 x

User avatar
Odex64
Superfighter
Superfighter
Posts: 173
Joined: Sat Jul 29, 2017 12:39 pm
Title: Content Creator
SFD Account: Odex64
Started SFD: PreAlpha
Location: Italy
Gender:
Age: 22

Post by Odex64 » Tue Jan 02, 2024 3:09 pm

There is.
Image
0 x
Image

Post Reply