(Solved) Need help scripting messages

Hi folks,

I am trying to create a script which lets the player select an item, converts the item ID to the name string, checks the string, and if it matches the required value, shows some dialogue.

Here is my event:
fsdfsdcfs.JPG


I am having a few problems, though.

The first is that I want to show one message, then another message. Using "$gameMessage.add" seems to just keep adding to the current message. I checked the script call reference doc and couldn't find any way to start a new message.

The second and much more confusing issue is that, as you can see in the image, there are a few basic common events set to run before and after this script. The ones before just zoom in the game screen and show an image, and the ones after zoom it back out and clear that image.

For some reason, ALL of the common events run before this script, even though it is between them. The game lets me select an item, zooms in and shows the image, immediately zooms out and clears the image, and then shows the message in my script, which is between them.

Replacing my script with a typical "show text" command works properly - the zoom in and image happen, the text shows, and then it zooms back out and clears the image when the message is closed. I have no clue how things are running out of sequence when it is in a script. I have tried copying this to other events, and it always works the same way - the script runs after all the common events. Strangely, if I add regular "show text" command after my script, it will run everything correctly - zoom, then script text, then command text, then zoom out.

Any help is much appreciated!
For your first problem, try adding the following script in-between your $gameMessage.add calls:
JavaScript:
$gameMessage.newPage();

For your second problem, you can try adding wait commands above your common events and see if that helps.
For your first problem, try adding the following script in-between your $gameMessage.add calls:
JavaScript:
$gameMessage.newPage();

For your second problem, you can try adding wait commands above your common events and see if that helps.

The first bit did work to split the messages up, though strangely there is now a wait for input after "Oh, " in the first message, which is a little confusing.

Adding wait commands sadly has not helped with the common events. Strangely, adding a text command with nothing but "\^" in it (so it instantly auto-completes) after the script does fix it, but I would rather figure out what is actually going on there as this seems like a weird requirement to be able to run a script, haha.
Just to mention it, it doesn't hurt anything, but that first line of code is superfluous.

If the only purpose of putting the item's name into your variable is to check that name, just do it.
Code:
if ($dataItems[$gameVariables.value(15)].name == "Pizza")

It's also not clear why you're using script for this at all.

All you need to do is use the Select Item command like you are at the top, then do Conditional Branch -> Variable -> 15 -> = (id of pizza)

Then you can continue doing the Show Text with the normal event command.

Even if there was a reason you couldn't do that, you still wouldn't need to be using code for the game message. Since the only place you were referencing a variable was in the if, you'd just paste that conditional into the Script field of Conditional Branch - again, construct the Show Text after that with regular event commands.
Just to mention it, it doesn't hurt anything, but that first line of code is superfluous.

If the only purpose of putting the item's name into your variable is to check that name, just do it.
Code:
if ($dataItems[$gameVariables.value(15)].name == "Pizza")

It's also not clear why you're using script for this at all.

All you need to do is use the Select Item command like you are at the top, then do Conditional Branch -> Variable -> 15 -> == (id of pizza)

Then you can continue doing the Show Text with the normal event command.

Even if there was a reason you couldn't do that, you still wouldn't need to be using code for the game message. Since the only place you were referencing a variable was in the if, you'd just paste that conditional into the Script field of Conditional Branch - again, construct the Show Text after that with regular event commands.

Ah, good catch! That is a pretty redundant way of formatting that, haha.

To give some context, this is the beginning stages of what will become a pretty lengthy list of "liked" items by an NPC, which will have 50+ items added and will change over time. The idea is to format it in a way that is simple to add to and easy to read/edit.

Using regular commands (or a conditional command checking a script condition) is just too clunky to say, check if the selected item is one of 30 potential items they love, then running some commands, and if not, checking another 30 items they like, then 30 they hate, then the remaining "else" items they are neutral on.

Using a script like this, I can just add items to the pool easily as I develop, and check their string names rather than needing to reference all of their IDs.

For this reason, (as far as I know), everything within this conditional script needs to be in the script itself, as I can't set an if statement by script, then run normal commands, and then close that statement in another script.
For some reason, ALL of the common events run before this script, even though it is between them. The game lets me select an item, zooms in and shows the image, immediately zooms out and clears the image, and then shows the message in my script, which is between them.
Your script does run in-between them, it just doesn't do what you think it does. It doesn't actually display a message, just adds it to the Game_Message (which will display it when it's next updated, not immediately).
For this reason, (as far as I know), everything within this conditional script needs to be in the script itself
That's not true - you can just create a regular conditional branch, and select a "Script" condition. No JavaScript if statements needed.

I agree with Turan that this will be much simpler with event commands. But in case you really do want to go down the path of complex eventing within scripts, here is a good place to start. Though it will require a fair amount of JavaScript proficiency.
For this reason, (as far as I know), everything within this conditional script needs to be in the script itself, as I can't set an if statement by script, then run normal commands, and then close that statement in another script.
Of course you can, exactly as I described above. (although I don't know what you mean by "close that statement in another script")

If you want something you can paste into a Conditional Branch to check for multiple items, you do Select Item into variable 15 like you are now, then do this:
Code:
[12, 22, 84, 76].includes($gameVariables.value(15))
The numbers in the brackets are the IDs of the items the NPC likes. List as many as you wish.

Again, that just goes into the Script field of Conditional Branch then you can put regular event commands inside, including a Script if it's actually necessary for some reason.
Of course you can, exactly as I described above. (although I don't know what you mean by "close that statement in another script")

If you want something you can paste into a Conditional Branch to check for multiple items, you do Select Item into variable 15 like you are now, then do this:
Code:
[12, 22, 84, 76].includes($gameVariables.value(15))
The numbers in the brackets are the IDs of the items the NPC likes. List as many as you wish.

Again, that just goes into the Script field of Conditional Branch then you can put regular event commands inside, including a Script if it's actually necessary for some reason.

Ah, that's a great way to format it! I was doing really long || lists to check the value over and over. I didn't know I could put them all in a list like that and check once. That definitely makes a conditional script a lot more appealing (I was just avoiding it because having only one long line of text in a small field was annoying to add a long list to and edit).
FYI and back to your original question/problem:
If several different code sources try to use the same display port at the same time, it will cause a mess.
To prevent that the message system is basically "outsourced" as a non-programmer would say. The message controller works on its own, handling a queue of messages that are supposed to be displayed.
both the event command "show text" and the script command "addmessage" simply put their text into that queue, to be displayed when the message controller has time to handle it.

However, the event command has the entire framework that pauses the event interpreter to give the message controller time to handle that queue - and that framework is missing when you add a text directly.
and your script is linear, it completely ignores anything else and does not allow the game loop to process other objects while it is running.

show text and call common event are the two event commands mostly involved with such queues and are actually easier to use as event commands than to mess with their script equivalents exactly because they use a queue instead of direct action.
FYI and back to your original question/problem:
If several different code sources try to use the same display port at the same time, it will cause a mess.
To prevent that the message system is basically "outsourced" as a non-programmer would say. The message controller works on its own, handling a queue of messages that are supposed to be displayed.
both the event command "show text" and the script command "addmessage" simply put their text into that queue, to be displayed when the message controller has time to handle it.

However, the event command has the entire framework that pauses the event interpreter to give the message controller time to handle that queue - and that framework is missing when you add a text directly.
and your script is linear, it completely ignores anything else and does not allow the game loop to process other objects while it is running.

show text and call common event are the two event commands mostly involved with such queues and are actually easier to use as event commands than to mess with their script equivalents exactly because they use a queue instead of direct action.

Ah, this makes sense. Thanks for the clarity!

Luckily, using the script conditional is going to work for me after all, so no need for all of these message scripts anymore.

Original thread: https://forums.rpgmakerweb.com/threads/solved-need-help-scripting-messages.170548/