Push a box using javascript.

I try to push a event using javascript on player touch.
Of course I know its can be done easy with event, but curently I try to do some test for learning.

I use the follow code.
Code:
this.event().moveStraight($gamePlayer.direction());
if (this.event().isMoving()){
$gamePlayer.moveForward();
}
I got error undefined is not a function.

I try to look inside rpg_object.js but didt find yet what i looking for.

Expected result: If player try to push the box but the box cant be push its do nothing, else, the objet got push and player move to.
Well if you're doing this in an event script call, your first problem is that "this" is an instance of Game_Interpreter, and Game_Interpreter doesn't have an event() function, so the code is wrong right off the bat.

I'm not going to give you the whole thing right off the bat because if I do all the work for you, you won't learn it for yourself, but here's a starting point for your first line:

"this" is the interpreter. The interpreter has an eventId() function. In order to access the event itself, you need to find the event on $gameMap that has that ID.
I just played around with this and I learned pushing an event can be achieved with two simple lines of event script, with the trigger being "Player Touch".
PHP:
var p = $gamePlayer;
$gameMap._events[this._eventId].moveStraight(p._direction);
p.moveStraight(p._direction);
push.gif
I just played around with this and I learned pushing an event can be achieved with two simple lines of event script, with the trigger being "Player Touch".
PHP:
var p = $gamePlayer;
$gameMap._events[this._eventId].moveStraight(p._direction);
p.moveStraight(p._direction);
OK thanks, but why you code didt lead to a player jam if objet was blocked... didt understand but great.
For unknow reason when i push objet and change direction but immetiatly when back to original position.
I try to fix it...
I tested this again and the player doesn't get jammed.

I'm not sure what could be causing this problem.

Do you mean to say, that if pushed up against a wall, you want the object to slide in any available direction to avoid jams?
This should do that.
PHP:
var d = $gamePlayer._direction; var ev = $gameMap._events[this._eventId];
if(ev.canPass(ev._x, ev._y, d)){
  ev.moveStraight(d);
}else{
  var dir = [2,4,6,8]; dir.splice(dir.indexOf(d), 1);
  while(dir.length > 0){
    var d2 = dir[Math.floor(Math.random()*dir.length)];
    if(ev.canPass(ev._x, ev._y, d2)){ev.moveStraight(d2); break;}else{dir.splice(dir.indexOf(d2), 1);}
  }
} $gamePlayer.moveStraight(d);

slidesides-o.gif


If this object pushing is for a Sokoban game and you need to reset to the original positions, you might want to consider transfer to another map and back.
I tested this again and the player doesn't get jammed.

I'm not sure what could be causing this problem.

Do you mean to say, that if pushed up against a wall, you want the object to slide in any available direction to avoid jams?
This should do that.
PHP:
var d = $gamePlayer._direction; var ev = $gameMap._events[this._eventId];
if(ev.canPass(ev._x, ev._y, d)){
  ev.moveStraight(d);
}else{
  var dir = [2,4,6,8]; dir.splice(dir.indexOf(d), 1);
  while(dir.length > 0){
    var d2 = dir[Math.floor(Math.random()*dir.length)];
    if(ev.canPass(ev._x, ev._y, d2)){ev.moveStraight(d2); break;}else{dir.splice(dir.indexOf(d2), 1);}
  }
} $gamePlayer.moveStraight(d);

slidesides-o.gif


If this object pushing is for a Sokoban game and you need to reset to the original positions, you might want to consider transfer to another map and back.

Sorry maybe i get unclear. I said that your code actually work, its didt get jam and that surprise me.
Usually when we ask player to do action he cant he jam but not with your code. I got surprise that all.

My only actual concern was that when I push an objet that was animater like a vehicule or npc, they didt keep direction where i push them.
To keep direction...
PHP:
$gameMap._events[this._eventId]._directionFix = true; // to keep direction
But I think you mean use the same as player like driving the vehicle?
PHP:
$gameMap._events[this._eventId]._direction = $gamePlayer._direction;
$gameMap._events[this._eventId]._directionFix = true;
my problem is the oposive. when I push the objet, during the push the object turn. its a vehicule that I push so its logicial...
But as soon I finish to push its when back to the original position.
Can you show a screenshot of what you mean, XGuarden?
If by original position, you mean original direction? If I understand this correctly, _directionFix is the solution.
PHP:
var d = $gamePlayer._direction; var ev = $gameMap._events[this._eventId];
ev._directionFix=true;
if(ev.canPass(ev._x, ev._y, d)){
  ev._direction=d;ev.moveStraight(d);
}else{
  var dir = [2,4,6,8]; dir.splice(dir.indexOf(d), 1);
  while(dir.length > 0){
    var d2 = dir[Math.floor(Math.random()*dir.length)];
    if(ev.canPass(ev._x, ev._y, d2)){ev._direction=d2;ev.moveStraight(d2);break;}else{dir.splice(dir.indexOf(d2), 1);}
  }
} $gamePlayer.moveStraight(d);

It looks like this.
pushdir-o.gif


One thing this event script is missing is _directionFix "off". That might need a SetMovementRoute wait 30 frames and switch direction fix off again.
If by original position, you mean original direction? If I understand this correctly, _directionFix is the solution.
PHP:
var d = $gamePlayer._direction; var ev = $gameMap._events[this._eventId];
ev._directionFix=true;
if(ev.canPass(ev._x, ev._y, d)){
  ev._direction=d;ev.moveStraight(d);
}else{
  var dir = [2,4,6,8]; dir.splice(dir.indexOf(d), 1);
  while(dir.length > 0){
    var d2 = dir[Math.floor(Math.random()*dir.length)];
    if(ev.canPass(ev._x, ev._y, d2)){ev._direction=d2;ev.moveStraight(d2);break;}else{dir.splice(dir.indexOf(d2), 1);}
  }
} $gamePlayer.moveStraight(d);


One thing this event script is missing is _directionFix "off". That might need a SetMovementRoute wait 30 frames and switch direction fix off again.
I like your code that wil be usefull eventually. But I dont need it to move if in border. But you save me another question because later some event will react like that =:0)

My curent working code:
var p = $gamePlayer;

Code:
var p = $gamePlayer;
$gameMap._events[this._eventId].moveStraight(p._direction);
p.moveStraight(p._direction);
$gameMap._events[this._eventId]._direction = p._direction;

I just dont get why I have to put the last line. For exemple, if the event look down and I push him left, he will look left while moving and immediatly after look back down if I didt put the past line. Its the logic that I didt get.
This is a little bit oversimplified, but this is the best way to singularly plugin one event without having to plugin the whole cast. This is event script though.

I got this guy to look where he moves, unless he also needs to look where he is being pushed.
PHP:
var p = $gamePlayer; var ev = $gameMap._events[this._eventId];
if(ev.isMoving2 === undefined){
  ev.isMoving2 = ev.isMoving; ev.isMoving = function(){
    var move = this.isMoving2.call(this);
    this._directionFix = move;
    return move;
  };
} ev.moveStraight(p._direction);
p.moveStraight(p._direction);
ev._directionFix = true;
ev._direction = p._direction;
pushit3-opt.gif


Now that I think about it, if you don't want him to look where he moves, you could just keep using the event script you got and tick the box to turn direction fix on.
This is a little bit oversimplified, but this is the best way to singularly plugin one event without having to plugin the whole cast. This is event script though.

I got this guy to look where he moves, unless he also needs to look where he is being pushed.
PHP:
var p = $gamePlayer; var ev = $gameMap._events[this._eventId];
if(ev.isMoving2 === undefined){
  ev.isMoving2 = ev.isMoving; ev.isMoving = function(){
    var move = this.isMoving2.call(this);
    this._directionFix = move;
    return move;
  };
} ev.moveStraight(p._direction);
p.moveStraight(p._direction);
ev._directionFix = true;
ev._direction = p._direction;
pushit3-opt.gif


Now that I think about it, if you don't want him to look where he moves, you could just keep using the event script you got and tick the box to turn direction fix on.

thay whole point in this case its that I want the direction do not be fix. But its got fix automaticly. Unfortunatly with my current computer, I cant take video.
Code:
var p = $gamePlayer;
$gameMap._events[this._eventId].moveStraight(p._direction);
p.moveStraight(p._direction);
Oh. I'm sorry. In that case, just take out the
$gameMap._events[this._eventId]._direction = p._direction;
Code:
var p = $gamePlayer;
$gameMap._events[this._eventId].moveStraight(p._direction);
p.moveStraight(p._direction);
Oh. I'm sorry. In that case, just take out the
$gameMap._events[this._eventId]._direction = p._direction;
that exactly my problem... its work with this line, but if I remove it, its stop working corectly, I dont understand.
when i remove this line, something wrong happen.
Exemple:
im at the left of a npc. The npc looking down. i push npc to the right. while moving the npc look right. Immediatly when finish pusinh he start looking back down agian... I dont want him to look down again... mave no sense.
javascript overrides specific data, but it doesn't care about other data - so the event will not change the set facing unless it is commanded to do so.
That is one reason why this is usually easier to do by eventing alone, the event commands automatically take care of connected info while the javascript requires specific changes for anything.

You need to give an additional command to change the direction after the move.
javascript overrides specific data, but it doesn't care about other data - so the event will not change the set facing unless it is commanded to do so.
That is one reason why this is usually easier to do by eventing alone, the event commands automatically take care of connected info while the javascript requires specific changes for anything.

You need to give an additional command to change the direction after the move.
You perfectly answered my question =:0) Thanks

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/push-a-box-using-javascript.75244/