Creating a choice of objects at the position of another object (using "choose" correctly) [SOLVED]

Thanks to previous help I have a grid of 24 circles each with a num variable of 1-24. I want to create various patterns using objects in these circles, which I can do with a single object:
GML:
with obj_circle{//Square
    if num==1 or num==2 or num==3 or num==4 or
    num==5 or num==6 or num==7 or num==13 or
    num==12 or num==18 or num==19 or num==20 or
    num==21 or num==22 or num==23 or num==24 {
        instance_create_layer(x+6,y+6,"text",obj_pig_head)}
But if I change the last line to create one of two objects using choose instance_create_layer(x+6,y+6,"text",choose(obj_pig_head,obj_bomb))}
the positions of the objects get messed up. Obviously I'm using the wrong function or using choose incorrectly. If anyone can point out the flaw(s) in my code I'd really appreciate it.

Additionally, is there a way to select a range of numbers using : (for example, num 1 to 7) or would I have to use an array or list?[/ICODE]
 
Last edited:

trip

Member
I thing you use choose correctly.
Maybe sprite assigned with object have different x, y offset. Check this in sprite editor.

GML:
if num< 8 or num = 12 or num = 13 or num > 17{
}
this is one way but not good.
Can you use bitwise for store data?
bitwise is much bette for you in IF function but maybe terrible for saving data.
Example for you
GML:
#macro CreatePigHeadBitMask 16652415 // the dec number =  bitwise 111111100001100001111111
if num & CreatePigHeadBitMask {
// if any bit in num is 1 and same bit in mask is 1 then If is true
    instance_create_layer(x+6,y+6,"text",obj_pig_head)
}
 
Last edited:
Regarding your huge if statement, there are a few ways to do this better. The bitmask solution could work, as could a switch, which would be a little more readable (IMO) at the expense of performance:
GML:
var vchange,vobj;

with obj_circle{
    vchange = false;
    switch num{
        case 0: vchange = true; break;
        case 1: vchange = true; break;
        case 2: vchange = true; break;
        //and so on
        }
    if vchange{
        vobj = choose(obj_pig_head,obj_bomb);
        instance_create_layer(x+6,y+6,"text",vobj);
        }
    }
But probably the best thing, in the long run, would be creating a ds_grid or a 2-d array, populating that with the objects you want, then pulling what object to create from the structure. This would make it easy to save the pattern, or create a simple editor to make your grid (levels? puzzles?) in a visual interface, then load those instead of manually defining them in code.

GML:
var vx,vy;

global.patternGrid = ds_grid_create(6,4);//I'm assuming 6x4 since you said it's a grid with 24 positions
//clear pattern grid to obj_circle
ds_grid_clear(global.patternGrid,obj_circle);

global.patternGrid[# 0,0] = choose(obj_pig_head,obj_bomb);
global.patternGrid[# 1,0] = choose(obj_ping_head,obj_bomb);
//and so on

//now instead of making circles and replacing some of them with pigs, we'll just
//make either circles, bombs, or pigs from the start.

var vobj,vinst,dx,dy;
for (vy = 0; vy < 4; vy++){
    for (vx = 0; vx < 6; vx++){
        vobj = global.patternGrid[# vx,vy];
        dx = (vx * 36) + 150;//adjust for the size of your objects and the upper left corner of the overall grid
        dy = (vy * 36) + 150;
        vinst = instance_create_layer(dx,dy,"text",vobj);
        vinst.gridX = vx;
        vinst.gridY = vy;
        }
    }
Using dx and dy instead of calculating the x/y values in instance_create_layer makes it easy to use the debugger to figure out exactly where your instances are being placed, since you can check the actual value the game is using to position your instances (dx and dy) instead of guessing. Information like that is useful when you need to figure out why some instances aren't being placed at the correct spot.
 
Last edited:
Top