• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Movement script too slow!

T

Toxicosis

Guest
I was trying to optimize, and it backfired.

My movement script required one too many calls to x++ and place_meeting, so I decided to minimize the number of place_meeting calls required. And things went south.

Here's my current movement script. I replaced the var by argument calls directly in order to minimize load, as the script is recursive and could require one too many vars, but it's still too slow in walking slopes up and down.

Code:
//This script will attempt to move every element with the least amount of checks possible.

//Check that many steps forward.
//If there's nothing, we're cool.

//If there are, check halfway across back. If the script returns positive, check halfway across forward.
//If negative, check halfway across back.
//Recursively call until we're checking to within 1 step. Then return the value.
//Theoretically at least, this should minimize the computing load.
//We could move up to 2048 pixels with a mere 11 calls to place_meeting.
//Now, the problem is the vertical movement also necessary.

//Arguments:
//Argument 0: Extent of movement.
//Argument 1: Horizontal direction (-1,0,1)
//Argument 2: Vertical direction (-1,0,1)

//var extent, vertical, horizontal, i, extent_step;
//extent = argument0;
//horizontal = argument1;
//vertical = argument2;
//extent_step = min(extent,sprite_width);

if place_meeting(x+argument1,y+argument2,obj_wallz) return false;
//This is the kill code. Once the character is like RIGHT UP TO A WALL, it returns false, indicating we've hit a wall.
if argument0 == 0 return true;
//If we've run out of steps, mission accomplished.
{
  if !place_meeting(x+argument1*argument0,y+argument2*argument0,obj_wallz)
  {
  //If there's nothing in the way, we're cool. Move the thing there.
  x=x+argument1*argument0;
  y=y+argument2*argument0;
  return true;
  }
  else
  {
  if argument0 == 1 return false; //If we tried to move a single step and it failed, then it failed.
  //If there was something in the way, halve the value and test again. If it finds room, call it again to check the rest of it.
  if scr_movement(argument0 div 2,argument1,argument2)
  scr_movement(argument0 - argument0 div 2,argument1,argument2); //Recursion lol
  else return false; //we've hit a wall.
  }
}
//NOTE: THIS SCRIPT WILL CAUSE BUGS IF THE PLAYER IS WIDER THAN THE STEP.
This is the script inside the player, handling movement and stuff.
Code:
if (player_right xor player_left)
{
var cling = place_meeting(x,y+1,obj_wallz); //Checks if the player's close to the ground.
var hopper = 16; //Maximum height the player can "hop".
var xinit= x;
var yinit= y;
if !scr_movement(step_move,player_dir,0) //If scr_movement returns false, a wall stopped the player.
  {
  scr_movement(hopper,0,-1); //Jump one hop.
  scr_movement(step_move-(x-xinit),player_dir,0); //Move the player the rest of the distance.
  if y != yinit //If the player is at a different height than they started, lower them as far as they will go, then set cling to false, as we've already moved down that much.
  {
  scr_movement(hopper,0,1);
  cling = false;
  }
  }
if cling
  if place_meeting (x,y+hopper,obj_wallz) //If there's a ground to return to, it returns to the ground.
  scr_movement(hopper,0,1);
}
I tested the slope with a diamond-shaped fixture. It did not go too well. Slowdowns for higher speeds (32, 100, 320) knock me under 60 at times. Not cool. Any advice?
 
Switching from instance based collisions to array/grid based collisions would speed things up dramatically. Unfortunately, adding slopes to an array/grid based collision system is a lot more work.
 
Top