Changing event image randomly without using event pages

Before I spend a huge amount of time on this that might all end up for naught, I wanted to get some advice on how to go about this problem. No existing threads specifically address this.

In my project, there are events strewn throughout the map that are "garbage". The player can pick up the garbage, naturally.

I have 16 different little garbage images, and I'd like the garbage events to randomly choose an image from among them when the map loads.

The garbage images are in the B Tileset.

My first thought was to create a common event that would set a variable randomly to 1-16, and then I would use Set Movement Route to set the garbage image.

However, movement route doesn't permit me to select from a Tileset image, unfortunately.

The only other solution I can figure is for each and every garbage event to be an autorun with 16 event pages, and for the first event page to set one of 16 switches to on, which would run the corresponding page and then turn that activating switch off before the engine goes on to run the subsequent garbage events.

This solution is not only time-consuming but it also feels quite nauseating from a basic coding hygiene perspective -- there has to be an easier way.

If anyone has any thoughts, I'm all ears! Thank you!!
Just make the garbage a character graphic instead of being on the tileset.

If you wanted to do the second option though, you don't have to use switches. You could just use a variable for each piece of garbage. You'd still have 16 pages, though.
Just make the garbage a character graphic instead of being on the tileset.

If you wanted to do the second option though, you don't have to use switches. You could just use a variable for each piece of garbage. You'd still have 16 pages, though.

This led me to realize that event pages have as conditions variable being greater than or equal to a number!! No simple "equal to" condition. Been working with this engine for years and never realized it was limited in that way.

I recall there being some reason that dissuaded me from putting the garbage into character images ... is there any reason that I wouldn't want to do that? Or was I genuinely hallucinating when I went about this?
you need 1 variable and 2 pages, not 16.

you can use a garbage switch on this one as well.

if garbage switch is off
control variable 1...16
control garbage switch in ON
end
if variable x == 1
change graphic
end
if varablex == 2
change garbage
end
etc (or use a scriptcall which is faster) if each has it own sheet

for (var g = 1; g <= 16; g++) {
// need to find the call for change graphic
// to draw imageName + g
}

g = 1, 2, ,3 ,4 ,5 ,6 ,7 etc. but I need to check that, and you can use erase event.
than when you transfer, turn the switch off, page 2 can handle the main graphic
which turn the switch off when you transfer.
Before I spend a huge amount of time on this that might all end up for naught, I wanted to get some advice on how to go about this problem. No existing threads specifically address this.

In my project, there are events strewn throughout the map that are "garbage". The player can pick up the garbage, naturally.

I have 16 different little garbage images, and I'd like the garbage events to randomly choose an image from among them when the map loads.

The garbage images are in the B Tileset.

My first thought was to create a common event that would set a variable randomly to 1-16, and then I would use Set Movement Route to set the garbage image.

However, movement route doesn't permit me to select from a Tileset image, unfortunately.

The only other solution I can figure is for each and every garbage event to be an autorun with 16 event pages, and for the first event page to set one of 16 switches to on, which would run the corresponding page and then turn that activating switch off before the engine goes on to run the subsequent garbage events.

This solution is not only time-consuming but it also feels quite nauseating from a basic coding hygiene perspective -- there has to be an easier way.

If anyone has any thoughts, I'm all ears! Thank you!!
How comfortable are you with scripting?

Using a parallel event on the map that runs the script calls/commands to control the 16 events on the screen at the beginning of the map and erases itself will be a way to do this.

There is a function that allows you to set the tile image for the character I believe:
JavaScript:
Game_CharacterBase.prototype.setTileImage = function(tileId) {...


Since you are on tileset B, you can specify the tileId for it in the range 0 - 255.

You can also use this script in the console log to see what tile ID it is when you select it as the image - you can do this for all the events that are using tileset B as an image ( change the 1 to the specific event you want):
JavaScript:
$gameMap.event(1).tileId()
Be sure to write the id down somewhere.

Do you mind sharing the tileset image or specifying which tiles are the garbage tiles? it would help with the process of finding the IDs and I should have a better idea of how to script it. It sounds like one instance of a parallel event could do it.

You could even do something like 'check event with note tag <garbage> and randomize tile id for those events only'.
Thanks all -- I'll be trying out both approaches, but for now, I'm curious how I can choose from a standard 12 frame character sheet from within Set Movement.

I've transferred all the garbage images to 3x4 sheets, but now I'm realizing that the "change image" from within set movement won't permit me to choose from between the 12 images.

I'm less comfortable doing all this with scripting, since I'm reluctant to include anything I can't modify and troubleshoot without help, but I'll be trying it out -- I suspect I might want to use a combination of both these approaches.

But for now, I'm interested in learning how to actually change these images.

Thanks!!!

EDIT: Realizing it would make sense to learn the scripting for this at this stage -- let's say I have $garbage1.png as a character sheet, what would the script call be to change the image to any of the 12 images on that sheet? I think I can take it from there ...
You can use it similar like this to change the event image
JavaScript:
$gameMap._events[eventId].setImage(filename, filenameIndex);

// example
$gameMap._events[eventId].setImage("Actor1", 6);

but you need a loop to control the index (0-7) if you want it randomly
between events.

[2,4,6,12].contains .... and more of the code, than a loop from 0-7
for the random index. to assign it.

or you can just call the event separately to change a random value to it.
You can use it similar like this to change the event image
JavaScript:
$gameMap._events[eventId].setImage(filename, filenameIndex);

// example
$gameMap._events[eventId].setImage("Actor1", 6);

but you need a loop to control the index (0-7) if you want it randomly
between events.

[2,4,6,12].contains .... and more of the code, than a loop from 0-7
for the random index. to assign it.

or you can just call the event separately to change a random value to it.

Got it, @ShadowDragon ... what would the script be if I'm simply doing this in a Common Event and need for the image change to apply to whichever event includes the CE?

What I'm experimenting with is having each little garbage event just point to this one CE, and within that CE I am fine with just having a 16-part conditional branch, that seems straightforward. No need to use a loop I don't think.

Any idea?
EDIT: Realizing it would make sense to learn the scripting for this at this stage -- let's say I have $garbage1.png as a character sheet, what would the script call be to change the image to any of the 12 images on that sheet? I think I can take it from there ...
If you are using a common event, be sure to reference the correct event ID - I will use ID 1 for this example.

Character Directions and Patterns for single spritesheet:

Code:
Down  (2): 0 1 2
Left  (4): 0 1 2
Right (6): 0 1 2
Up    (8): 0 1 2
Here is the script call you must use to target a specific cell (assuming you are in a common event and not using move route commands) - for direction right, pattern 2:
JavaScript:
$gameMap.event(1).setDirection(6);
$gameMap.event(1)._originalPattern = 2;
Do note that these need to be in the same script call and not two separate calls because the engine will run them separately and you will notice two changes, one right after the other, instead of just one change using one call.

If you are interested in the alternative, I'll be more than happy to post the code for loading targeted events with notetag, Tileset B usage with randomized placement on the map upon loading and randomized Tileset B images which are static and do not require direction or pattern changes. I use it for picking up plants in my game as a fun little mini-game (will probably be timed). It's a lot cleaner with just a few lines of code. I'll be sure to put notes in as well as a reference.
@bass2yang Thanks so much for this, trying it now.

If I need to reference each individual event ID then a CE isn't the best solution I don't think. So I'll be including this within each garbage event on the map. This way, I could just copy-paste that event without changing anything -- does that seem right?

It means I'd need to change the script call of $gameMap.event(1) to referencing "this event" -- would you know offhand how I might do that?

EDIT: I'm noticing that you mentioned that this script call would only work if in a common event, which would explain why I'm not having luck testing this.

I'd love to just zoom out a second to take a look at the problem for a second, because I'm realizing there were a few things I didn't realize. Pardon me bein all nitty gritty here :LZScheeze:

First scenario: running the image randomizer within each garbage event.

Seems straightforward to choose a random image this way but I'm running into eventing troubs. Basically, how would I even do this within the event? The garbage needs to be interacted with (the player walks over it to pick it up), so if I autorun the image randomizer, then set a self switch to trigger another event page, how would I then set that "pick up garbage" page to have the same image? I've experimented with both autorun and parallel pages but I'm stuck.

Second scenario: running the image randomizer in a single common event.

If the first option is as tricky as I'm finding, then maybe a CE is the best bet after all. The reason I moved away from that was because if I need to manually set the event ID for each event that's calling it, then it feels like that kind of cancels out the usefulness of using a common event to simplify things.

For either scenario: if I need to use script calls either way, is there any real difference to storing all the garbage images in a character sheet vs a tileset? At the moment, I have it set up both ways and could easily move from one to another. Maybe important to keep in mind that ultimately I will have about 50-60 different garbage images to choose from, so I would need to deploy them across multiple sprite sheets and have an additional random number to choose from among them (not a big deal imo).

Backup scenario: manually choosing the garbage image for each and every garbage event. Well why don't I just drink out of a toilet! There's no fun in that :LZSwink:

The reason I really want to get this right from the outset is because this is actually a rather important mechanic in my project, and before I deploy hundreds of garbage sprites and realize I'm doing it wrong, I want to ensure I have the best possible solution at the start.

Thanks again all!
@bass2yang Thanks so much for this, trying it now.

If I need to reference each individual event ID then a CE isn't the best solution I don't think. So I'll be including this within each garbage event on the map. This way, I could just copy-paste that event without changing anything -- does that seem right?

It means I'd need to change the script call of $gameMap.event(1) to referencing "this event" -- would you know offhand how I might do that?

EDIT: I'm noticing that you mentioned that this script call would only work if in a common event, which would explain why I'm not having luck testing this.

I'd love to just zoom out a second to take a look at the problem for a second, because I'm realizing there were a few things I didn't realize. Pardon me bein all nitty gritty here :LZScheeze:

First scenario: running the image randomizer within each garbage event.

Seems straightforward to choose a random image this way but I'm running into eventing troubs. Basically, how would I even do this within the event? The garbage needs to be interacted with (the player walks over it to pick it up), so if I autorun the image randomizer, then set a self switch to trigger another event page, how would I then set that "pick up garbage" page to have the same image? I've experimented with both autorun and parallel pages but I'm stuck.

Second scenario: running the image randomizer in a single common event.

If the first option is as tricky as I'm finding, then maybe a CE is the best bet after all. The reason I moved away from that was because if I need to manually set the event ID for each event that's calling it, then it feels like that kind of cancels out the usefulness of using a common event to simplify things.

For either scenario: if I need to use script calls either way, is there any real difference to storing all the garbage images in a character sheet vs a tileset? At the moment, I have it set up both ways and could easily move from one to another. Maybe important to keep in mind that ultimately I will have about 50-60 different garbage images to choose from, so I would need to deploy them across multiple sprite sheets and have an additional random number to choose from among them (not a big deal imo).

Backup scenario: manually choosing the garbage image for each and every garbage event. Well why don't I just drink out of a toilet! There's no fun in that :LZSwink:

The reason I really want to get this right from the outset is because this is actually a rather important mechanic in my project, and before I deploy hundreds of garbage sprites and realize I'm doing it wrong, I want to ensure I have the best possible solution at the start.

Thanks again all!
That's understandable - limiting the scope to test first before proceeding with an idea is a good mindset.

The difference between using tileset vs spritesheet is that the tileset is static with no animation/turn/pattern, it is just one image cell. It may be more efficient if you are just looking to have garbage sprites on the screen.

Question - will you be interacting with the garbage events (being able to pick it up or just be able to walk over it)?

I'll try to set it up for you using one of my tilesets so you can see it. I'll set it up with 16 events using one main map event to control all of it with 48 tileset B images to choose from randomly.

The main event will be a few script calls with two pages (one parallel, and another selfswitch to manually run the same code). All other events will only have 1-2 pages with no code or image selected since we are going for static sprites on the screen.

I'll get a video up and some images of the code and post it here later so you can see the workflow. If it is similar to what you are going for, then great - feel free to experiment with it.

Since it really only needs a few script calls, you won't be using crazy amounts of pages, conditional branches, switches, variables, or event commands.
Question - will you be interacting with the garbage events (being able to pick it up or just be able to walk over it)?

So incredibly grateful for your help here.

These garbages couldn't be more simple. They appear as an image on the screen, the player walks over them, and that calls a "garbage get" common event where they receive a random garbage.

Anything you could provide to help make this a doable thing would be incredibly appreciated.

Thanks!!
So incredibly grateful for your help here.

These garbages couldn't be more simple. They appear as an image on the screen, the player walks over them, and that calls a "garbage get" common event where they receive a random garbage.

Anything you could provide to help make this a doable thing would be incredibly appreciated.

Thanks!!
Thanks for the info - I'll see if I can implement that too. It should be as simple as adding a player touch, a self-switch command, and some code to each event (or a small common event to take care of it).

Do they receive the item "garbage" in their inventory when they pick it up?

Alright, here is what I have. Please let me know if you have any questions.


Here are screenshots:

I am starting Tileset B on index 8 (0-7 is first row, 8-15 is second, etc.) - Only in the editor though - the actual sheet will look different since it divides the sheet into columns. Here is a better reference for tileset and how to get their IDs:

Tileset B (indexes 0-255) with 16 "garbage" events, Harold (event 17) as parallel event to control everything, and 8 treasure chests that do nothing so that you can see that the script only targets the events with the note tag <garbage>:
s1.png


Event 1-16 - Page 1: Note <garbage> with Common Event "Get Garbage".
- Priority: Same as characters
- Trigger: Player Touch
- No image set
s2.png


Events 1-16 - Page 2: Self Switch A checked on
- Priority: Below characters
- Trigger: Action Button
- No image set
s3.png


Harold Event 17 - Page 1: script and comments in image
- Priority: Same as characters
- Trigger: Parallel

Script Code for convenience:
JavaScript:
const eventId = $dataMap.events.reduce((acc, cur) => {
  if (cur?.meta.garbage) acc.push(cur.id);
  return acc}, []);
for (const id of eventId) {
  this.character(id).setTileImage( Math.randomInt(49) + 8 );
};
$gameVariables.setValue(1, eventId);
Math.randomInt(49) function generates a random number between 0 and the number you specify (in this case, the numbers range from 0-48). Adding 8 to it allows the number to always start no lower than 48 so it essentially becomes 48-56.

s4.png


Harold Event 17 - Page 2: Self Switch A is checked on
Pretty much the same info with a few additions to reset the self switches of the <garbage> events and reset/randomize the tileset images of those events.
- Priority: Same as characters
- Trigger: Action Button

Script Call:
JavaScript:
const gSS = $gameSelfSwitches;
for (const key in gSS._data) {
  if ( key.split(',')[0] == this._mapId &&
       $gameVariables.value(1).includes(Number(key.split(',')[1])) )
       gSS.setValue(key, false);
};
JavaScript:
for (const id of $gameVariables.value(1)) {
  this.character(id).setTileImage( Math.randomInt(41) + 8 );
};
Edit: I used 49 in the first page but 41 here, feel free to use the values you need - here, the range becomes 40-48)
s5.png


Treasure Chest Events 18-25: Image set to Treasure. Just a placeholder event.
s6.png


Common Event "Get Garbage":
Play SE Book1
Add Items to Inventory (Garbage is set to Key Item to separate it from normal inventory)
Control Self Switch A = ON allows each individual event to use their own selfswitch when it runs this common event.
s7.png



Hope this is what you were looking for! You will have to customize as needed but this is pretty much the workflow if you are going this way and wanting to simplify.

The issue, as with all evals, is that error detecting and console errors are not helpful so you really need to know where and how to approach this and deduce your way through possible errors. Otherwise, you are welcome to use these calls and work through them.

First off @bass2yang this is about the most comprehensive, clearly-written, most generous use of time I've ever seen on this board, and I'm floored by your effort in helping me out to this extent to create random garbage. It is both hilarious and also immensely appreciated.

That said, I'm having some issues with it -- the images aren't showing up on the garbage events. I'll lay out what I've got and hopefully you or someone else could set me straight. I'm motivated to get this one working for sure.

Here is my Control Harold:

2024-05-01 01_24_33-ID_009 - Event Editor.png


According to the tileID guide you linked (which was highly useful btw and I didn't know about before) My B Tilesheet has garbage in ID's 40 through 47, but when I tried Math.randomInt(48) + 40, the images weren't loading properly, so I am just leaving it how you put it just to test until I get any image in the garbage.

I'm not faffing about with Harold's garbage reset since that seems best left until I get this basic functionality working.

Here's my Tilesheet B
SF_Outside_B.png

Just for reference. And I've confirmed that the map I'm testing on is using this tileset (SF Outside). Yes I have some empty tiles in there but I've tested this using 25 events a whole bunch of times and I never get any images at all, so I don't think it's picking the empty tiles 100% of the time.

Here's my test garbage event:

2024-05-01 01_24_43-ID_008 - Event Editor.png


And here's my test garbage get common event:


2024-05-01 01_34_30-NHNT - RPG Maker MV.png


Everything seems to work fine except the events never seem to have any images on them.

Any thoughts?

And truly, thanks again, I wish there were some RPGMaker forum awards I could nominate you for because this is some legit godmode forum troubleshooting :LZSkiss:
First off @bass2yang this is about the most comprehensive, clearly-written, most generous use of time I've ever seen on this board, and I'm floored by your effort in helping me out to this extent to create random garbage. It is both hilarious and also immensely appreciated.

That said, I'm having some issues with it -- the images aren't showing up on the garbage events. I'll lay out what I've got and hopefully you or someone else could set me straight. I'm motivated to get this one working for sure.

Here is my Control Harold:

View attachment 303703

According to the tileID guide you linked (which was highly useful btw and I didn't know about before) My B Tilesheet has garbage in ID's 40 through 47, but when I tried Math.randomInt(48) + 40, the images weren't loading properly, so I am just leaving it how you put it just to test until I get any image in the garbage.

I'm not faffing about with Harold's garbage reset since that seems best left until I get this basic functionality working.

Here's my Tilesheet BView attachment 303704
Just for reference. And I've confirmed that the map I'm testing on is using this tileset (SF Outside). Yes I have some empty tiles in there but I've tested this using 25 events a whole bunch of times and I never get any images at all, so I don't think it's picking the empty tiles 100% of the time.

Here's my test garbage event:

View attachment 303705

And here's my test garbage get common event:


View attachment 303706

Everything seems to work fine except the events never seem to have any images on them.

Any thoughts?

And truly, thanks again, I wish there were some RPGMaker forum awards I could nominate you for because this is some legit godmode forum troubleshooting :LZSkiss:
Use Math.randomInt(8) + 41

That will give you random integers between 40 and 48.

My apologies for the confusion, the way I wrote it was a mistake even though the code was correct so that probably threw you off as well regarding the randomInt. I fixed it in the above post.

Also, check to make sure each garbage event has self switch A checked ON for page 2 just in case.

The blank row of tiles below the garbage tiles are values 48-56, which are actually also in the numbers generated by the code you put in as Math.randomInt(48) + 8 in the Harold Event, making it pretty much blanks generated.

Let me know if this works for you. Thanks!
Thanks again @bass2yang , unfortunately it still isn't creating the images. I've changed the script as you indicated, and then just let it choose from between 1-200 and it's not showing anything on the garbages.

Would you have any advice on how I might troubleshoot?

Here's my Control Harold now, everything else is the same. The garbage events are unchanged, and have the self switches on second event page.

2024-05-02 00_22_30-Window.png


Thanks!
Thanks again @bass2yang , unfortunately it still isn't creating the images. I've changed the script as you indicated, and then just let it choose from between 1-200 and it's not showing anything on the garbages.

Would you have any advice on how I might troubleshoot?

Here's my Control Harold now, everything else is the same. The garbage events are unchanged, and have the self switches on second event page.

View attachment 303824

Thanks!
What is on page 2 of the Harold Event? Also, is self switch A checked for page 2?

Can you show me a screenshot of the console? If you press f8 ot f12 during gameplay, that will show you the console. I just want to double check everything as well.
What is on page 2 of the Harold Event? Also, is self switch A checked for page 2?

Can you show me a screenshot of the console? If you press f8 ot f12 during gameplay, that will show you the console. I just want to double check everything as well.
2024-05-02 02_42_15-Window.png


Sure thing -- I don't have the scripts to reset all the garbages in the second event page -- just the self switch.

Interesting you mention the console -- I hadn't thought to check on that. Indeed it's popping off. What do you make of it? I wouldn't have thought that any plugins would be presenting conflicts but I will defer to you!

Note that the top two are minor flags that have been in place for years and years.

2024-05-02 02_44_01-Window.png


I'm going to test without the Yanfly plugin. Will report back!

EDIT: It's looking like your script might be conflicting with some basic Yanfly core stuff, because after disabling the flagged plugin, I'm still getting this:

2024-05-02 02_49_06-Window.png


I find this fairly interesting, and curious if you can suss out the conflict. Everything else about the garbage events works beautifully!

Thanks for all your help!

EDIT: Please keep in mind, if this presents too much of an issue, I'm perfectly happy to simply put manual garbage images on my events. I'd love to get the random images working but I would feel super bad if you devoted any serious time towards hunting down conflicts. I think we both appreciate how cool random elements are but I'm mindful of your time.

Thanks again!! :LZSkiss:
Type in process.versions in the console and expand the info.

We are looking to see which node/nwjs versions you have.

My current guess is that if you have node version below 13 and/or nwjs version below 44, it does not support option chaining (?.) which could be why we are having issues. I am going to guess based on the PIXI version 4.5.4 and the last update MV forced on everyone that you probably are using nwjs 0.29 or something with node 12 or below.

We'll approach it slightly different just to make sure.

Change the script by adding this code above the const eventId:
JavaScript:
let eventId = $dataMap.events.filter(event=>event);
Remove the word const from const eventId and remove the question mark from cur?.

It should look like
let eventId = $dataMap.events.filter...
eventId = $dataMap.events.reduce...

Try that and let me know if anything changes.
Here's the new harold event, per your instructions.

2024-05-02 03_06_16-Window.png


Unfortunately still no images 🙁 The console:

2024-05-02 03_05_17-Window.png


I believe in you @bass2yang lol!

Let's get this garbage randomized 😀

Original thread: https://forums.rpgmakerweb.com/threads/changing-event-image-randomly-without-using-event-pages.168137/