Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///scr_teleport_to_loc(instance_to_teleport, tele_x, tele_y, collidable_index)
- //This function DOES NOT move the object provided in the arguments.
- //Instead, it does the math and returns a ds_list with the information determined in the function
- //You can use this information however you want
- //The ds_list contains the x and y coordinates of the collision at index 0 and 1 respectively,
- // and at index 2 it contains the instance ID of the object it collided with.
- //Be sure to save this information in a var when you call this scripted, and
- // MAKE SURE TO CALL DS_LIST_DESTROY() ON THE VARIABLE AFTER COMPLETION
- var tele_ob = argument0; //object to teleport
- var tele_x = argument1; //X coord to teleport to
- var tele_y = argument2; //Y coord to teleport to
- var collide_with = argument3; //object index of what collidables to check for
- //collide_with should be set to an unused parent object in the resource tree
- //set the Parent of all collidable objects to this parent
- //for instance, 'obj_collidable' could be an empty object never spawned in game, but used as a parent on all collidable objects
- var accuracy = max(1, floor(argument4)); //The pixel accuracy for the loop
- //setting this to 1 means 1 loop iteration (and 1 collision check) per pixel
- //this means a teleport of 350 pixel distance would loop and check 350 times
- //I suggest setting accuracy between 1 and 12. 1 is unoptimal and can potentially cause hiccups, depending on
- //the teleport distance. If the object is large, say 64x64, then accuracy of 10-15 would be totally fine.
- var dir = point_direction(tele_ob.x, tele_ob.y, tele_x, tele_y);
- var dist = point_distance(tele_ob.x, tele_ob.y, tele_x, tele_y);
- var iterate_x = lengthdir_x(min(accuracy, dist), dir);
- var iterate_y = lengthdir_y(min(accuracy, dist), dir);
- var dist_per_iteration = point_distance(0, 0, iterate_x, iterate_y);
- var cur_x = tele_ob.x;
- var cur_y = tele_ob.y;
- var collision_info = ds_list_create();
- ds_list_add(collision_info, cur_x);
- ds_list_add(collision_info, cur_y);
- ds_list_add(collision_info, noone);
- with(tele_ob) {
- while(dist > 0) {
- cur_x += iterate_x;
- cur_y += iterate_y;
- dist -= dist_per_iteration;
- var colliding = place_meeting(cur_x, cur_y, collide_with);
- if(colliding) {
- var colliding_instance = instance_place(cur_x, cur_y, collide_with);
- var rev_dir = dir + 180;
- while(place_meeting(cur_x, cur_y, collide_with)) {
- cur_x += lengthdir_x(1, rev_dir);
- cur_y += lengthdir_y(1, rev_dir);
- }
- collision_info[| 0] = cur_x;
- collision_info[| 1] = cur_y;
- collision_info[| 2] = colliding_instance;
- break;
- }
- }
- }
- if(collision_info[| 2] == noone) {
- collision_info[| 0] = cur_x;
- collision_info[| 1] = cur_y;
- }
- return collision_info;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement