• 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!

GML Inserting / Removing instances in an array

Hello! I'm programming a small physics based fishing game, but have run into a challenge where a functionality I'd like to include eclipses my knowledge of GML and arrays / lists. For clarification, what I would like to simulate is the act of reeling in a fishing line by creating (and destroying) instances of the object I have called obj_String. I am at the stage in my project where everything else works fine without major errors, but I do not know the best way of integrating the create/destroy functionality with the current setup. As a disclaimer, the majority of this code was purchased from the marketplace, as I am a beginner programmer, and I have made small changes to suit the direction of this project. All comments in the code are my own.

There is a script I'm running that uses a For loop to create instances of my string object until a limit as been reached. Right now, just 5 instances of obj_String (which are only about 4 pixels long each) are created by entering 5 into the _segments variable. Please assume that the below code works as intended, and creates 5 instances, with other scripts dealing with the creation of physics joints for the edge cases (i.e where the string joins the fishing rod, and where it joins the hook).

In the following code, I am telling the game to create 5 small string segments attached with 2 physics joints each. I've edited the code to include just enough information to give a sense of how the array is coded. It runs in the create event of my game_room.

Code:
for (i=0; i<_segments; i++) {
    //the index will be at 0 the first time around as it was initialized at -1 previously. 
  rp_segment[rp_index, i] = instance_create(_x+lengthdir_x(_dis, _dir), _y+lengthdir_y(_dis, _dir), obj_String);
  with (rp_segment[@ rp_index, i]) {
    rp_number = i;
    rp_id = other.rp_index;
    if (i != 0) {
      _inst1 = other.rp_segment[@ other.rp_index, i-1];
      _inst2 = other.rp_segment[@ other.rp_index, i];
      rp_joint[0] = physics_joint_revolute_create(_inst1, _inst2, _a_x, _a_y, 0, 0, 0, 0, 0, 0, 0);
      rp_joint[1] = physics_joint_distance_create(_inst1, _inst2, _inst1.x, _inst1.y, _inst2.x, _inst2.y, 0);
    }
  }
}
I would like to create a script that will run in a Step event called increase_length() that inserts an additional instance of obj_String at the start of this list (at position zero) bumping all other pieces down by the appropriate distances (i.e height, direction etc.). This is where I have run into a dead end. Is there a built-in function that lets me insert new information into the rp_segment array? The only lead I could find was the ds_list_insert command, but nothing I did with this command gave me what I wanted. Any guidance would be appreciated.
 
H

Homunculus

Guest
You should probably be using a ds_list instead of an array if you want to dynamically expand, shrink or insert elements. With an array, you’ll have to move every item yourself to achieve this behviour
 

samspade

Member
Short answer no, not for arrays. But insertion is pretty easy to code. You can get a bunch of array functions here, or you can go to the git repository and just copy the insert function.
 
Thanks for the feedback. I'll try adding instances to the end as suggested, but I'm actually not sure about the best way of doing that.

For the loop in the OP, I've used an instance_create with a bunch of local variables that, if I understand correctly, are being deleted after the create event finishes (note, the variables with an underscore are all local variables, the others are not). I'm not sure how to reference these variables again, to create a new instance at the appropriate x and y coordinates of the last segment. In other words how could I reference the x and y coordinates (plus any other data) of the last segment (whose rp_number variable will be equal to the _segments - 1)?

Wouldn't this mean I'd need to keep the variables that I discarded through the var declaration so that the step event code can use them? Forgive me if I'm missing something obvious.
 
Top