Error, Index was out of range

Hi, so I have been making a custom function for npcs by giving them an npc score. After doing that, I decided I wanted to add flags depending on how high or low the score is. Since my first function returns the score as npc.score/npc.maxscore, I couldn't have the code recognize the integer so my solution for this was making a copy of the function NpcScore and making a NpcVScore which does the same thing except the return value is an integer. But the problem with that is when I run it, it now gives me this error:

Error evaluating expression 'NpcScoreV(Tiffany)': Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index

It will also be noted I am using the conlib library in this code.

The two functions are here:

<function name="NpcScore" parameters="npc, change" type="string"><![CDATA[
    if (not HasInt (npc, "score")) {
      npc.score = 10
      npc.maxscore = 20
    }
    if (IsDefined ("change")) {
      npc.score = npc.score + change
      if (npc.score > npc.maxscore) {
        npc.score = npc.maxscore
      }
    }
    return (npc.score + "/" + npc.maxscore)
    if (npc.score > 20) {
      npc.score = 20
    }
  <function name="NpcScoreV" type="int"><![CDATA[
    if (not HasInt (npc, "score")) {
      npc.score = 10
      npc.maxscore = 20
    }
    if (IsDefined ("change")) {
      npc.score = npc.score + change
      if (npc.score > npc.maxscore) {
        npc.score = npc.maxscore
      }
    }
    return (npc.score)
    if (npc.score > 20) {
      npc.score = 20
    }
    else if (npc.score = 20) {
      SetObjectFlagOn (npc, "love")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score > 15) {
      SetObjectFlagOn (npc, "strongly like")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score > 10) {
      SetObjectFlagOn (npc, "like")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score = 10) {
      SetObjectFlagOn (npc, "neutral")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score > 5) {
      SetObjectFlagOn (npc, "dislike")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score < 5) {
      SetObjectFlagOn (npc, "strongly dislike")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score < 0) {
      SetObjectFlagOn (npc, "hate")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score > -5) {
      SetObjectFlagOn (npc, "despise")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "love")
      SetObjectFlagOff (npc, "loathe")
    }
    else if (npc.score > -10) {
      SetObjectFlagOn (npc, "loathe")
      SetObjectFlagOff (npc, "strongly like")
      SetObjectFlagOff (npc, "like")
      SetObjectFlagOff (npc, "neutral")
      SetObjectFlagOff (npc, "dislike")
      SetObjectFlagOff (npc, "strongly dislike")
      SetObjectFlagOff (npc, "hate")
      SetObjectFlagOff (npc, "despise")
      SetObjectFlagOff (npc, "love")
    }
    else if (npc.score > -20) {
      npc.score = -20
    }
  ]]></function>

And the code that calls said function is here:

nscore = NpcScoreV(Tiffany)
nscore = ToInt (nscore)
if (nscore < 10) {
  HideTopic (Fight with Aphrodite)
  HideTopic (Bake Sale)
  HideTopic (Yellow Envelope)
}
else {
  msg ("\"What do you want? I'm busy.\"")
}

I noticed I forgot to add the parameters so I did do that and I got this error instead.

Error running script: Error compiling expression 'ToInt (nscore)': FunctionCallElement: Could find not function 'ToInt(Int32)'

So I removed the piece of code that was trying to convert the integer to an integer and I stopped getting errors but now the topics I want hidden aren't hidden and the conversation continues. I want it to end the conversation and have it so that Tiffany won't speak to the player


It looks like nscore is already an integer.

Does it work if you remove the following line (the 2nd line in that last block you posted)?

nscore = ToInt (nscore)

Yes it did and I fixed it, and it looks like I just didn't refer to the topic correctly, now it is working fine


It looks like your making tons of progress, by the way!


A function ends when it reaches a return statement. That means that you can use return to end a loop early; but it also means that you need to put the return line at the end of your function if there's other stuff you want to do.

However, I'm not sure why you need a second function here. Do yiu only want to set all those flags if you use NpcScoreV?

If you're just using it to return the score, there's no need for that. You could do:

nscore = NpcScore(Tiffany)
if (Tiffany.score < 10) {
  HideTopic (Fight with Aphrodite)
  HideTopic (Bake Sale)
  HideTopic (Yellow Envelope)
}
else {
  msg ("\"What do you want? I'm busy.\"")
}

(in this example, the return value is still nscore, even if you don't need it… because you already have the numeric value Tiffany.score. Having only one function to do a thing is usually best practice, because with so many flags being turned on and off, it would be easy to accidentally introduce an error or discrepancy in one of the functions without realising)

Hope that makes sense, if I'm understanding correctly what you want to do.


This topic is now closed. Topics are closed after 14 days of inactivity.

Support

Forums