Windows [Gm2] How to use this jumbled code to my advantage, sandbox world with creating new blocks

A

aristhemage

Guest
Hi all, with the help of another I have done some world generation for a sandbox game, here is how it generates.
GML:
//World array
global.world = [];
global.worldArrayLength = 0;
//First two layers
for (var i = 0; i < 80; i++;){
    global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 768, "Blocks", block_grass);
    global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 832, "Blocks", block_dirt);
}
//Ignore this
global.xmem = x
global.ymem = y
//Block name
global.block = "";

//Trees
var wait = 0;
var tree = 0;
for (var i = 0; i < 80; i++)
{
    if (--wait <= 0)
    {
        var chance = random(100);
        var tree = irandom_range(0,1)
        if (chance < 10) // ~10% chance
        {
            if tree = 0{
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 704, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 640, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 576, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 512, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 64, 512, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 64, 512, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 128, 448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 64, 448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64,      448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 64, 448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 128, 448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 64, 384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 , 384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 64, 384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 , 320, "Blocks", block_leaves);
            }
            if tree = 1{
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 704, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 640, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 576, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 512, "Blocks", block_log); 
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 448, "Blocks", block_log); 
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 64, 448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 64, 448, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 128, 384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 64, 384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64, 384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 64,384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 128,384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 + 64, 320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 , 320,"Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 - 64, 320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(i * 64 , 256, "Blocks", block_leaves);
}
            wait = irandom_range(3, 6);
        }
    }
}



//"Cave" System
for (var i = 0; i < 80; i++;){
    for (var b = 0; b < 30; b++;){
        if b < 10{
global.world[global.worldArrayLength++] = instance_create_layer(i * 64, b*64+896, "Blocks", block_stone);
        }else
        {
    var number = irandom_range(0,10);
if number > 6
{
    global.world[global.worldArrayLength++] = instance_create_layer(i * 64, b*64+896, "Blocks", block_stone);
}else
{
  
}
        }
      
}
}
I have an object, sapling, that I want a tree to grow, but this does not work
GML:
global.world[global.worldArrayLength++] = instance_create_layer(x, y, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(x, y-64, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(x, y-128, "Blocks", block_log);
            global.world[global.worldArrayLength++] = instance_create_layer(x, y-192, "Blocks", block_log); 
            global.world[global.worldArrayLength++] = instance_create_layer(x, y-256, "Blocks", block_log); 
            global.world[global.worldArrayLength++] = instance_create_layer(x - 64, y-256, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x + 64, y-256, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x - 128, y-320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x - 64, y-320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x, y-320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x + 64,y-320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x + 128,y-320, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x + 64, y-384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x , y-384,"Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x - 64, y-384, "Blocks", block_leaves);
            global.world[global.worldArrayLength++] = instance_create_layer(x , y-448, "Blocks", block_leaves);
This creates a visual tree, but crashes the game the second I click (right or left) anywhere, my belief is that it pushes every block a few blocks and gets jumbled on what's what.

If it helps here is the left and right click of things
GML:
if (mouse_check_button_released(mb_left)) {

    var mouseClickX = floor(mouse_x / blockSize) * blockSize;
    var mouseClickY = floor(mouse_y / blockSize) * blockSize;

    var blockClicked = undefined;
    var blockClickedIndex = undefined;
    for (var i = 0; i < array_length_1d(global.world); i++) {

        if (global.world[i].x == mouseClickX && global.world[i].y == mouseClickY) {
            blockClicked = global.world[i];
            blockClickedIndex = i;
        }
    }

    if (blockClicked != undefined) { // if there was a block on the position you clicked
        var isInRange = point_distance(obj_player.x, obj_player.y, blockClicked.x, blockClicked.y) <= playerAttackRange;
        if (isInRange) {blockClicked.hp -= playerAttackDamage;}
        if (blockClicked.hp <= 0) {
            instance_destroy(blockClicked); // to remove the actual object from the game
            global.world = remove_from_array(global.world, [blockClickedIndex]); // to remove the object from the 'global.world' array

    }
    }
}

if (mouse_check_button_released(mb_right)) {
    var mouseClickX = floor(mouse_x / blockSize) * blockSize;
    var mouseClickY = floor(mouse_y / blockSize) * blockSize;
var blockClicked = undefined
    for (var i = 0; i < array_length_1d(global.world); i++) {

        if (global.world[i].x == mouseClickX && global.world[i].y == mouseClickY) {
            blockClicked = global.world[i];
            blockClickedIndex = i;
        }
    }
if (global.inventory[global.slot * 4] < 20 && global.inventory[global.slot * 4 + 1] > 0) {
      
    if blockClicked == undefined{
        var blockCreated = instance_create_layer(mouseClickX, mouseClickY, "Blocks",global.inventory[global.slot * 4 + 3]);
        global.world[array_length_1d(global.world)] = blockCreated;
        global.inventory[global.slot*4+1] -= 1;
    }
      
}

    }
This is the error:

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_player:
Unable to find any instance for object index '101664' name '<undefined>'
at gml_Object_obj_player_Step_0 (line 54) - if (global.world.x == mouseClickX && global.world.y == mouseClickY) {
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_player_Step_0 (line 54)


If you could help me that would be great, I have tried asking my partner, but hasn't responded to anything for a month and a half now, so I assume connections were cut.

Anyways, I'll be here to answer questions, I am not the brightest of programmers, 70% of this code is his.
 

Nidoking

Member
It says you've got global.world.x and global.world.y without subscripts on line 54 of the step event.

Oh, wait, that's just BBcode eating the subscripts. It;s probably checking for an instance that you destroyed but left the index in the array.
 
A

aristhemage

Guest
It says you've got global.world.x and global.world.y without subscripts on line 54 of the step event.

Oh, wait, that's just BBcode eating the subscripts. It;s probably checking for an instance that you destroyed but left the index in the array.
Do you know how to fix this? I also have this as a custom script
GML:
var resultingArray = [];
var resultingArrayIndex = 0;

var originalArray = argument0;
var indicesArray = argument1;

var originalArrayLength = array_length_1d(originalArray);
var indicesArrayLength = array_length_1d(indicesArray);

for (var i = 0; i < originalArrayLength; i++) {
    var toRemove = false;

    for (var j = 0; j < indicesArrayLength; j++) {
        if (i == indicesArray[j])
            toRemove = true;
    }

    if (!toRemove)
        resultingArray[resultingArrayIndex++] = originalArray[i];
}

return resultingArray;
the script is called remove_from_array
 

Nidoking

Member
Seems like you might want to try using the debugger there and inspect the contents of the array. I have no idea what might be going on without a wider perspective.
 
Top