[solved] Weapons in lua
Moderator: Moderators
-
- Private Recruit
- Posts: 16
- Joined: Wed Jul 22, 2020 4:25 pm
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
[solved] Weapons in lua
Is there a SetClassProperty equivalent for weapons? I read somewhere that you could use SetProperty to change the com versions of weapons (though my own attempts have been unsuccessful) but is there a way to modify a specific weapon with mission scripts? Also, I've been trying to use SetClassProperty to remove a weapon by setting the WeaponName4 to "-" because I was looking in the assets and I saw that the devs used that to make values empty or something. What do I actually do to accomplish that?
Last edited by JustSomeGuy on Mon Oct 05, 2020 11:15 pm, edited 1 time in total.
-
- Corporal
- Posts: 151
- Joined: Thu Jan 24, 2019 11:02 pm
- Projects :: No Mod project currently
- xbox live or psn: No gamertag set
Re: Weapons in lua
Are you trying to modify a weapon or remove it? And are you trying to make all these changes before the start of the mission, or on the go?
I think SetClassProperty() works for modifying weapons, but you put in the name of the weapon odf to change a weapon's properties. That would modify the weapons for any units which haven't spawned yet (change the class at the spawn screen) but it doesn't change the weapons for units which are already spawned/alive. To swap weapons, then you might be able to use SetClassProperty() with the character class and "WeaponName1", "WeaponAmmo1", "WeaponChannel1" etc. SetProperty() is designed for units which are spawned, but I don't know how you would use it on a weapon, because weapons don't have instances (reference) and SetProperty() is supposed to take userdata. I'm not sure how you find a spawned weapon's userdata, if it even has any.
The easiest way to change/add/remove weapons is just to make a new unit properly, with a side mod. If you're in the mission lua anyway, then it's the normal way to do things. Though if you're doing something like a custom user script, and aren't changing the mission lua directly, then you could hack code into AddUnitClass() and do some trickery with that.
If you want to change weapons on the go, there was someone who made a Gun Game mod a while back and I think I remember it changing your character model and weapon loadouts while you were playing (ie without forcing you to respawn). That suggests that it's possible but I have no idea how it was made.
Edit: Does it not work when you use SetClassProperty("Put the character class name here", "WeaponName4", "")? Same question for SetProperty(charUnit, "WeaponName4", ""), though in that case I half expect the weapon to only disappear when you switch away and try to switch back, like when you lock weapons for awards. Or maybe it could crash. The empty string might be important in case it tries to read "-" as -.odf
Edit2: Also, in that reference post above, Zerted pointed out that changing some weapon properties crashes multiplayer. It really depends on if you need to figure out how to make changes mid mission. Otherwise you should just make a side mod.
I think SetClassProperty() works for modifying weapons, but you put in the name of the weapon odf to change a weapon's properties. That would modify the weapons for any units which haven't spawned yet (change the class at the spawn screen) but it doesn't change the weapons for units which are already spawned/alive. To swap weapons, then you might be able to use SetClassProperty() with the character class and "WeaponName1", "WeaponAmmo1", "WeaponChannel1" etc. SetProperty() is designed for units which are spawned, but I don't know how you would use it on a weapon, because weapons don't have instances (reference) and SetProperty() is supposed to take userdata. I'm not sure how you find a spawned weapon's userdata, if it even has any.
The easiest way to change/add/remove weapons is just to make a new unit properly, with a side mod. If you're in the mission lua anyway, then it's the normal way to do things. Though if you're doing something like a custom user script, and aren't changing the mission lua directly, then you could hack code into AddUnitClass() and do some trickery with that.
If you want to change weapons on the go, there was someone who made a Gun Game mod a while back and I think I remember it changing your character model and weapon loadouts while you were playing (ie without forcing you to respawn). That suggests that it's possible but I have no idea how it was made.
Edit: Does it not work when you use SetClassProperty("Put the character class name here", "WeaponName4", "")? Same question for SetProperty(charUnit, "WeaponName4", ""), though in that case I half expect the weapon to only disappear when you switch away and try to switch back, like when you lock weapons for awards. Or maybe it could crash. The empty string might be important in case it tries to read "-" as -.odf
Edit2: Also, in that reference post above, Zerted pointed out that changing some weapon properties crashes multiplayer. It really depends on if you need to figure out how to make changes mid mission. Otherwise you should just make a side mod.
-
- Private Recruit
- Posts: 16
- Joined: Wed Jul 22, 2020 4:25 pm
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
Re: Weapons in lua
"Are you trying to modify a weapon or remove it?"
Both
"And are you trying to make all these changes before the start of the mission, or on the go?"
before the start
"I think SetClassProperty() works for modifying weapons, but you put in the name of the weapon odf to change a weapon's properties."
In my testing, that doesn't work. Nothing happens to the weapon
" Does it not work when you use SetClassProperty("Put the character class name here", "WeaponName4", "")? Same question for SetProperty(charUnit, "WeaponName4", "")"
I tried that already, and it crashed the game
Both
"And are you trying to make all these changes before the start of the mission, or on the go?"
before the start
"I think SetClassProperty() works for modifying weapons, but you put in the name of the weapon odf to change a weapon's properties."
In my testing, that doesn't work. Nothing happens to the weapon
" Does it not work when you use SetClassProperty("Put the character class name here", "WeaponName4", "")? Same question for SetProperty(charUnit, "WeaponName4", "")"
I tried that already, and it crashed the game
- Maveritchell
- Jedi Admin
- Posts: 7366
- Joined: Mon Aug 21, 2006 11:03 pm
Re: Weapons in lua
WeaponName# only works when the .odf you're modifying uses the same schema. If your unit .odf uses WeaponName (without a number), you won't be able to modify it. You also can't replace a filled weapon slot with no weapon; you need to make a "dummy" weapon (a weapon that does nothing) and swap that in when you want to remove a weapon.
You can't modify a weapon .odf itself in any meaningful way in the game. The only way you could possibly change what a weapon does outside of swapping the weapon entirely would be to have all the effects of the weapon be scripted (i.e. a weapon that fires a dummy ordnance that triggers a script effect). Depending on the weapon you're talking about, you might be able to make a reasonable facsimile of a "normal" weapon in the script (i.e. you could replicate a deployable turret weapon easily), but most normal weapons you couldn't replicate (i.e. you couldn't really replicate a gun).
You can't modify a weapon .odf itself in any meaningful way in the game. The only way you could possibly change what a weapon does outside of swapping the weapon entirely would be to have all the effects of the weapon be scripted (i.e. a weapon that fires a dummy ordnance that triggers a script effect). Depending on the weapon you're talking about, you might be able to make a reasonable facsimile of a "normal" weapon in the script (i.e. you could replicate a deployable turret weapon easily), but most normal weapons you couldn't replicate (i.e. you couldn't really replicate a gun).
-
- Private Recruit
- Posts: 16
- Joined: Wed Jul 22, 2020 4:25 pm
- Projects :: No Mod project currently.
- xbox live or psn: No gamertag set
Re: Weapons in lua
ok, good to know