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

How Would I "Shift" the values in an Array?

S

Scoptile

Guest
What I would like to do is move all the values in an array from a certain point over one. Like this:

array[1] = "value1"
array[2] = "value2"
array[3] = "value3"

becomes:

array[1] = "value1"
array[2] = ""
array[3] = "value2"
array[4] = "value3"

if you "shift" it at the second value
sorry if this is confusing.
 
P

Paolo Mazzon

Guest
I think what you're looking for is ds_list_insert which does exactly that. Lists are fairly straightforward, they're basically just arrays with some very useful functions built in. As a quick side note, it probably doesn't matter to what you're doing, but I'll mention it anyways, if you have a ton of things in the list, and then put something at the start of it, it will be fairly heavy since it then has to loop through the whole list and displace everything.
 
S

Scoptile

Guest
I think what you're looking for is ds_list_insert which does exactly that. Lists are fairly straightforward, they're basically just arrays with some very useful functions built in. As a quick side note, it probably doesn't matter to what you're doing, but I'll mention it anyways, if you have a ton of things in the list, and then put something at the start of it, it will be fairly heavy since it then has to loop through the whole list and displace everything.
im trying that out now. do you know of a way to overwrite an existing value in the ds list?
 
P

Paolo Mazzon

Guest
@Scoptile, yes, you can still use array notation (myarray[x]) with lists, just with lists make sure you put a | before the number. (So arr[4] would be arr[|4] if it was a list)
 

wamingo

Member
ds_list is all but certainly the right answer, but here's a way to shift forward and insert into a 1d array:

Code:
// forward shift from index 2
var shiftfrom = 2;
for (var i=array_length_1d(a); i>shiftfrom; i--){
  a[i]=a[i-1];
}
// insert
a[shiftfrom] = 'newdata';
 
Top