Error with custom function

Hello, so I am making a npc score function for npc relationships but the function won't work because it won't recognize the npc object and says its an unknown object or variable. I am using the npclib and the parameters of the function are obj and npc.

d = NewStringDictionary()
dictionary add (d, "nscore", nscore)
object.parent = npc
npc.parent = obj
set (npc, "score", 10)
set (npc, "maxscore", 20)
nscore = npc.score + "/" + npc.maxscore

Hello.

What are the function's parameters?

If that is your entire function's script, the parameters would need to be nscore, object,npc, and obj. Otherwise, none of those variables are defined in that script.


Hello, yes those are the parameters, but when I put all of those parameters in, it raises this error:

Error running script: Too few parameters passed to NpcScore function - only 2 passed, but 4 expected

So I tried to fix it by removing parameters but it raised the other error


If you can copy the error it prints and paste it here, we'd have a better idea of what is happening.

It might also help if we could see the code that is calling that function.


d = NewStringDictionary()
dictionary add (d, "nscore", nscore)
object.parent = npc
npc.parent = obj
set (npc, "score", 10)
set (npc, "maxscore", 20)
nscore = npc.score + "/" + npc.maxscore

Every thing that is red could be problematic, depending on:

  • what all parameters were included when you created the function
  • what arguments are being passed when that function is called

Let's say (just for example's sake) that function's name is Foo.

In full code view, with each of those parameters included, the way you have posted it and explained it, that probably looks like this:

   <function name="Foo" parameters="nscore, object, npc, obj">
    d = NewStringDictionary()
    dictionary add (d, "nscore", nscore)
    object.parent =  npc
    npc.parent = obj
    set (npc, "score", 10)
    set (npc, "maxscore", 20)
    nscore = npc.score + "/" + npc.maxscore
  </function>

I'm not saying it should look like that, by the way. I'm just saying that's the way it seems like it would look as described.


The code to call that would be something like:

Foo ("42", bar, Bob, room)

Depending on the objects bar, Bob, and room actually existing in the game.


It would also better to use object1 and object2 rather than object and obj, to avoid confusion.


Also, it looks like maybe you want d to do something, but I don't see that it does anything. And it doesn't exist after this script runs, because it is a local variable.


I don't know that nscore is actually being used either, really. And if you are passing an object attribute as this parameter, this function won't change that attribute. If that's the goal, you could add this after that last line of code:

set (npc, "nscore", nscore)

Error running script: Too few parameters passed to NpcScore function - only 2 passed, but 4 expected

What is the code you use to call NpcScore?


Hi, so the function is called NpcScore, the error raised was as mentioned Error running script: Too few parameters passed to NpcScore function - only 2 passed, but 4 expected

and the code that calls the function is here:

NpcScore (npc, nscore)
npc.score = npc.score -1
msg ("Your relationship with Tiffany has gone down by 1. ")
msg (nscore)

I was trying to add nscore to a string dictionary but I think I accidentally just made a new dictionary named "d" and nscore was supposed to display the npc score in a status attribute format by formatting it like npc.score/npc.maxscore so it would display like !/10 in a similar way to the player status score attribute.


Okay, I see.

Well, sort of, I think, haha. (I'm not making fun. It's hard to describe what we want code to do, and it's hard to share code without posting too much or not enough, haha.)

  // Create a new string dictionary
    d = NewStringDictionary()
  // Add an item to it
    dictionary add (d, "nscore", nscore)
  // That dictionary is never used again, and it does not exist once this script closes
  // ---
  // I'm assuming we are giving an object to an npc somewhere in this script
    object.parent =  npc
  // Then  we move the npc to a different location?
    npc.parent = obj

Can you post the first line of your function from full code view?

It should look something like this:

   <function name="Foo" parameters="nscore, object, npc, obj">

This is the function code you asked for right?
<function name="NpcScore" parameters="npc, obj, nscore, object" type="int">
I am trying to add a relationship score to npcs which is named "nscore" so I am basically setting a npc score for every npc with a max of 20

I didn't mean to give any objects or move the npc I just wanted to have it so it defines the object in question as an npc and creates a score for that specific npc. But I am still figuring out how the function and parameter coding works and how the npclib library works and how to take said object that is an npc and add a score.

In other words I am trying to define the object which is a character so lets say the character is bob. I am trying to define the object "bob" as an npc and add an npc score to bob.

But what if I want to add it to all my npcs, thats what this function is supposed to do, when I run it, it is supposed to create a npc score for the npc in question (in this case bob).


From what I get, I don't need the obj and object parameters because it is just moving around bob and giving objects to bob so the function could be reduced to this

  </function>
  <function name="NpcScore" parameters="npc, nscore" type="int">
    set (npc, "score", 10)
    set (npc, "maxscore", 20)
    nscore = npc.score + "/" + npc.maxscore

and the calling function code would look like this

NpcScore (npc, nscore)
          npc.score = npc.score -1
          msg ("Your relationship with Tiffany has gone down by 1. ")
          msg (nscore)

But when I run the code I still get this error

Error running script: Error compiling expression 'npc': Unknown object or variable 'npc'
And it doesn't seem to be adding the attributes "score" and "maxscore" to bob


And it doesn't seem to be adding the attributes "score" and "maxscore" to bob

Your code doesn't mention bob anywhere. You tell it to add those attributes to an object named npc.

If the npc is named bob (its name is exactly bob), that code should be:

NpcScore (bob, nscore)
bob.score = bob.score -1
msg ("Your relationship with Tiffany has gone down by 1. ")

You need to use the name of the npc object, so that the function knows which object to add the attributes to.

However, the nscore attribute still makes no sense. In the line NpcScore (bob, nscore), "nscore" refers to a variable that exists only outside the function. In the line <function name="NpcScore" parameters="npc, nscore" type="int">, "nscore" refers to a variable which only exists inside the function.

They are different variables even though they have the same name.
The inside nscore is created with the same value as the outside nscore, but changing the inside one doesn't change the outside one. If you want to get something out of a function, you need to use return. Like this:

Function code:

<function name="NpcScore" parameters="npc" type="string">
    npc.score = 10
    npc.maxscore = 20
    return (npc.score + "/" + npc.maxscore)
</function>

and the code that calls it:

nscore = NpcScore (bob)
bob.score = bob.score - 1
msg ("Your relationship with Tiffany has gone down by 1. ")
msg (nscore)

(replacing bob with the actual name of the npc object you want to set the attributes for)

However… nscore in this case will always be "10/20". It can't be anything else, because it's generated immediately after setting those initial values.

This also means that if the NPC's score and maxscore have already been set, they will be overwritten This probably isn't what you intended. In this case, I think it might be better to have a function which sets the initial values if they haven't already been set, then adds or subtracts a number, and then returns your nscore value after the change is made. So it would be:
Function code:

<function name="NpcScore" parameters="npc, change" type="string">
  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)
</function>

and the code that calls it:

nscore = NpcScore (bob, -1)
msg ("Your relationship with Tiffany has gone down by 1. ")
msg (nscore)

All off the top of my head; typing on mobile so it's a bit harder to check any of this. Hope I'm making sense.


Ohhh okay I see what you mean now, I have to define it when I call the function.


thank you!


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

Support

Forums