Scripting problem: RemoveItem

Not about OpenMW? Just about Morrowind in general? Have some random babble? Kindly direct it here.
Post Reply
erretel
Posts: 14
Joined: 29 Jan 2018, 23:20

Scripting problem: RemoveItem

Post by erretel »

I'm not sure if this go here or in the general discussion, since I'm not sure the problem is with OpenMW, Morrowind itself or just my lousy scripting.
Anyway, I'm trying to make an alternate Levelling Up mod, and it's working surprisingly well despite how surprisingly limited morrowind's scripting is. To put it quickly, you have those Experience Shards you can use on yourself to open a menu, from there you can spend them to increase whatever skill you want.
The issue is that I can't seem to be able to remove the Experience Shards themselves.
If I try to remove any amount from the script attached to the Experience Shards themselves it gets weird and buggy, which is kinda expected.
So I decide to use a global script. From the script attached to Experience Shard I'd try to activate a global script, which would then RemoveItem an amount (just 1 for testing) Experience Shard from the player inventory. If I do it this way, the global script fail to remove any ExpShrd at all and the ExpShrd themselves stop working (even if I load a previous save, I need to restart OpenMW).
Here are the scripts.

The one attached to Experience Shard

Code: Select all

Begin AAA_experience_shard_script

	short runOnce
	short OnPcEquip
	short costBase
	short costMulti
	short acrobaticsNow
	short acrobaticsCost
	short alchemyNow
	short alchemyCost
	short alterationNow
	short alterationCost
	short armorerNow
	short armorerCost
	short athleticsNow
	short athleticsCost
	short axeNow
	short axeCost
	short blockNow
	short blockCost
	short bluntweaponNow
	short bluntweaponCost
	short conjurationNow
	short conjurationCost
	short destructionNow
	short destructionCost
	short enchantNow
	short enchantCost
	short heavyarmorNow
	short heavyarmorCost
	short illusionNow
	short illusionCost
	short lightarmorNow
	short lightarmorCost
	short longbladeNow
	short longbladeCost
	short marksmanNow
	short marksmanCost
	short mediumarmorNow
	short mediumarmorCost
	short mercantileNow
	short mercantileCost
	short mysticismNow
	short mysticismCost
	short restorationNow
	short restorationCost
	short securityNow
	short securityCost
	short shortbladeNow
	short shortbladeCost
	short sneakNow
	short sneakCost
	short spearNow
	short spearCost
	short speechcraftNow
	short speechcraftCost
	short unarmoredNow
	short unarmoredCost

	short buttonPressed
	short buttonPressedDeeper
	short categoryChosen
	short skillMenu
	short skillChosen
	
	short shardAmount
	short shardsToRemove

	if ( runOnce == 0 )
		set runOnce to 1
		set costBase to 50
		set costMulti to 7
		set categoryChosen to -1
		set skillMenu to -1
		set skillChosen to -1
		set buttonPressed to -1
		set buttonPressedDeeper to -1
	endif

;	if ( OnPcEquip == 0 )
;		return
;	endif

	if ( categoryChosen == -1 )
		if ( OnPcEquip == 1 )
			MessageBox "What do you want to improve?", "Combat", "Magic", "Stealth", "EXIT"
			;set skillMenu to 1
			set OnPcEquip to 0
		endif

		set buttonPressed to GetButtonPressed

		if ( buttonPressed == 0 )
			Set categoryChosen to 0
		elseif ( buttonPressed == 1 )
			Set categoryChosen to 1
		elseif ( buttonPressed == 2 )
			Set categoryChosen to 2
		elseif ( buttonPressed == 3 )
			Set categoryChosen to 3
		endif
	endif

	if ( categoryChosen == -1 )
		return
	endif

	if ( categoryChosen == 3 )
		set categoryChosen to -1
		set skillMenu to -1
		set skillChosen to -1
		set buttonPressed to -1
		set buttonPressedDeeper to -1
		return
	endif

	set shardAmount to ( player->GetItemCount, "AAA_experience_shard" )

	Set acrobaticsNow to player->GetAcrobatics
	Set acrobaticsCost to costBase + ( acrobaticsNow * costMulti )

	Set alchemyNow to player->GetAlchemy
	Set alchemyCost to costBase + ( alchemyNow * costMulti )
		
	Set alterationNow to player->GetAlteration
	Set alterationCost to costBase + ( alterationNow * costMulti )

	Set armorerNow to player->GetArmorer
	Set armorerCost to costBase + ( armorerNow * costMulti )

	Set athleticsNow to player->GetAthletics
	Set athleticsCost to costBase + ( athleticsNow * costMulti )

	Set axeNow to player->GetAxe
	Set axeCost to costBase + ( axeNow * costMulti )

	Set blockNow to player->GetBlock
	Set blockCost to costBase + ( blockNow * costMulti )

	Set bluntweaponNow to player->GetBluntWeapon
	Set bluntweaponCost to costBase + ( bluntweaponNow * costMulti )

	Set conjurationNow to player->GetConjuration
	Set conjurationCost to costBase + ( conjurationNow * costMulti )

	Set destructionNow to player->GetDestruction
	Set destructionCost to costBase + ( destructionNow * costMulti )

	Set enchantNow to player->GetEnchant
	Set enchantCost to costBase + ( enchantNow * costMulti )

	Set heavyarmorNow to player->GetHeavyArmor
	Set heavyarmorCost to costBase + ( heavyarmorNow * costMulti )

	Set illusionNow to player->GetIllusion
	Set illusionCost to costBase + ( illusionNow * costMulti )

	Set lightarmorNow to player->GetLightArmor
	Set lightarmorCost to costBase + ( lightarmorNow * costMulti )

	Set longbladeNow to player->GetLongBlade
	Set longbladeCost to costBase + ( longbladeNow * costMulti )

	Set marksmanNow to player->GetMarksman
	Set marksmanCost to costBase + ( marksmanNow * costMulti )

	Set mediumarmorNow to player->GetMediumArmor
	Set mediumarmorCost to costBase + ( mediumarmorNow * costMulti )

	Set mercantileNow to player->GetMercantile
	Set mercantileCost to costBase + ( mercantileNow * costMulti )

	Set mysticismNow to player->GetMysticism
	Set mysticismCost to costBase + ( mysticismNow * costMulti )

	Set restorationNow to player->GetRestoration
	Set restorationCost to costBase + ( restorationNow * costMulti )

	Set securityNow to player->GetSecurity
	Set securityCost to costBase + ( securityNow * costMulti )
	
	Set shortbladeNow to player->GetShortBlade
	Set shortbladeCost to costBase + ( shortbladeNow * costMulti )

	Set sneakNow to player->GetSneak
	Set sneakCost to costBase + ( sneakNow * costMulti )

	Set spearNow to player->GetSpear
	Set spearCost to costBase + ( spearNow * costMulti )

	Set speechcraftNow to player->GetSpeechcraft
	Set speechcraftCost to costBase + ( speechcraftNow * costMulti )

	Set unarmoredNow to player->GetUnarmored
	Set unarmoredCost to costBase + ( unarmoredNow * costMulti )

	if ( skillMenu == 0 )
		set buttonPressedDeeper to GetButtonPressed
	elseif ( skillMenu == -1 )
		if ( categoryChosen == 0 )
			MessageBox "What do you want to improve? \n Armorer %G \n Athletics %G \n Axe %G \n Block %G \n Blunt Weapon %G \n Heavy Armor %G \n Long Blade %G \n Medium Armor %G \n Spear %G", armorerCost, athleticsCost, axeCost, blockCost, bluntweaponCost, heavyarmorCost, longbladeCost, mediumarmorCost, spearCost, "Armorer", "Athletics", "Axe", "Block", "Blunt Weapon", "Heavy Armor", "Long Blade", "Medium Armor", "Spear", "EXIT"
			set skillMenu to 0
		elseif ( categoryChosen == 1 )
			MessageBox "What do you want to improve? \n Alchemy %G \n Alteration %G \n Conjuration %G \n Destruction %G \n Enchant %G \n Illusion %G \n Mysticism %G \n Restoration %G \n Unarmored %G", alchemyCost, alterationCost, conjurationCost, destructionCost, enchantCost, illusionCost, mysticismCost, restorationCost, unarmoredCost, "Alchemy", "Alteration", "Conjuration", "Destruction", "Enchant", "Illusion", "Mysticism", "Restoration", "Unarmored", "EXIT"
			set skillMenu to 0
		else
			MessageBox "WORK IN PROGRESS", "OK"
			set skillMenu to 0
		endif
	endif

	if ( skillMenu == -1 )
		return
	endif

	if ( buttonPressedDeeper == -1 )
		return
	endif

	set skillChosen to buttonPressedDeeper
	set skillChosen to skillChosen + ( categoryChosen * 10 )
	

	if ( skillChosen == 0 )
		
		if ( armorerCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to armorerCost
			player->ModArmorer, 1
			MessageBox "Armorer has improved!"
		endif
	elseif ( skillChosen == 1 )
		
		if ( athleticsCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to athleticsCost
			player->ModAthletics, 1
			MessageBox "Athletics has improved!"
		endif
	elseif ( skillChosen == 2 )
		
		if ( axeCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to axeCost
			player->ModAxe, 1
			MessageBox "Axe has improved!"
		endif
	elseif ( skillChosen == 3 )
		
		if ( blockCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to blockCost
			player->ModBlock, 1
			MessageBox "Block has improved!"
		endif
	elseif ( skillChosen == 4 )
		
		if ( bluntweaponCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to bluntweaponCost
			player->ModBluntWeapon, 1
			MessageBox "Blunt Weapon has improved!"
		endif
	elseif ( skillChosen == 5 )
		
		if ( heavyarmorCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to heavyarmorCost
			player->ModHeavyArmor, 1
			MessageBox "Heavy Armor has improved!"
		endif
	elseif ( skillChosen == 6 )
		
		if ( longbladeCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to longbladeCost
			player->ModLongBlade, 1
			MessageBox "Long Blade has improved!"
		endif
	elseif ( skillChosen == 7 )
		
		if ( mediumarmorCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to mediumarmorCost
			player->ModMediumArmor, 1
			MessageBox "Medium Armor has improved!"
		endif
	elseif ( skillChosen == 8 )
		
		if ( spearCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to spearCost
			player->ModSpear, 1
			MessageBox "Spear has improved!"
		endif
	elseif ( skillChosen == 9 )
		;the player choose EXIT
	elseif ( skillChosen == 1 )
		
		if ( athleticsCost > shardAmount )	
			MessageBox "You don't have enough Experience Shards"
		else
			set shardsToRemove to athleticsCost
			player->ModAthletics, 1
			MessageBox "Athletics has improved!"
		endif

	endif


	;final cleanup
	set categoryChosen to -1
	set skillMenu to -1
	set skillChosen to -1
	set buttonPressed to -1
	set buttonPressedDeeper to -1

	if ( shardsToRemove > 0 )
		;set AAA_experience_shards_remover_script.shardsToRemove to shardsToRemove
		set shardsToRemove to 0
    	player->StartScript "AAA_experience_shards_remover_script"
	endif


End AAA_experience_shard_script
And the global one

Code: Select all

Begin AAA_experience_shards_remover_script

	;short shardsToRemove

	;if ( shardsToRemove > 0 )
	;	player->RemoveItem, "AAA_experience_shard", shardsToRemove
	;	set shardsToRemove to 0
	;	;StopScript AAA_experience_shards_remover_script
	;	;return
	;endif
	
	StopScript "AAA_experience_shards_remover_script"
	
	if ( GetItemCount, "AAA_experience_shard" > 0 )
		RemoveItem, "AAA_experience_shard", 1
	endif


End AAA_experience_shards_remover_script
User avatar
akortunov
Posts: 899
Joined: 13 Mar 2017, 13:49
Location: Samara, Russian Federation

Re: Scripting problem: RemoveItem

Post by akortunov »

I can suggest you:
1. To use the player->GetItemCount and player->RemoveItem.
2. To check the console output and look for errors.
Also I am not sure if RemoveItem supports variables as arguments.
User avatar
Zini
Posts: 5538
Joined: 06 Aug 2011, 15:16

Re: Scripting problem: RemoveItem

Post by Zini »

Yes it does. In OpenMW all arguments can be expressions of arbitrary complexity. This obviously includes single variables.
Post Reply