GMS 2.3+ Make object orbit player

tiberionx

Member
Ok so i didnt want to necro an old thread

so this is what i derived from that thread, instead of placing the code in the player events i wanted it to be in the orbiting object

GML:
orbit_dir+=3;

if (orbit_dir>359) { orbit_dir=0; }

orbit_x= o_ship.x+(lengthdir_x(64, orbit_dir));
orbit_y= o_ship.y+(lengthdir_y(64, orbit_dir));

x = orbit_x;
y = orbit_y;
now I wanted to place like 2 or 3 of these objects but they all start at the same place in the orbit

in the old Thread Erik had this code


Code:
// Value that store how many shields to use.
// Three for example. You can change this value to as many shields
// as you want or need. ;)
shield_num=3;
// Setup orbit_dir variable for future
orbit_dir=0;

// For loop to create each shield
for(num_s=0;num_s<shield_num;num_s++){
     with(instance_create(x,y,obj_orbit)){
           //Setting a value that will be used to alter the
           //degrees of rotation in relation to the other shields.
           rot_mod=((360 div other.shield_num)*other.num_s);
           // setting mother id
           mother=other.id;
      }
}
but i couldnt derive it to my code , like rot_mod and instance_create seems to be a variable i dunno where to place

appreciate the help :)
 

Nidoking

Member
instance_create is deprecated and has been for a long time. You just need to use one of the new functions that create instances. rot_mod appears to be an angular offset. You'd add it to the orbit_dir somewhere along the way.
 

sylvain_l

Member
but i couldnt derive it to my code , like rot_mod and instance_create seems to be a variable i dunno where to place
first are you using gms1.4? because instance_create is an old function for 1.4; for gms2 you'll use instance_create_layer or instance_create_depth instead.

and rot_mod is used to evently distridute the "shields" (the orbiting objects: obj_orbit) around the ship

as I see it you should place a similar code in the create event of your o_ship (the ship is then responsible to spawn the shields around him with that snippets of code) and you use the value of rot_mod to initialize the value of orbit_dir
 

FrostyCat

Member
You need to learn the difference between objects and instances (Finding the right instance > Subsidiary instances). If there will be multiple instances of o_ship, the object ID isn't enough to find you the correct instance anymore, you need the exact instance ID.

Player Create:
GML:
var n_shields = 3;
var radius = 64;

for(var i = 0; i < n_shields; ++i) {
     with (instance_create_layer(x, y, layer, obj_orbit)) {
           orbit_dir = 360*i/n_shields;
           orbit_rad = radius;
           mother = other.id;
      }
}
Orbit Step:
GML:
orbit_dir += 3;
if (orbit_dir > 359) {
    orbit_dir -= 360;
}

x = mother.x+lengthdir_x(orbit_rad, orbit_dir);
y = mother.y+lengthdir_y(orbit_rad, orbit_dir);
 

tiberionx

Member
hey man ! thanks for the help , and yeah i thought it wasnt important to mention as there will always be only one instance of o_ship at any given time :)
 

tiberionx

Member
You need to learn the difference between objects and instances (Finding the right instance > Subsidiary instances). If there will be multiple instances of o_ship, the object ID isn't enough to find you the correct instance anymore, you need the exact instance ID.

Player Create:
GML:
var n_shields = 3;
var radius = 64;

for(var i = 0; i < n_shields; ++i) {
     with (instance_create_layer(x, y, layer, obj_orbit)) {
           orbit_dir = 360*i/n_shields;
           orbit_rad = radius;
           mother = other.id;
      }
}
Hey man Thanks again! :D

How come it doesnt work when i change these to normal variables ?

I was trying to spawn them by picking up a power up but im getting an error that the variables are not set

sorry for the noob questions .... just started out
 

FrostyCat

Member
Learn the rules for variable scoping inside a with statement. And given how you've completely dismissed the local scope as "abnormal", I also suggest that you learn what each of the three variable scopes are for:
If you're starting out, learn the basic rules of the language and use that skill to interpret everything you see in reference material. Don't just copy "how to make X" tutorials.
 

GMWolf

aka fel666
hey man ! thanks for the help , and yeah i thought it wasnt important to mention as there will always be only one instance of o_ship at any given time :)
I believe it is still good practice not to reference variables through objects but use instances at all times.
In this case, using mother rather than o_ship allows you to reuse this shield object for other ships (like enemy ships etc) easily by simply creating it from any instance you want to apply it to.

It is very common to see projects with many duplicate objects (like o_shield_player, o_shield_enemy, o_shield_boss etc) because something as simple as an object changes, esper with newcomers to GML.
It's good practice to try to keep it as a single object with different properties/variables (when it makes sense... Knowing what makes sense comes with experience).
This stops you from needing to repeat code and makes editing these objects easier as you do not need to replicate the change in many different objects.
 
Top