xcafe Posted November 23, 2016 Share Posted November 23, 2016 So I've defined a custom function as: bool function ArrayAddForm(Form[] SpellsToAdd, Form SpellLearned) int i = 0 debug.notification("SpellsToAdd.Length = " + SpellsToAdd.Length) while i < SpellsToAdd.Length if SpellsToAdd[i] == none SpellsToAdd[i] = SpellLearned debug.notification("Adding " + SpellLearned + " to the array.") return true else i += 1 endif endWhile return false endFunction But I don't know how to call it! Supposedly you're supposed to call it with the function name and then the variables, but when I try and call it like this: ArrayAddForm(Spellstoadd, spelllearned) It gives me this error: "spelltomereadscript.psc(46,10): type mismatch on parameter 1 (did you forget a cast?)" Please help, I don't know how to fix this :confused: Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 23, 2016 Share Posted November 23, 2016 When you write a function the parameters that you define are local variables to the function only. When you call the function you replace the parameters with data of the same type.The compiler is indicating that when you are calling the function you are not using the correct data within the first parameter. Based upon the variable names you are using, I have to ask if the form array SpellsToAdd is already defined earlier in your script. If it is, you do not need to redefine it within the function definition. Also, once you get past the first parameter issue, you'll have issue with the second parameter. Passing a variable into a function where a variable of the same name is defined will cause problems for the compiler. Overall, based upon what I see here your function definition can be reduced to: bool function ArrayAddForm() Link to comment Share on other sites More sharing options...
xcafe Posted November 23, 2016 Author Share Posted November 23, 2016 When you write a function the parameters that you define are local variables to the function only. When you call the function you replace the parameters with data of the same type.The compiler is indicating that when you are calling the function you are not using the correct data within the first parameter. Based upon the variable names you are using, I have to ask if the form array SpellsToAdd is already defined earlier in your script. If it is, you do not need to redefine it within the function definition. Also, once you get past the first parameter issue, you'll have issue with the second parameter. Passing a variable into a function where a variable of the same name is defined will cause problems for the compiler. Overall, based upon what I see here your function definition can be reduced to: bool function ArrayAddForm() When I do that, it gives me all these errors: C:\Program Files (x86)\Steam\steamapps\common\Skyrim\Data\scripts\source\spelltomereadscript.psc(46,10): no viable alternative at input 'bool'C:\Program Files (x86)\Steam\steamapps\common\Skyrim\Data\scripts\source\spelltomereadscript.psc(46,10): no viable alternative at input 'bool'C:\Program Files (x86)\Steam\steamapps\common\Skyrim\Data\scripts\source\spelltomereadscript.psc(48,8): mismatched input 'else' expecting ENDEVENTC:\Program Files (x86)\Steam\steamapps\common\Skyrim\Data\scripts\source\spelltomereadscript.psc(49,17): mismatched input '.' expecting LPARENC:\Program Files (x86)\Steam\steamapps\common\Skyrim\Data\scripts\source\spelltomereadscript.psc(0,0): error while attempting to read script spelltomereadscript: Object reference not set to an instance of an object.:( Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 23, 2016 Share Posted November 23, 2016 (edited) Can't tell what one change will do to the rest of the script when the rest of the script is not present. This is why I said it was based upon what I saw. EDIT: Please tell me that you didn't wipe the entire function and replace it with just that one line. It was just a change of the first line. The first line is where you define all the parameters of the function, thus it is the function definition. Edited November 23, 2016 by IsharaMeradin Link to comment Share on other sites More sharing options...
xcafe Posted November 23, 2016 Author Share Posted November 23, 2016 Well in that case: Scriptname SpellTomeReadScript extends ObjectReference {Teaches Spell to player through use.} Spell Property SpellLearned Auto string Property School Auto string Property LearnSpell Auto int Property difficulty = 0 Auto Spell[] Property SpellsToAdd Auto float percentageLearned = 0.0 float modifier = 1.0 int flatgain = 10 Location Property WinterholdCollegeLocation auto Location Property WinterholdCollegeArcanaeumLocation auto Event OnEquipped(Actor reader) ;Check if player is in the College of Winterhold if (Game.GetPlayer().IsInLocation(WinterholdCollegeLocation)) flatgain += 10 endIf ;Check if player is in the Arcanaeum if (Game.GetPlayer().IsInLocation(WinterholdCollegeArcanaeumLocation)) flatgain += 5 endIf modifier += (Game.getPlayer().getAV(School) - difficulty)/100 ;Check if player knows the spell if (Game.GetPlayer().HasSpell(SpellLearned)) Debug.Notification("You already know this spell.") else percentageLearned += flatgain * modifier GoToState("Learning") endIf EndEvent State Learning Event OnBeginState() ;If spell has reached 100% learned, add spell to player if (percentageLearned >= 100) bool function ArrayAddForm() Debug.Notification("You have learned" + LearnSpell + ".") else Debug.Notification("You have learned " + percentageLearned as int + "% of " + LearnSpell + ".") endIf ;Wait an hour before allowing further study Utility.WaitGameTime(1) Debug.Notification("You feel as though your mind is ready to study " + LearnSpell + " again.") flatgain = 10 modifier = 1.0 GoToState("Done") EndEvent Event OnEquipped(Actor reader) Debug.Notification("Your mind cannot focus. Wait a while before studying this spell again.") EndEvent EndState bool function ArrayAddForm(Form[] SpellsToAdd, Form SpellLearned) int i = 0 debug.notification("SpellsToAdd.Length = " + SpellsToAdd.Length) while i < SpellsToAdd.Length if SpellsToAdd[i] == none SpellsToAdd[i] = SpellLearned debug.notification("Adding " + SpellLearned + " to the array.") return true else i += 1 endif endWhile return false endFunction Here's the whole thing! Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 23, 2016 Share Posted November 23, 2016 You replaced the function call instead of changing what should have been changed in the function definition. Following is a corrected version which also compiles. Whether or not it does what you want, remains to be seen. Additionally, you were using Game.GetPlayer() a lot. This takes up extra resources as it has to call another script. If you compare it once to the 'reader' in your OnEquipped event, you can then use the 'reader' variable instead of Game.GetPlayer(). It increases thread safety and speeds up the script. Scriptname SpellTomeReadScript extends ObjectReference {Teaches Spell to player through use.} Spell Property SpellLearned Auto string Property School Auto string Property LearnSpell Auto int Property difficulty = 0 Auto Spell[] Property SpellsToAdd Auto float percentageLearned = 0.0 float modifier = 1.0 int flatgain = 10 Location Property WinterholdCollegeLocation auto Location Property WinterholdCollegeArcanaeumLocation auto Event OnEquipped(Actor reader) If reader == Game.GetPlayer() ;Check if player is in the College of Winterhold if (reader.IsInLocation(WinterholdCollegeLocation)) flatgain += 10 endIf ;Check if player is in the Arcanaeum if (reader.IsInLocation(WinterholdCollegeArcanaeumLocation)) flatgain += 5 endIf modifier += (reader.getAV(School) - difficulty)/100 ;Check if player knows the spell if (reader.HasSpell(SpellLearned)) Debug.Notification("You already know this spell.") else percentageLearned += flatgain * modifier GoToState("Learning") endIf EndIf EndEvent State Learning Event OnBeginState() ;If spell has reached 100% learned, add spell to player if (percentageLearned >= 100) ArrayAddForm() Debug.Notification("You have learned" + LearnSpell + ".") else Debug.Notification("You have learned " + percentageLearned as int + "% of " + LearnSpell + ".") endIf ;Wait an hour before allowing further study Utility.WaitGameTime(1) Debug.Notification("You feel as though your mind is ready to study " + LearnSpell + " again.") flatgain = 10 modifier = 1.0 GoToState("Done") EndEvent Event OnEquipped(Actor reader) Debug.Notification("Your mind cannot focus. Wait a while before studying this spell again.") EndEvent EndState bool function ArrayAddForm() int i = 0 debug.notification("SpellsToAdd.Length = " + SpellsToAdd.Length) while i < SpellsToAdd.Length if SpellsToAdd[i] == none SpellsToAdd[i] = SpellLearned debug.notification("Adding " + SpellLearned + " to the array.") return true else i += 1 endif endWhile return false endFunction Link to comment Share on other sites More sharing options...
xcafe Posted November 23, 2016 Author Share Posted November 23, 2016 Awesome, thank you! D'you think that you could help me with one more thing? (I'm new to scripting :laugh: ) Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 23, 2016 Share Posted November 23, 2016 Depends upon what it is. Not a mind reader and I don't know everything. Link to comment Share on other sites More sharing options...
xcafe Posted November 23, 2016 Author Share Posted November 23, 2016 https://forums.nexusmods.com/index.php?/topic/5095555-add-spells-from-array/ This here :D Link to comment Share on other sites More sharing options...
Recommended Posts