Jump to content

[LE] Casting spells by script?


irswat

Recommended Posts

Grimybunyip had a feature in his Grimy Utilities that allowed you to set a spell to a hotkey, and then cast that spell without equipping a weapon, or equipping the spell. How is this accomplished? I'm putting the final touches on the skyrim voice command engine I've been working on for some time now, and one of the remaining features is spell casting. I want to give players the option of either equipping the spell, which is easy, or casting the spell.

I assume I need to find out what type of spell it is, is it beneficial or not, and then use Spell.cast. How do I get the position the cross hair are pointing at? Do I just place some some of marker at that position and then do spell.cast(PlayerRef, CrossHairMarker)?

Link to comment
Share on other sites

No, when you use the Cast function in a script it automatically fires the spell in a straight line in the direction the player is facing. Sidenote: I don't know how well that function is going to work for things like concentration spells.

Link to comment
Share on other sites

What do you think about this?

 

form FormThatMatches=ExEquipItem(ItemNameLength, SearchType)
			if FormThatMatches
				Spell castSpell=FormThatMatches as Spell
				if castSpell
					ObjectReference curTarget
					Actor curTargetActor
					
					int IsASpell=GetSpellType(castSpell)
					int SpellCastType=GetSpellCastType(castSpell)
					int SpellDeliveryType=GetSpellDelivery(castSpell)
					
					bool curTargActorHostile
					bool HostileSpell
					
					if IsASpell==0	;spell
						if SpellCastType!=1 ;not fire and forget
							;simply equip spell
																
							int SpellToEquip=GetEquipSlot(CMDParserCounter) ;gets what hand spell should be equipped in
							
							if SpellToEquip==1 || SpellToEquip==2
								PlayerRef.EquipItemEx(FormThatMatches,SpellToEquip,false,true)
							elseif SpellToEquip == 3
								PlayerRef.EquipItemEx(FormThatMatches,1,false,true)
								PlayerRef.EquipItemEx(FormThatMatches,2,false,true)
							elseif SpellToEquip == -1 
								;EXCEPTION ERROR
							elseif SpellToEquip == -99 
								;no equipment slot command detected
								if !Game.GetPlayer().GetEquippedWeapon()
									PlayerRef.EquipItemEx(FormThatMatches,1,false,true)
								elseif Game.GetPlayer().GetEquippedWeapon() && !Game.GetPlayer().GetEquippedWeapon(false)
									PlayerRef.EquipItemEx(FormThatMatches,2,false,true)
								else
									PlayerRef.EquipItemEx(FormThatMatches,1,false,true)
								endif
							endif							
						elseif SpellCastType==1
						
							HostileSpell=castSpell.IsHostile()
							curTarget=Game.GetCurrentCrosshairRef()	
							curTargetActor=curTarget as Actor
							
							if curTargetActor
								curTargActorHostile=curTargetActor.IsHostileToActor(PlayerRef)	
							endif
							
							if curTargetActor && curTargActorHostile==false && HostileSpell==false
								;cast beneficial spell at curTargetActor
								castSpell.cast(PlayerRef,curTargetActor)
							elseif curTargetActor && curTargActorHostile==true && HostileSpell==true
								;cast hostile spell at curTargetActor
								castSpell.cast(PlayerRef,curTargetActor)
							elseif !curTargetActor && HostileSpell==false
								;cast beneficial spell at self
								castSpell.cast(PlayerRef)
							elseif !curTargetActor && HostileSpell==true
								;cast hostile spell at default
								castSpell.cast(PlayerRef)
							endif
						endif
					endif
				endif
			endif 

 

 

Edited by irswat
Link to comment
Share on other sites

Wow, uh, that is a lot of functions. :laugh:

I assume that's part of a larger event/function, so as long as the script compiles, it should be fine. It looks like what you did with the .cast function is solid, but I've never used .equipitemex, so I wouldn't know whether or not that would work. Test it out and let me know! :happy:

Link to comment
Share on other sites

thanks xcafe. This is the semi-final code, and it works!

 

form FormThatMatches=ExEquipItem(ItemNameLength, SearchType)
debug.trace("Returned form " + FormThatMatches.GetName())
if FormThatMatches
	debug.trace("Form that matches!")
	Spell castSpell=FormThatMatches as Spell
	debug.trace("FormThatMatches as Spell: " + castSpell)
	if castSpell
		debug.trace("castSpell!")
		ObjectReference curTarget
		Actor curTargetActor
		
		int IsASpell=GetSpellType(castSpell)
		debug.trace("GetSpellType(0=spell): " + IsASpell)
		int SpellCastType=GetSpellCastType(castSpell)
		debug.trace("SpellCastType: " + SpellCastType)
		int SpellDeliveryType=GetSpellDelivery(castSpell)
		debug.trace("SpellDeliveryType: " + SpellDeliveryType)
		bool curTargActorHostile
		bool HostileSpell
		
		if IsASpell==0	;spell
			if SpellCastType!=1 ;not fire and forget
				debug.trace("Not a fire and forget spell!")
				;simply equip spell
													
				int SpellToEquip=GetEquipSlot(CMDParserCounter)
				
				if SpellToEquip==1 || SpellToEquip==2
					debug.trace("Equipping to right hand")
					PlayerRef.EquipSpell(castSpell,1)  ;right
				elseif SpellToEquip == 3
					debug.trace("Equipping to both hands")
					PlayerRef.EquipSpell(castSpell,1) ;right
					PlayerRef.EquipSpell(castSpell,0) ;left
				elseif SpellToEquip == -1 
					;EXCEPTION ERROR
				elseif SpellToEquip == -99 
					;no equipment slot command detected
					if !Game.GetPlayer().GetEquippedWeapon()
						debug.trace("Equipping to right hand")
						PlayerRef.EquipSpell(castSpell,1) ;right
					elseif Game.GetPlayer().GetEquippedWeapon() && !Game.GetPlayer().GetEquippedWeapon(false)
						debug.trace("Equipping to left hand")
						PlayerRef.EquipSpell(castSpell,0) ;left
					else
						debug.trace("Equipping to right hand")
						PlayerRef.EquipSpell(castSpell,1) ;right
					endif
				endif							
			elseif SpellCastType==1
			
				HostileSpell=castSpell.IsHostile()
				curTarget=Game.GetCurrentCrosshairRef()	
				curTargetActor=curTarget as Actor
				
				if curTargetActor
					curTargActorHostile=curTargetActor.IsHostileToActor(PlayerRef)	
				endif
				
				if curTargetActor && curTargActorHostile==false && HostileSpell==false
					;cast beneficial spell at curTargetActor
					castSpell.cast(PlayerRef,curTargetActor)
				elseif curTargetActor && curTargActorHostile==true && HostileSpell==true
					;cast hostile spell at curTargetActor
					castSpell.cast(PlayerRef,curTargetActor)
				elseif !curTargetActor && HostileSpell==false
					;cast beneficial spell at self
					castSpell.cast(PlayerRef)
				elseif !curTargetActor && HostileSpell==true
					;cast hostile spell at default
					castSpell.cast(PlayerRef)
				endif
			endif
		endif
	endif
endif
 

 



Most of these functions are from GrimyPlugin. Again this is a voice command engine. ExEquipItem parses the voice command and returns the form of the item. For example "Cast Spark both hands quick". Cast is the command, ExEquipItem returns the spell form Sparks, and GetEquipHands returns either left, right, both, or -1 for exception, or -99 if there is no equipping command, in which case it will default to the empty hand, and if no hand is empty, to the right hand.

The function works! Thanks for your help

Link to comment
Share on other sites

  • Recently Browsing   0 members

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