[Papyrus Script] Help with spell conditions

Post » Tue Jun 19, 2012 7:09 am

Hey guys and gals,
So I had a problem last week, and took a break in hopes that I could attack it again with a fresh mind! But I am still having problems!

Essentially, I really just want to be able to do exactly what spell effect conditions do, but within a script. To be more precise I want to use spell effect conditions (that check keywords of an attacking actor) within an If statement.
For the life of me I cannot do it, and it’s frustrating me because it’s literally stopped any progress on my mod :(

If anyone could do me the great service of writing a proof of concept script for me to anolyse I would appreciate it. A Great proof would be a script that can check an aggressive actor who has just hit the player, to see if they are a beast, and if that is the case, cast a spell.
This would be enough for me to combat my problem!

Many thanks,
From a weary modder!
User avatar
Juan Cerda
 
Posts: 3426
Joined: Thu Jul 12, 2007 8:49 pm

Post » Tue Jun 19, 2012 4:39 pm

The easiest way to get a script on the player (that I've found) is to attach it to a Magic Effect and slap that in an Ability.

I looked at the Animal Allegiance voice power (makes animals follow you) and all it really checks is if the target has the keyword "ActorTypeAnimal" and isn't undead. So...



Scriptname YourScript extends ActiveMagicEffectKeyword Property ActorTypeAnimal autoSpell Property Whatevs autoEvent OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, \ bool abBashAttack, bool abHitBlocked)  if akAggressor.HasKeyword(ActorTypeAnimal) ;should return true if you were just hit by an animal  Whatevs.Cast(Game.GetPlayer(), akAggressor) ;casts from the player to the animal, could also have the animal cast it on itself  endifEndEvent
User avatar
FLYBOYLEAK
 
Posts: 3440
Joined: Tue Oct 30, 2007 6:41 am

Post » Tue Jun 19, 2012 9:23 am

redundent post!
User avatar
Rinceoir
 
Posts: 3407
Joined: Thu Jun 29, 2006 1:54 am

Post » Tue Jun 19, 2012 8:55 am

Ok, so cscottydont is a legend, his script solution was perfect! I will try to elaborate on my final problem because i feel that there are ways of solving it, but my papyrus knowledge is not too great.

So I want to cast a spell when hit by a hostile spell. I will use the same set up as cs described, with a script extending an activemagiceffect.

I think i am on a winner but have hit a little stumble.

Scriptname feedback extends ActiveMagicEffect  actor property selfRef auto hiddenspell property FortifyEffect autoEVENT OnEffectStart(Actor Target, Actor Caster)	    	    selfRef = caster	    endEVENTEVENT onHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)if akSource.IsHostile() && \    !(selfRef.isdead())    FortifyEffect.cast(selfRef, selfRef)   	endifendEVENT


I am getting this error though:
IsHostile is not a function or does not exist

I have a feeling it is because akSource can be more than just a spell? is there a way I can perform a check on the akSource to see if its a spell, then do the isHostile() function?


help!
User avatar
Marie
 
Posts: 3405
Joined: Thu Jun 29, 2006 12:05 am

Post » Tue Jun 19, 2012 6:03 am

Sort of. This is because IsHostile is not defined in the http://www.creationkit.com/Form_Script, so trying to call it on an object of type Form won't work. You need to cast akSource to a type with this function defined, like http://www.creationkit.com/Spell_Script, so the compiler can find the function that you want to call:
(akSource as Spell).IsHostile()
If akSource cannot be cast to type Spell (for example, if it's a Weapon) then the result of the cast will be None, so it would be best to check that the result of the cast is not None before attempting to use it to call a function.

Cipscis
User avatar
joeK
 
Posts: 3370
Joined: Tue Jul 10, 2007 10:22 am

Post » Tue Jun 19, 2012 7:17 am

Hey again Cipscis, thanks for the help. (i am at work right now so I am going to try and anticipate issues i may run into tonight).

If akSource cannot be cast to type Spell (for example, if it's a Weapon) then the result of the cast will be None, so it would be best to check that the result of the cast is not None before attempting to use it to call a function. Cipscis

This will likely happen. If I were to use the code you provided I am guessing it would compile, but what would happen in game if..
The player is hit by a harmful spell: (hostilespell returns 1, and spell is cast)
The player is hit by a friendly spell: (hostilespell returns 0, no spell cast)
The player is hit by a non spell source: error, script crashes?
User avatar
Zualett
 
Posts: 3567
Joined: Mon Aug 20, 2007 6:36 pm

Post » Tue Jun 19, 2012 4:48 pm

Assuming the code used has structure something like this:
If (akSource as Spell)	If (akSource as Spell).IsHostile()		; Do stuff	EndIfEndIf
Then the second condition will only be checked if akSource can be cast correctly.

I think the following would also work, but it didn't in previous games (which, admittedly, used completely different scripting languages) and I haven't tested it personally:
If ((akSource as Spell)  && (akSource as Spell).IsHostile())

I expect trying to call http://www.creationkit.com/IsHostile_-_Spell on an incorrect cast (which results in None) would print an error to the script log and the code would continue to run. I'm not sure if there are any side effects of this, but it seems to me that it's a case that would be best avoided.

Cipscis
User avatar
Add Meeh
 
Posts: 3326
Joined: Sat Jan 06, 2007 8:09 am

Post » Tue Jun 19, 2012 7:27 am

Cip, that worked perfectly, thank you so much :) and my alpha is done :D
User avatar
Spaceman
 
Posts: 3429
Joined: Wed May 23, 2007 10:09 am


Return to V - Skyrim