Jump to content

Made an "at runtime" description compiler, but one thing doesn't work...


HappySlapp

Recommended Posts

I made fully working runtime description compiler for challenges I'm adding to my mod, but one thing is a bit off, it compiles it just fine with the number of items and the name of the object, but it always one more of the last object in the list, despite it seeming like it shouldn't be doing that, here's the script:

Scriptname YAS_Script_ChallengesManager extends Quest

YAS_Script_Main	Property	YASMain	Auto

Struct ChallengeStruct
	String			ChallengeID
	Message			Description
	Int			TokenReward
	Formlist		Reward
	String			RewardDescription
	Int			CurrentRank
	GlobalVariable	        MaxRank
EndStruct

ChallengeStruct[] Property ChallengeArray Auto

Event OnQuestInit()
	InitializeChallengeDescriptions(ChallengeArray)
EndEvent

Message Function GetChallengeDescription(String akID)
	Int iStructIndex = ChallengeArray.FindStruct("ChallengeID", akID, 0)
	Message _description = ChallengeArray[iStructIndex].Description
	Return _description
EndFunction

String Function GetChallengeRewardDescription(String akID)
	Int iStructIndex = ChallengeArray.FindStruct("ChallengeID", akID, 0)
	String _rewardDescription = ChallengeArray[iStructIndex].RewardDescription
	Return _rewardDescription
EndFunction

Function InitializeChallengeDescriptions(ChallengeStruct[] akChallenges)
	Int iArrayIndex = 0
	While (iArrayIndex < akChallenges.Length)
		ChallengeStruct akChallenge = akChallenges[iArrayIndex]
		akChallenge.RewardDescription = "Rewards:<br><br>"
		Int iIndex = 0
		While (iIndex < akChallenge.Reward.GetSize())
			If (iIndex != akChallenge.Reward.GetSize())
				If (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() > 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName() + "s, ")
				ElseIf (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() == 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName() + ", ")
				EndIf
			ElseIf (iIndex == akChallenge.Reward.GetSize())
				If (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() > 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName() + "s")
				ElseIf (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() == 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName())
				EndIf
			EndIf
			iIndex += 1
		EndWhile
		iArrayIndex += 1
	EndWhile
EndFunction

The part of the script where it compiles the descriptions of all challenges is here:

Function InitializeChallengeDescriptions(ChallengeStruct[] akChallenges)
	Int iArrayIndex = 0
	While (iArrayIndex < akChallenges.Length)
		ChallengeStruct akChallenge = akChallenges[iArrayIndex]
		akChallenge.RewardDescription = "Rewards:<br><br>"
		Int iIndex = 0
		While (iIndex < akChallenge.Reward.GetSize())
			If (iIndex != akChallenge.Reward.GetSize())
				If (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() > 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName() + "s, ")
				ElseIf (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() == 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName() + ", ")
				EndIf
			ElseIf (iIndex == akChallenge.Reward.GetSize())
				If (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() > 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName() + "s")
				ElseIf (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() == 1)
					akChallenge.RewardDescription += (((akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(0) as GlobalVariable).GetValueInt() + " " + (akChallenge.Reward.GetAt(iIndex) as Formlist).GetAt(1).GetName())
				EndIf
			EndIf
			iIndex += 1
		EndWhile
		iArrayIndex += 1
	EndWhile
EndFunction

Anyone have any advice on what I might've done wrong? I feel like it should be working.

Edited by HappySlapp
Link to comment
Share on other sites

The last Formlist within the Rewards Formlist.

 

It contains a GlobalVariable which determines the amount of the reward, and a form in the second index which is the object given.

 

It always seems to duplicate the information from the last index of the Rewards Formlist.

Link to comment
Share on other sites

hmmm... not sure what's wrong, but the comparison for the conditional of the last index of the FormList should be iIndex == akChallenge.Reward.GetSize() - 1

The while loop should never meet the if condition for iIndex == akChallenge.Reward.GetSize() because the loop condition is iIndex < akChallenge.Reward.GetSize()

 

GetSize() = 3:

Index 0 (if 0 == 3)

Index 1 (if 1 == 3)

Index 2 (if 2 == 3)

 

The index will not reach 3.

 

Sorry I can't help with the actual problem, tho.

Link to comment
Share on other sites

hmmm... not sure what's wrong, but the comparison for the conditional of the last index of the FormList should be iIndex == akChallenge.Reward.GetSize() - 1

The while loop should never meet the if condition for iIndex == akChallenge.Reward.GetSize() because the loop condition is iIndex < akChallenge.Reward.GetSize()

 

GetSize() = 3:

Index 0 (if 0 == 3)

Index 1 (if 1 == 3)

Index 2 (if 2 == 3)

 

The index will not reach 3.

 

Sorry I can't help with the actual problem, tho.

 

Actually what you just said in theory just solved his problem. His rewards system constructs a message based on the reward formlist and it would look like this:

 

Rewards:

- 6 Stimpacks

- 1 Military Grade Duct Tape

 

His problem was that the last item got duplicated so it printed:

 

Rewards:

- 6 Stimpacks

- 1 Military Grade Duct Tape

- 1 Military Grade Duct Tape

 

This is indeed because he was using while(iIndex < akChallenge.Reward.GetSize()) instead of while(iIndex < akChallenge.Reward.GetSize() - 1). What's confusing me is that he's accessing an element outside of the formlist index bounds yet it still somehow worked instead of returning an error or None.

Edited by NoCashNoExp
Link to comment
Share on other sites

I though the same, but actually while(iIndex < akChallenge.Reward.GetSize()) should be ok, let's say length is 3 as in my example

 

While 0 < 3 is true

While 1 < 3 is true

While 2 < 3 is true

While 3 < 3 is false, it should not run this loop

 

If you use -1 the index would stop at index 1, because 2 is not less than 2.

Edited by DieFeM
Link to comment
Share on other sites

Okay, this is really weird, I changed the iIndex == akChallenge.Reward.GetSize() to iIndex == akChallenge.Reward.GetSize() - 1 and yet it's now duplicating both items in the formlist. I really don't get how that's happening.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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