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

Variable not set - Object created within Object [SOLVED]

I am creating a shop. I want to draw an arrow object when an object for sale is clicked. At the moment, the shop object draws the objects from a shop inventory (ds_grid_get(myItems,x,y)) with:
(note that the local variables like itemLeftStart and horizInc are just set to numbers within the object. Also note that the (I) is in square brackets, but this doesn't let me post those for some reason).

for(i = 0; i < global.inventoryEndAt; ++i) {
global.itemNow (I) = instance_create_depth(itemLeftStart+i*horizInc,itemTopStart,objdepth,ds_grid_get(myItems, 3, i));
}

..which should store the item instance in the global variable itemNow. Note that the objects are drawn on the screen just fine.

Then in the Left Pressed, I use the following code for the idea, "If clicking on one of the created objects in the shop, create a new instance of an arrow":

for(i = 0; i < global.inventoryEndAt; ++i) {
with (instance_position(mouse_x,mouse_y,global.itemNow(I)))
{
global.selectButton=instance_create_depth(350+36*i,260,-200,obj_shopArrowDown);
}

}

And after all of that, when I click on an object, receive the error, "Variable obj_fish.i(100009, -2147483648) not set before reading...
obj_fish is the name of the object clicked in this case.

Help??
 
Last edited:

TheouAegis

Member
Use code blocks if you use i in arrays.

You need to declare i with the var command in your loops. You use i inside a with command, but i is only local to the instance calling this code, not the instances you created. You need to use var i so all instances will have access to i during the execution of this block of code.
 
Awesome, thank you!

I used:

for(i = 0; i < inventoryEndAt; ++i) {
var i;
with (instance_position(mouse_x,mouse_y,global.itemNow(I)))
{
global.selectButton=instance_create_depth(350+36*i,260,-200,obj_shopArrowDown);
}
}
 

TheouAegis

Member
Awesome, thank you!

I used:

for(i = 0; i < inventoryEndAt; ++i) {
var i;
with (instance_position(mouse_x,mouse_y,global.itemNow(I)))
{
global.selectButton=instance_create_depth(350+36*i,260,-200,obj_shopArrowDown);
}
}

Almost.

for(var i = 0; i < inventoryEndAt; ++i) {

Your code will run, but it's probably using the wrong value for i. Then again, if it does work just fine like that, then that's something new I learned about gamemaker. 10 years later.
 
Top