Advertisement
Skizerzz

Gamemaker Studio 2 Object Teleport

Mar 12th, 2019
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///scr_teleport_to_loc(instance_to_teleport, tele_x, tele_y, collidable_index)
  2.  
  3. //This function DOES NOT move the object provided in the arguments.
  4. //Instead, it does the math and returns a ds_list with the information determined in the function
  5. //You can use this information however you want
  6. //The ds_list contains the x and y coordinates of the collision at index 0 and 1 respectively,
  7. // and at index 2 it contains the instance ID of the object it collided with.
  8. //Be sure to save this information in a var when you call this scripted, and
  9. // MAKE SURE TO CALL DS_LIST_DESTROY() ON THE VARIABLE AFTER COMPLETION
  10.  
  11. var tele_ob = argument0; //object to teleport
  12. var tele_x = argument1; //X coord to teleport to
  13. var tele_y = argument2; //Y coord to teleport to
  14. var collide_with = argument3; //object index of what collidables to check for
  15. //collide_with should be set to an unused parent object in the resource tree
  16. //set the Parent of all collidable objects to this parent
  17. //for instance, 'obj_collidable' could be an empty object never spawned in game, but used as a parent on all collidable objects
  18. var accuracy = max(1, floor(argument4)); //The pixel accuracy for the loop
  19. //setting this to 1 means 1 loop iteration (and 1 collision check) per pixel
  20. //this means a teleport of 350 pixel distance would loop and check 350 times
  21. //I suggest setting accuracy between 1 and 12. 1 is unoptimal and can potentially cause hiccups, depending on
  22. //the teleport distance.  If the object is large, say 64x64, then accuracy of 10-15 would be totally fine.
  23.  
  24. var dir = point_direction(tele_ob.x, tele_ob.y, tele_x, tele_y);
  25. var dist = point_distance(tele_ob.x, tele_ob.y, tele_x, tele_y);
  26. var iterate_x = lengthdir_x(min(accuracy, dist), dir);
  27. var iterate_y = lengthdir_y(min(accuracy, dist), dir);
  28. var dist_per_iteration = point_distance(0, 0, iterate_x, iterate_y);
  29. var cur_x = tele_ob.x;
  30. var cur_y = tele_ob.y;
  31.  
  32. var collision_info = ds_list_create();
  33. ds_list_add(collision_info, cur_x);
  34. ds_list_add(collision_info, cur_y);
  35. ds_list_add(collision_info, noone);
  36.  
  37. with(tele_ob) {
  38.     while(dist > 0) {
  39.         cur_x += iterate_x;
  40.         cur_y += iterate_y;
  41.         dist -= dist_per_iteration;
  42.         var colliding = place_meeting(cur_x, cur_y, collide_with);
  43.         if(colliding) {
  44.             var colliding_instance = instance_place(cur_x, cur_y, collide_with);
  45.             var rev_dir = dir + 180;
  46.             while(place_meeting(cur_x, cur_y, collide_with)) {
  47.                 cur_x += lengthdir_x(1, rev_dir);
  48.                 cur_y += lengthdir_y(1, rev_dir);
  49.             }
  50.             collision_info[| 0] = cur_x;
  51.             collision_info[| 1] = cur_y;
  52.             collision_info[| 2] = colliding_instance;
  53.             break;
  54.         }
  55.     }
  56. }
  57.  
  58. if(collision_info[| 2] == noone) {
  59.     collision_info[| 0] = cur_x;
  60.     collision_info[| 1] = cur_y;
  61. }
  62.  
  63. return collision_info;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement