Jump to content

One script activating functions in another script which feed back into the first?


lucidaxe

Recommended Posts

Hi all, I'm stumped. To keep the question as simple as possible, I'll present the problem and its circumstances as simple as possible. I'm trying to understand how to call a function in other another script to change a value AND then get the value back into the original script for further processing.

 

 

SETUP

 

I have created a custom Magic Effect (MyMagicEffect) to be activated from a spell. On MyMagicEffect, I've put two scripts: MyScriptEvent and MyScriptFunction.

Scriptname MyScriptEvent extends activemagiceffect

Type Property Item1  Auto
Type Property Item2  Auto
Type Property Item3  Auto
Type Property Item4  Auto

Int CarriedAmountItem1
Int CarriedAmountItem2
Int CarriedAmountItem3

Int RemoveAmountItem1
Int RemoveAmountItem2
Int RemoveAmountItem3
Int AddAmountItem4

Event OnEffectStart(Actor akTarget, Actor akCaster)
objectReference Caster = akCaster
	CarriedAmountItem1 = Caster.GetItemCount(Item1)
	CarriedAmountItem2 = Caster.GetItemCount(Item2)
	CarriedAmountItem3 = Caster.GetItemCount(Item3)
	If Caster.GetItemCount(Item4) < 4
		Function1()
	ElseIf Caster.GetItemCount(Item4) < 8
		Function2()
	ElseIf Caster.GetItemCount(Item4) >= 8
		Function3()
	EndIf
	If AddAmountItem4 == 0
		Debug.Notification("You get nothing.")
	Else
		Caster.RemoveItem(Item1, RemoveAmountItem1, true)
		Caster.RemoveItem(Item2, RemoveAmountItem2, true)
		Caster.RemoveItem(Item3, RemoveAmountItem3, true)
		Caster.AddItem(Item4, AddAmountItem4, false)
	EndIf
EndEvent
Scriptname MyScriptFunction extends activemagiceffect

Int CarriedAmountItem1
Int CarriedAmountItem2
Int CarriedAmountItem3

Int RemoveAmountItem1
Int RemoveAmountItem2
Int RemoveAmountItem3
Int AddAmountItem4

float AddableAmountItem4
float RemovableAmountItem1
float RemovableAmountItem2
float RemovableAmountItem3
Int WholeAddableAmountItem4


Int Function1()
	If CarriedAmountItem1 >= 16
		AddableAmountItem4 = CarriedAmountItem1 / 16
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem1 = WholeAddableAmountItem4 * 16
	EndIf
	If CarriedAmountItem2 >= 16
		AddableAmountItem4 = CarriedAmountItem2 / 16
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem2 = WholeAddableAmountItem4 * 16
	EndIf
	If CarriedAmountItem3 >= 16
		AddableAmountItem4 = CarriedAmountItem3 / 16
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem3 = WholeAddableAmountItem4 * 16
	EndIf
EndFunction

Int Function2()
	If CarriedAmountItem1 >= 10
		AddableAmountItem4 = CarriedAmountItem1 / 10
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem1 = WholeAddableAmountItem4 * 10
	EndIf
	If CarriedAmountItem2 >= 10
		AddableAmountItem4 = CarriedAmountItem2 / 10
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem2 = WholeAddableAmountItem4 * 10
	EndIf
	If CarriedAmountItem3 >= 10
		AddableAmountItem4 = CarriedAmountItem3 / 10
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem3 = WholeAddableAmountItem4 * 10
	EndIf
EndFunction

Int Function3()
	If CarriedAmountItem1 >= 4
		AddableAmountItem4 = CarriedAmountItem1 / 4
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem1 = WholeAddableAmountItem4 * 4
	EndIf
	If CarriedAmountItem2 >= 4
		AddableAmountItem4 = CarriedAmountItem2 / 4
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem2 = WholeAddableAmountItem4 * 4
	EndIf
	If CarriedAmountItem3 >= 4
		AddableAmountItem4 = CarriedAmountItem3 / 4
		WholeAddableAmountItem4 = AddableAmountItem4 as Int
		AddAmountItem4 = AddAmountItem4 + WholeAddableAmountItem4
		RemoveAmountItem3 = WholeAddableAmountItem4 * 4
	EndIf
EndFunction

The idea is that MyScriptEvent should activate upon the spell being cast.

(1) It sets CarriedAmountItem1, CarriedAmountItem2 and CarriedAmountItem3's values based on the caster's inventory. These variables now contain the inventory values but can be used simply as values standing on their own, divorced of their specific reference to the caster.

(2) With the values determined for CarriedAmountItem1, CarriedAmountItem2 and CarriedAmountItem3 respectively (and the default value for AddAmountItem4 being 0), it calls upon MyScriptFunction's functions to perform operations that result in the values for RemoveAmountItem1, RemoveAmountItem2, RemoveAmountItem3 and AddAmountItem4 being determined depending on how many Item4 the caster has in their inventory (see the conditionals in MyScriptEvent that call a specific function).

(3) Taking the values for RemoveAmountItem1, RemoveAmountItem2, RemoveAmountItem3 and AddAmountItem4 created by MyScriptFunction, MyScriptEvent continues first to check if the functions did not all fail their conditionals, which would leave AddAmountItem4 at value 0.

(4) If AddAmountItem4 does not equal 0, then the values for RemoveAmountItem1, RemoveAmountItem2, RemoveAmountItem3 and AddAmountItem4 that were determined by MyScriptFunction are used to remove and add those amounts of the items from/to the caster.

 

The idea is that MyScriptFunction contains a few functions to perform simple mathematics on the initial variable values it is given, with each function only running if MyScriptEvent detects that the caster has a specific amount (range) of Item4 in their inventory.

(1) For each function, it detects the values for CarriedAmountItem1, CarriedAmountItem2 and CarriedAmountItem3 given to it by MyScriptEvent, and performs operations based on those values to fill in the values for variables RemoveAmountItem1, RemoveAmountItem2, RemoveAmountItem3 and AddAmountItem4 (which need to be integers because only whole numbers of the items can be removed from or added to the caster's inventory).

(2) With these variables given a new value, the function that was activated stops and MyScriptEvent takes over again, picking up the values for variables RemoveAmountItem1, RemoveAmountItem2, RemoveAmountItem3 and AddAmountItem4 that MyScriptFunction has created.

 

TL;DR: Script 1 sets values for variables -> Script 1 calls for function in Script 2 to set new values for other variables, based on the Script 1 set variables -> Script 2 function sets values for variables and processing resumes in Script 1, based on the Script 2 set variables. I'm dumb, how do?

 

 

THE BIG QUESTIONS

 

What should I write in these scripts to make them behave the way I expect them to? And I can't divorce MyScriptEvent from MyMagicEffect since it needs the event to determine who/what the caster is, but is it necessary to attach/extend MyScriptFunction to/from something else? Specifically...

 

A. How do I make MyScriptEvent activate the functions in MyScriptFunction?

 

B. The moment MyScriptEvent calls to activate a function in MyScriptFunction, does it communicate the values for CarriedAmountItem1, CarriedAmountItem2 and CarriedAmountItem3 that it set automatically to MyScriptFunction as well?

 

C. Once the activated function in MyScriptFunction has completed, are the values for RemoveAmountItem1, RemoveAmountItem2, RemoveAmountItem3 and AddAmountItem4 that it set automatically brought over to MyScriptEvent asMyScriptEvent tries to continue past the If/ElseIf Caster.GetItemCount conditional that activated the function, further performing its own functions based on those values?

 

D. I assume I should create MyScriptFunction first and make it 'findable' before creating and compiling MyScriptEvent, since MyScriptEvent refers to it. However, it also seems like MyScriptEvent might need to be created and made 'findable' first, since MyScriptEvent has to feed values for CarriedAmountItem1, CarriedAmountItem2 and CarriedAmountItem3 into MyScriptFunction to start it off. Which is correct?

Edited by lucidaxe
Link to comment
Share on other sites

First of all, the syntax for your event is wrong, it's just "Event OnWhatever()", not "OnEvent OnWhatever()" Check here for correct syntax: http://www.creationkit.com/index.php?title=Category:Papyrus

 

I think this would be way simpler if you explain what you seek to accomplish with this script. I've read through it and I honestly have no idea. A large amount of code seems redundant. Consider if you actually need multiple scripts. That said, here's an example of comunication between two scripts that may help:

Scriptname MyEventScript extends activemagiceffect
 
MySuperScript Property ScriptThatHandlesStuff Auto; This is a startgame-enabled quest with the script MySuperScript attached. You want a quest because you can make a reference to it.
 
Event OnEffectStart(Actor akTarget, Actor akCaster)
  Int DataToPass = whatever ;obtain some data
  Int ReturnValue = ScriptThatHandlesStuff.MyFunction(DataToPass) ;call my function passing the data we collected and save to an integer what is returned
  Int Whatever = ReturnValue + 847 ; do stuff with what was returned
EndEvent
 
------------------------
 
ScriptName MySuperScript  extends Quest
 
Int Function MyFunction(Int data); function that takes an integer (data) and returns another int.
  StuffToReturn = data * 847 ;do some stuff with data
  Return StuffToReturn ; this is what the other script receives
EndFunction

You cannot make this with two activemagiceffect scripts because you can't obtain a reference to it on the fly. Again though, consider if you actually need two scripts.

Edited by FrankFamily
Link to comment
Share on other sites

Hey FrankFamily, thanks for the feedback.

 

Luckily the OnEvent thing was just a typo on my part when making the example code, in my actual script it's correct. I adjusted it now in the original post too.

 

The reason for a number of the redundancies is because my actual script is far more complex and elaborate and I wanted to keep some of the basic structure I'd used for that the same in the example, but it would be a waste of time to copy the entire script and explain everything about it when I just wanted to figure out a very specific aspect, for which I could use a highly simplified example segment.

Basically the gist of this segment is being able to 'cash in' specific items for one specific reward item, with a higher or lower amount being awarded to the caster based on how much of the reward item is already currently in their possession.

 

Thanks a lot for the simple example of communication between scripts - that tells me exactly the basic mechanism for what I'm trying to accomplish. I appreciate the reply!

Link to comment
Share on other sites

"I'm trying to understand how to call a function in another script to change a value AND then get the value back into the original script for further processing."

 

​This kind of stuff can be done generically and easily with Global variables ...

 

GlobalVariable Property Vat Auto

 

Vat.SetValueInt(2)

 

If(Vat.GetValueInt()==(2))

 

Remember: you also have to physically make the global in the creation kit by it's name and type.

 

(then you can use them as directors for functions and/or values)

Edited by NexusComa
Link to comment
Share on other sites

  • Recently Browsing   0 members

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