Push a box using javascript.

Status
Not open for further replies.

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,959
First Language
English
Primarily Uses
RMMV
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.
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
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);
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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...
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
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);


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.
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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);


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.
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
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;
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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.
 

Trihan

Speedy Scripter
Veteran
Joined
Apr 12, 2012
Messages
2,604
Reaction score
1,959
First Language
English
Primarily Uses
RMMV
Can you show a screenshot of what you mean, XGuarden?
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
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.


One thing this event script is missing is _directionFix "off". That might need a SetMovementRoute wait 30 frames and switch direction fix off again.
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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.
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
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;


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.
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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;


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.
 

mogwai

1984
Veteran
Joined
Jun 10, 2014
Messages
875
Reaction score
591
First Language
English
Primarily Uses
RMMV
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;
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,365
Reaction score
7,675
First Language
German
Primarily Uses
RMMV
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.
 

XGuarden

Veteran
Veteran
Joined
May 10, 2016
Messages
446
Reaction score
17
First Language
French
Primarily Uses
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
 

Archeia

Level 99 Demi-fiend
Developer
Joined
Mar 1, 2012
Messages
15,141
Reaction score
15,473
First Language
Filipino
Primarily Uses
RMMZ

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.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,050
Members
137,571
Latest member
grr
Top