String with two string variables

Started by tor.brandt, Mon 24/10/2016 14:23:21

Previous topic - Next topic

tor.brandt

I'm trying to modify the LW_BASS module, such that when an inventory item is active, the action text will display "Use [active inventory item] with [the hotspot/object/etc. which the cursor is currently over]".

What is wrong with this line:
Code: ags
lblActionText.Text = ("Use %s with %s", player.ActiveInventory, Game.GetLocationName(mouse.x, mouse.y));
?

AGS says there is a parse error.
Now, there may be other errors, and I'm not even sure if I'm using player.ActiveInventory and/or Game.GetLocationsName() correctly here, but I just can't see the parse error.

Crimson Wizard

#1
Firstly, idk if that is a typo, but you are not calling actual function, just typed parameters to it. You are missing "String.Format" before opening bracket.

Code: ags

lblActionText.Text = String.Format("Use %s with %s", player.ActiveInventory, Game.GetLocationName(mouse.x, mouse.y));


Secondly, ActiveInventory cannot be converted to string using "%s", because its.... well, InventoryItem, not String. You probably need player.ActiveInventory.Name.
NOTE: you may not get any compile errors using just player.ActiveInventory, but the actual outcome will be either runtime error, or some garbage printed instead of meaningful string.

tor.brandt

Oooh I see, I wasn't aware that I needed the String.Format part.

And thanks for the player.ActiveInventory.Name correction, now it's working perfectly :smiley:

SMF spam blocked by CleanTalk