Jump to content

Need Help on Arrays


fg109

Recommended Posts

In order to learn Papyrus, I decided to try and recreate the CM Partners mod for Skyrim. It had a function to summon partners to the player. However, it did this through scripts on the partners themselves, which meant that the partners don't teleport if they're not in a loaded cell. So I decided to store references to the partners in an array and use those references to teleport them around using the quest script.

 

This is what I have so far in the quest script:

 

 

ScriptName fg109PartnersQuestScript extends Quest


int index
int count
ObjectReference[] ActivePartners

Spell property Spell01 auto
Spell property Spell02 auto


Event OnInit()

index = 0
ActivePartners = new ObjectReference[10]
Game.GetPlayer().AddSpell(Spell01)
Game.GetPlayer().AddSpell(Spell02)

EndEvent


Function Summon()

index = 0
While (index < ActivePartners.length)
	if (ActivePartners[index] == None)
		index = ActivePartners.length
	else
		ActivePartners[index].MoveTo(Game.GetPlayer())
	endif
	index += 1
EndWhile

EndFunction


Function AddActivePartner(Actor Partner)

index = 0
While (index < ActivePartners.length)
	if (ActivePartners[index] == None)
		ActivePartners[index] = Partner
		index = ActivePartners.length
	elseif (index == (ActivePartners.length - 1))
		Debug.Notification("Something's wrong, array is already full.")
		index = ActivePartners.length
	endif
	index += 1
EndWhile

EndFunction


Function RemoveActivePartner(Actor Partner)

index = 0
count = 0
While (index < ActivePartners.length)
	if (ActivePartners[index] == Partner)
		ActivePartners[index] == None
		While (index < ActivePartners.length)
			if (ActivePartners[index + 1] == None)
				index = ActivePartners.length
			else
				ActivePartners[index] = ActivePartners[index + 1]
			endif
			index += 1
			count += 1
		EndWhile
		index = ActivePartners.length
	elseif (index == (ActivePartners.length - 1))
		Debug.Notification("Something's very wrong, Partner is not in the array.")
		index = ActivePartners.length
	endif
	index += 1
EndWhile
if (count > 0)
	Debug.Notification("Nested loop worked.")
endif

EndFunction

 

 

The two spells added were used for testing function calls in the quest script. Adding actors to the array and then teleporting them to the player worked fine. The problem is when I try to remove them from the array. I can remove all the references except for the last actor that is added.

 

For example, I add A to the array. I try to remove A and fail. I add B to the array. I can remove A but now I can't remove B. I add C, D, and E. I can remove B, C, and D, but not E.

 

It's impossible for me to empty out the array. I hope someone can point out what the error in the code is, because I can't figure it out.

 

P.S. There's no break function in Papyrus, so I had to improvise.

Edited by fg109
Link to comment
Share on other sites

Not sure how you'd do this correctly but you could always rebuild the array when needed and set your property with the new array.

 

Like if you need to remove the last added index (the one you can't remove right?) then just keep a count of it's actual length and rebuild the array if that's the one you're trying to remove.

Link to comment
Share on other sites

@fg, you are gonna kick yourself :)

 

 

Function RemoveActivePartner(Actor Partner)
       index = 0
       count = 0
       While (index < ActivePartners.length)
               if (ActivePartners[index] == Partner)
                       ActivePartners[index] == None ; <--------------------- Should be an assignment, not a comparison.
                       While (index < ActivePartners.length)
                               if (ActivePartners[index + 1] == None) ; <--- if (index == 9) ???? beyond array bounds
                                       index = ActivePartners.length
                               else
                                       ActivePartners[index] = ActivePartners[index + 1]
                               endif
                               index += 1
                               count += 1
                       EndWhile
                       index = ActivePartners.length
               elseif (index == (ActivePartners.length - 1))
                       Debug.Notification("Something's very wrong, Partner is not in the array.")
                       index = ActivePartners.length
               endif
               index += 1
       EndWhile
       if (count > 0)
               Debug.Notification("Nested loop worked.")
       endif
EndFunction

 

Your main problem is the comparison when you meant an assignment. Don't ask me how many times I've mixed those up lol.

The second problem will give you a headache as soon as your array fills.

 

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

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