Cricketfoot

Veteran
Veteran
Joined
Mar 3, 2013
Messages
34
Reaction score
1
First Language
English
Primarily Uses
I'm looking to run a common event just once when the player moves.


The idea of what I want the event to do: when a player goes over a certain terrain tag I want to check for a specific key item, and if that item exists add a specific item to the player's inventory.


Right now the script works by checking the terrain tag ID. If it matches, good. If not, then it moves on. I put a 60 frame (1 second) wait so the script will only run once every 60 seconds. The one thing I don't like about it is a player can stay in one spot and the script will continue to run so they would be able to stock up on whatever items they receive. 


I'm pretty sure I can figure out a way to limit it through checking coordinates and all that jazz, but it seems like it would be much easier to run certain scripts (like getting the player x and y) just once per step instead of continually checking it.


Thanks in advance for the help!
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,647
Reaction score
15,735
First Language
English
Primarily Uses
RMMV
So you only want the player to gain one of these items using this method, ever?


If you're using a common event set to parallel processing, it means you've conditioned it by a switch, and turned the switch on at the point where you want it to start checking.  Just turn the switch off, and it'll stop running.  You can still use a wait so it doesn't do the check 60 times a second.  You could even use variables to record the 'old' and 'current' number of steps, and only do the test if they are different.


If that switch controls other things as well, then use a new switch to condition the common event, and turn it on at the same time as the other one.
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,647
Reaction score
15,735
First Language
English
Primarily Uses
RMMV
Why use a script when you can do it without one?
 

Cricketfoot

Veteran
Veteran
Joined
Mar 3, 2013
Messages
34
Reaction score
1
First Language
English
Primarily Uses
You could even use variables to record the 'old' and 'current' number of steps, and only do the test if they are different.



This is what I was planning on doing. It just seems inefficient to continually call a script when you only need to run it once per step.


Thanks everyone for the help!
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,647
Reaction score
15,735
First Language
English
Primarily Uses
RMMV
It would still run it (the common event) once every frame just to see if you've taken a step.
 

Cricketfoot

Veteran
Veteran
Joined
Mar 3, 2013
Messages
34
Reaction score
1
First Language
English
Primarily Uses
It would still run it (the common event) once every frame just to see if you've taken a step.



Yes, but it would prevent it from executing the effect of the event repeatedly when all conditions are met which is really what I'm wanting to do, I was just thinking there was a way to only execute the event once per step instead of over and over again.


How many times does a script get called in a second? I'm guessing 60? Are there any lag issues from having a bunch of parallel events running at the same time?


Shaz, are there any tutorials you recommend for getting in to writing scripts for MV? I've done a bit of JS and I wouldn't mind trying my hand at writing a few simple scripts just for fun. I haven't really looked yet, but I thought maybe you could point me to a good resource.
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,647
Reaction score
15,735
First Language
English
Primarily Uses
RMMV
Yes, there are issues with having a bunch of parallel events running at the same time.


As I said, a common event is conditioned by a switch being turned on.  When your condition is met, turn the switch off, and it will stop running.


Let's assume your switch is 15 and is called Poll For Weapon, and you're interested in tiles with a terrain of 7.  Here's what your common event could look like (the Last Steps variable will be set to 0 the first time this is run):

Code:
Control Variables: current steps = Steps
If current steps <> last steps
  Control Variables: last steps = current steps
  Control Variables: x = Map X of Player
  Control Variables: y = Map Y of Player
  Get Location Info: terrain id, Terrain ID, ({x},{y})
  If terrain id = 7
    // give item to player
    Control Switches 15 = OFF
  Branch End
Branch End
Wait 4 Frames



By conditioning the common event by switch 15, you can turn that switch on the moment you want to start checking the terrain tag - this means the common event will not run at all until you reach the map where you want this to happen.  By then turning the switch off when the item is given, you are stopping the common event from running altogether.


While it is running, it is looking every frame to see what the current number of steps is and whether it has changed.  So even if you have the conditional branch to only make it check the terrain if the steps has changed, it's still looking on every frame to see if the number of steps have changed.  Adding the wait at the end gives it a bit of a delay.  60 frames is too much because the player could cover several tiles in one second, especially if they're dashing or their speed is increased in some other way.


Turning the switch off to stop the common event running once the item has been given is better than having it run every frame (or every few frames) when you don't want it to do anything anymore.
 

Cricketfoot

Veteran
Veteran
Joined
Mar 3, 2013
Messages
34
Reaction score
1
First Language
English
Primarily Uses
The basic idea is when a player is on a certain terrain, say walking through the forest, the player will have a chance of picking up wood.


I never thought of using steps before. I think I should be able to come up with something where I either check the change in steps, or just check the change in x or y and if there is a change, turn on the switch. If there isn't a change, don't. And once the event triggers it will turn off the switch until the next step.


Thanks for taking the time to write up the example. You definitely got me pointed in the right directions.
 

Cricketfoot

Veteran
Veteran
Joined
Mar 3, 2013
Messages
34
Reaction score
1
First Language
English
Primarily Uses
I've got it working. I have an event that stores the player x and y in a variable. I created new "Last X" and "Last Y" variables, put a check if Last X/Y is different than Player X/Y, and if it is different it calls the common event.
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
44,647
Reaction score
15,735
First Language
English
Primarily Uses
RMMV
I think I misunderstood your request a bit - I assumed you only EVER want to give them one of this item.


While the common event is enabled, it will test every 7 steps to see if the player is on the appropriate terrain.  If they are, it gives them a 1 in 4 chance of receiving the item.  On average, as long as they stick to the correct terrain, they'll get a block of wood every 28 steps, if you use the numbers I've got below.  You can adjust the Step Count > 6 line to make it check more or less frequently, and the random number to give them a higher or lower chance each time.


Again, turn the switch that conditions the common event on when you enter a map where you want this checking to be done, and off again when you leave the map - this will prevent it running on maps where the conditions will never be met.  Or if it's only on one map, just use a map event set to parallel process instead of a common event, and then you don't need a conditioning switch at all (unless there are places in the story where you don't want them to be able to get wood from here - for instance, if you only want to allow it after they get a certain quest, and only until the quest is complete).

Code:
// See how many steps taken since last time we ran the test
Control Variables: Step Count = Steps
Control Variables: Step Count -= Last Steps
If Step Count > 6 (let's say we only want to check the terrain every 6 steps)
  Control Variables: Last Steps = Steps (so we start counting again with the next iteration)
  Control Variables: X = Player's Map X
  Control Variables: Y = Player's Map Y
  Get Location Info: Terrain = Terrain id of ({x},{y})
  If Terrain == 7
    // Add a bit of randomness, so the player doesn't get wood every 7 steps exactly
    Control Variables: Random = Random(1..4)
    If Random == 1
      // give item
    Branch End
  Branch End
Branch End
Wait 4 frames
 

Latest Threads

Latest Profile Posts

It's definitely discouraging when streamers give a try-to-say-it-nicely (i.e. bad) review of your game demo. It's like jumping into a cold pool. Bad initally, but it gets better with time. And you are now more alert and see better.
Some screenshots of the pub and lodging with the beginnings of clutter taking shape
TTp8uCw.png

jtC202e.png

bSXgyVl.png

eRx9PUF.png
Backgrounds are not my strong suit... :kaodes:
DazI8jt.jpg

Just realized I forgot doing the clouds... woops
Went to get some stuff for February 10, the birthday of my favorite ink monster...
I have a little one-person party for Bendy every year, complete with the life-size plushie I made. And yes, I bake a birthday cake!

Forum statistics

Threads
118,497
Messages
1,116,407
Members
155,489
Latest member
Desarr0
Top