Function: Replace not found?

Gng

Quest won't find the Replace function. Is there another way to subtract a string from a string?


The Replace function is the one you want.

Can you show the code that isn't working, or the error message you're getting?

If it says something like Could Not Find Function: Replace(Object, String, String), look at the message. In that example, the problem is that the first parameter you're passing it is not a string. If you spell the name of an attribute wrong, for example, its value will most likely be null, which counts as an object.

A lot of built-in functions are strongly typed, which means that you need to make sure the types actually line up.

Another way this problem can come up is if you do something like:

recoloured_description = Replace(GetAttribute (room, "description"), "red", "blue")

This looks like it should work… but GetAttribute's signature says its return type is "Any" rather than "String". So Quest looks for a function with the signature Replace(Any, String, String) and doesn't find it - it never even attempts to run the code. Making this example work has two solutions:

recoloured_description = Replace(GetString (room, "description"), "red", "blue")

or

desc = GetAttribute (room, "description")
recoloured_description = Replace(desc, "red", "blue")

In the first case, you use a different function which always returns a string. In the second case, you get the value first, so that Quest assesses the second line based on the actual type of the value, rather checking if Replace can handle whatever value GetAttribute is capable of creating.

Similar issues are why it's advised to use StringListItem rather than ListItem, for example.

If this suggestion doesn't help you, it would probably be good to share the line of code you're using.


Gng

As always thanks for these tips. I wasn't aware of "any" type.

I just tried again and the code works now, I don't what I was doing wrong, thank you!

Here's the code for anyone interested:

msg ("You equip your armor.")
player.armoramount = this.acid
list remove (this.inventoryverbs, "Drop")
list remove (this.inventoryverbs, "Equip")
list add (this.inventoryverbs, "Unequip")
this.alias = this.alias + " (equipped)"
player.equippedarmor = this
----------------------------------
msg ("You unequip your armor.")
player.armoramount = 0
list add (this.inventoryverbs, "Drop")
list add (this.inventoryverbs, "Equip")
list remove (this.inventoryverbs, "Unequip")
this.alias = Replace(this.alias, " (equipped)", " ")
player.equippedarmor = "non"

I just tried again and the code works now, I don't what I was doing wrong, thank you!

In that kind of situation, the most likely cause of bugs is if the armour doesn't have an alias.

Most of the built-in functions will use the object's name if it doesn't have an alias; but this.alias will have the value null, and so trying to use Replace on it will fail because null isn't a string. There are several pieces of built-in code which will create an alias by copying the name; so it could be that the bug only occurs if you haven't done one of those things.
In this case, I would suggest that you either make sure the object has an alias. Or even better, use GetDisplayAlias so that if someone else copies your code, it will automatically check for the most common mistake.

So the lines would be:

this.alias = GetDisplayAlias (this) + " (equipped)"

in the first one, and

this.alias = Replace(GetDisplayAlias (this), " (equipped)", " ")

in the second.


Gng

Much appreciated. That's a nice tip.
The armor has an alias, I think that I did the Replace function wrong somehow.


I must be missing something in regards to Replace. After following the above steps using GetString on my variables, to make sure Replace was getting called with (string input, string old text, string new text), I still got an error. Even running the following test code (in the Web Editor) fails:

Replace ("bananas are delicious", "bananas", "fishsticks")

When the player hits that part of the script I get the following:

Error running script: Function not found: 'Replace'
Error running script: Object reference not set to an instance of an object.

Any thoughts on what I'm missing here?


@grumbleputty

Are you just putting that on a line by itself? I'm not sure what you expect it to do with the result.

If you want it to display the result, you'd do something like:

msg (Replace ("bananas are delicious", "bananas", "fishsticks"))

or you could put the result into a variable:

somevariable = Replace ("bananas are delicious", "bananas", "fishsticks")

or

player.motto = Replace ("bananas are delicious", "bananas", "fishsticks")

The purpose of the `Replace function is to return a modified string. Now, it's weird that it gives an error message rather than just doing nothing; but that's how many built-in functions work for some reason.

So… do something with the replaced string, and it should work fine.


Oh...now I just feel silly


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

Support

Forums