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

Unable to find instance for object index -4

jenixelle

Member
I'm making a spawner of oCustomer and making the oCustomer go choose between a random number of seats and go the x postion of one of the instances of oSeats. When they go to oSeats I prevented them from going to the same seats again if they're filled. The problem is when the oCustomer goes to seat 0 that I get this error .

Unable to find instance for object index -4 at gml_Object_oCustomer_Step_0 (line 5) - move_towards_point ((instance_find(oSeat, seatNumber)).x, oSeat.y, 2);

GML:
//oCustomerSpawn
//Create
timer=room_speed*10
global.customer = 0;

randomise()
global.seatChoose = 0;

for ( i = 0; i < 6; i++)
{
  global.seatVacancy[i] = 0; //0-empty 1-full
  global.customerPosition[i] = 0;
}

//Step
if (timer <=1)
{
  if(global.customer<6)
  {
    instance_create_layer(0,530,"Instances_1", oCustomer);
    global.customer++;
  }
  timer = irandom_range(room_speed*10, room_speed*20);
}
timer--;
GML:
//oCustomer
//Create
randomize();
sprite_index= choose(sCustomer1, sCustomer2, sCustomer3, sCustomer4);

seatChoose1 = ds_list_create();
for (i  = 0; i < 6; i++)
{
  if (global.seatVacancy[i] = 0)
  ds_list_add(seatChoose1, i);
  else
  {
  deleteSeat = ds_list_find_index(seatChoose1, i);
  ds_list_delete(seatChoose1, deleteSeat);
  }
}

seatChoose = choose(seatChoose1);
seatNumber = seatChoose;

//Step
if (oCustomer.x != (instance_find(oSeat, seatNumber)).x){
  move_towards_point ((instance_find(oSeat, seatNumber)).x, oSeat.y, 2);
}
 

Slyddar

Member
seatChoose = choose(seatChoose1);
Choose picks a random value from the choices you pass it. seatChoose1 is the id of the ds_list, not the size of it. You need to use ds_list_size first, and then use irandom on the (size - 1), to get a random seat.
 

Nidoking

Member
More relevantly to your question, instance_find is returning noone, and you're not checking for that. You can't get variables from noone.
 
You also only need to call randomize at the start of the game. Calling it every time you need something random is like shuffling the deck before tossing each player a card.
 

rytan451

Member
You also only need to call randomize at the start of the game. Calling it every time you need something random is like shuffling the deck before tossing each player a card.
Worse, actually. GMS uses a linear congruential generator as its PRNG. randomize() sets the seed based on the clock, which achieves pseudorandom distribution if called once. But, if you call it multiple times, you start being able to predict the randomness. The flaws of using similar random seeds can be seen in Minecraft, where a recently discovered bug relating to the use of the linear congruential generator means that you can correlate clay spawning with diamond spawning around 80% of the time.
 
Top