Jump to content

Problems with scripting


kublajkan

Recommended Posts

Hello!

 

I am working on a script for a mod, but i am stucked.

 

I would like to make a mod that makes iron sight adjustable (just 2 positions) long range and short range/cqc

 

the aproach is to make two diferent weapons, and switch them with a hotkey

 

the script should read the current health of the equipped weapon, unequip it, remove it, add the other version, equip it and modify the health to the stored value

 

the problem is i dont know how to script, and reading the tutorials, making trial and error attempts get a bit frustrating

 

so help me if you can :smile:

 

 

 

the script is an object script, and in this incarnation it does nothing in game

 

scn AdjustSightGalil556LRTOCQCScript
float WeaponHealth
short sToggle
float CurrentWeapon
short DoOnce
Begin OnEquip Player
set sToggle to 0
set DoOnce to 0
if IsKeyPressed 37
if sToggle == 0
set sToggle to 1
if DoOnce == 0
set WeaponHealth to player.GetEquippedCurrentHealth 5
set Currentweapon to player.GetEquippedObject 5
player.unequipitem CurrentWeapon
player.RemoveItem CurrentWeapon
player.AddItem WeapMikeGalilAR556CQC 1
player.equipitem WeapMikeGalilAR556CQC
player.SetEquippedCurrentHealth WeaponHealth 5
set DoOnce to -1
elseif DoOnce == -1
set WeaponHealth to player.GetEquippedCurrentHealth 5
set Currentweapon to player.GetEquippedObject 5
player.unequipitem Currentweapon
player.RemoveItem CurrentWeapon
player.AddItem WeapMikeGalilAR556CQC 1
player.equipitem WeapMikeGalilAR556CQC
player.SetEquippedCurrentHealth WeaponHealth 5
set DoOnce to 0
endif
endif
else
set sToggle to 0
endif
Return
End
Link to comment
Share on other sites

​I can see many problems with your approach but I'll begin with the most significant ones.

 

I've never used RH_Ironsights but, as far as I know, it achieves its goal through scripts, animations, and meshes. Am I right?

 

I can only help you with the scripts, which is what you're asking for. I just considered important pointing that out first because, if you can't manage to do everything else, it would be a waste of time.

​Now, the second important thing to mention is that, due to engine and FOSE limitations, a script would need to be way more complex and use, at the very least, formlists and other stuff to do what you want it to do. I already thought of many ways to do what you want to do but all of them require more than just one simple script and nothing else.

​Don't take me wrong, trying to discourage you is not my intention. :sweat: I just want to let you know beforehand that this does requires a lot more work than what you seem to expect.

 

It's a shame, really. It would be nicer if FOSE had many of the features that NVSE has. If so, doing things such as this would take less work. They would still take a lot of knowledge, though, but asking for help is always a possibility.

Link to comment
Share on other sites

Thx for the reply!

 

Well, the meshes and the Geck entries are already done and working, i just need the script.

 

In my limited understanding the script only needs to handle 2 "Ref" variable at a time, the ref of the current weapon, and the only one that it could be switch into. And the bloody health value :) So i dont see the need of formlists like in WMK for example. But if i am wrong plz explain.

Link to comment
Share on other sites

In my limited understanding the script only needs to handle 2 "Ref" variable at a time, the ref of the current weapon, and the only one that it could be switch into. And the bloody health value :smile: So i dont see the need of formlists like in WMK for example. But if i am wrong plz explain.

The problem is where you store those variables.

 

If you store them in a weapon script, they will be gone as soon as you remove the weapon from the player's inventory. Because of this, you need a quest script instead, and you need to set the quest delay of the quest to a small value like 0.05 or 0.1.

 

Then the script needs to know which weapon corresponds to each weapon. For that you will either need to manually set a ref variable in a weapon script for each weapon, which means that you would have to make a different script for each weapon, or you can make two formlists: one for the weapons in the standard position, and another one for the weapons in the custom position.

 

If you go for the formlists option, you need to ensure that the FormID's for each weapon are in the respective same index as their counterparts in the other formlist. Example:

 

Index	        Form 1			Form 2
0		​HuntingRifleFormID	HuntingRifleFormID2
1		​MagnumFormID		MagnumFormID2
2		AssaultRifleFormID	AssaultRifleFormID
When you have all of that set, add a quest script such as the following:

 

scn AdjustSightGalil556LRTOCQCScript

float weaponHealth
ref weaponREF
int keyPressed
int index

Begin GameMode

	if keyPressed == IsKeyPressed 37 ; Was the hotkey pressed or released?
		Return	;if it wasn't, trim CPU usage
	endif

	set keyPressed to IsKeyPressed 37

	if keyPressed ;key down
		set weaponREF to player.GetEqObj 5

		set index to ListGetFormIndex baseList weaponREF ;store the index of the weapon in the base list

		if index > -1 ; if the form exists in the base list
			set weaponHealth to player.GetWeaponHealthPerc ;store the current health of the weapon
			player.SetWeaponHealthPerc 1 ;change the current health of the weapon to 1% to ensure that the next function removes this particular weapon and not a more damaged version of the same weapon in the player's inventory (if any)
			player.RemoveItem weaponREF 1 1 ;remove the weapon from the inventory
			set weaponREF to ListGetNthForm secondList index ;set the ref variable to the weapon from the other formlist
			player.AddItem weaponREF 1 1 ;add the weapon
			player.EquipItem weaponREF 0 1 ;equip the weapon
			player.SetWeaponHealthPerc weaponHealth ;set the weapon to the correct health percentage
			Return ;trim CPU usage
		endif

		;if the script reaches this point it means the weapon is not in the base list so...
		set index to ListGetFormIndex secondList weaponREF ;store the index of the weapon in the second list

		if index > -1 ; if the form exists in the second list
			set weaponHealth to player.GetWeaponHealthPerc
			player.SetWeaponHealthPerc 1
			player.RemoveItem weaponREF 1 1
			set weaponREF to ListGetNthForm baseList index ;set the ref variable to the weapon from the other formlist
			player.AddItem weaponREF 1 1
			player.EquipItem weaponREF 0 1
			player.SetWeaponHealthPerc weaponHealth
		endif
	endif

End

​I hope that helps :smile: If you run into any problems let me know. Also, if you intend to publish this mod remember to list FOSE as a requirement.

 

EDIT: I should also add that there are other possible approaches that are more similar to what you first tried. So, it's not that you were THAT far off the target, but the solution I provided is the best for compatibility and performance reasons, and it saves you the hassle of manually making many different scripts for each weapon, because through this method you only need a plugin with: 1 quest, 1 quest script, 2 formlists and the records of the new weapons.

Edited by claustromaniac
Link to comment
Share on other sites

Thank you for your help!

 

But it freezes. I feel a little embarassed...

 

So i made 2 form lists

 

IronSightAdjustFormlist01

IronSightAdjustFormlist02

 

containing 2+2 weapons (normLongRange, silencedLongRange+normCQC, silencedCQC)

 

One quest

 

ID:IronSightAdjustQuestAA

priority:0

Start game enabled:true

script prcessing delay:0.1

script:AdjustSightQuestScript

 

and the script

 

scn AdjustSightQuestScript
float weaponHealth
ref weaponREF
int keyPressed
int index
Begin GameMode
if keyPressed == IsKeyPressed 37 ; Was the hotkey pressed or released?
Return ;if it wasn't, trim CPU usage
endif
set keyPressed to IsKeyPressed 37
if keyPressed ;key down
set weaponREF to player.GetEqObj 5
set index to ListGetFormIndex IronSightAdjustFormlist01 weaponREF ;store the index of the weapon in the base list
if index > -1 ; if the form exists in the base list
set weaponHealth to player.GetWeaponHealthPerc ;store the current health of the weapon
player.SetWeaponHealthPerc 1 ;change the current health of the weapon to 1% to ensure that the next function removes this particular weapon and not a more damaged version of the same weapon in the player's inventory (if any)
player.RemoveItem weaponREF 1 1 ;remove the weapon from the inventory
set weaponREF to ListGetNthForm IronSightAdjustFormlist02 index ;set the ref variable to the weapon from the other formlist
player.AddItem weaponREF 1 1 ;add the weapon
player.EquipItem weaponREF 0 1 ;equip the weapon
player.SetWeaponHealthPerc weaponHealth ;set the weapon to the correct health percentage
Return ;trim CPU usage
endif
;if the script reaches this point it means the weapon is not in the base list so...
set index to ListGetFormIndex IronSightAdjustFormlist02 weaponREF ;store the index of the weapon in the second list
if index > -1 ; if the form exists in the second list
set weaponHealth to player.GetWeaponHealthPerc
player.SetWeaponHealthPerc 1
player.RemoveItem weaponREF 1 1
set weaponREF to ListGetNthForm IronSightAdjustFormlist01 index ;set the ref variable to the weapon from the other formlist
player.AddItem weaponREF 1 1
player.EquipItem weaponREF 0 1
player.SetWeaponHealthPerc weaponHealth
endif
endif
End
it ctd when i equip the weapon and push K
If you have any idea, i would be thankful :blush:
Link to comment
Share on other sites

OK I tracked down the issue after a few tests.

 

It seems the script didn't like me using that many SetWeaponHealthPercent funcions at once in a single frame. Setting the temporary health percentage to 10 instead of 1 also helped.

 

Here's the fixed version for you. Sorry I didn't make a working version the first time lol. That's what I get for not testing everything, which is unfortunately a bad habit of mine.

scn AdjustSightQuestScript

float weaponHealth
ref weaponREF
int keyPressed
int index
int updatehealth

Begin GameMode

	if keyPressed == IsKeyPressed 37 ; Was the hotkey pressed or released?
		Return ;if it wasn't, trim CPU usage
	endif

	set keyPressed to IsKeyPressed 37

	if keyPressed ;key down
		set weaponREF to playerREF.GetEqObj 5

		if IsFormValid weaponREF != 1
			Return ;the ref is not valid, trim CPU usage.
		endif

		set index to ListGetFormIndex IronSightAdjustFormlist01 weaponREF ;store the index of the weapon in the base list

		set weaponHealth to playerREF.GetWeaponHealthPerc ;store the current health of the weapon

		if index > -1 ; if the form exists in the base list
			playerREF.SetWeaponHealthPerc 10 ;change the current health of the weapon to 10% to ensure that the next function removes this particular weapon and not a more damaged version of the same weapon in the player's inventory (if any)
			playerREF.RemoveItem weaponREF 1 1 ;remove the weapon from the inventory
			set weaponREF to ListGetNthForm IronSightAdjustFormlist02 index ;set the ref variable to the weapon from the other formlist
			playerREF.AddItem weaponREF 1 1 ;add the weapon
			playerREF.EquipItem weaponREF 0 1 ;equip the weapon
			set updatehealth to 1
			Return ;trim CPU usage
		endif

		;if the script reaches this point it means the weapon is not in the base list so...
		set index to ListGetFormIndex IronSightAdjustFormlist02 weaponREF ;store the index of the weapon in the second list

		if index > -1 ; if the form exists in the second list
			playerREF.SetWeaponHealthPerc 10
			playerREF.RemoveItem weaponREF 1 1
			set weaponREF to ListGetNthForm IronSightAdjustFormlist01 index ;set the ref variable to the weapon from the other formlist
			playerREF.AddItem weaponREF 1 1 ;add the weapon
			playerREF.EquipItem weaponREF 0 1
			set updatehealth to 1
		endif
		Return
	else ;key up
		if updatehealth
			playerREF.SetWeaponHealthPerc weaponHealth ;set the weapon to the correct health percentage
			set updatehealth to 0
		endif
	endif

End
Link to comment
Share on other sites

Thank you!

 

I tested the second version of script, and it worked fine.

 

Along the way i made some other modification that ruined the whole Fallout 3 setup, so now i'm reinstaling :D

 

I hope i can try out the 3. version.

 

Again, thank you, you saved me a lot of trouble.

 

By the way, is it posible to skip the equip/unequip animation, and play the reload D (double barell shotgun) animation instead? That would make the illusion of actually tinkering with the rear sight.

Link to comment
Share on other sites

By the way, is it posible to skip the equip/unequip animation, and play the reload D (double barell shotgun) animation instead? That would make the illusion of actually tinkering with the rear sight.

That would be nice indeed. However, it's beyond my abilities. The only way I can think of is by replacing those animations. :pinch:

 

Anyway, glad to be of help. :cool:

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...