[SOLVED] Too many "with"s and "If"s. Need Help

E

Edeyz

Guest
Sorry I think this is about my third post this week (and hopefully last). Other users have been very helpful so far, but for some reason I keep getting stuck on this and cant think of anything myself. So i'm hoping to get post the entire thing here to get advice instead of just the problem part.

The code provided is a bit messy as it is a direct copy and not finished. The image is a screenshot of the current game. There may be unused variables and things that i have changed trying to get it to work.

Some information:

I'm new to Game maker (bargain on humble bundle), and have only done a little bit of C# before during my year 12 course in IT

I have a "city builder" like game

The game uses "rooms" (not game maker rooms)
- A "room" is made from however many tiles of the same "room" the player happens to place next to each over.
- To define a room I use roomID
- roomID as well as any other infomation such as the size of the room is held in a "parentRoom" (again NOT a parent object) the parentRoom is the "room tile" that was most recently placed, and is part of that room.
- for example if i have a tile:
X
this tile is considered the "parentRoom" and has the "roomID" "0" as it is the only room, the "roomSize" is "1"
if I place a second tile
XQ
Q is now the "parentRoom" and all infomation about the room will be read from there, "roomSize" is "2" and the "roomID" of EACH room is "0"

-I need to check every instance of the object that the room is comprised of on the creation of a new "room"
Because I want the player to be able to merge "rooms" together So if there are 2 non adjoining rooms with the roomID's "0" and "1" and I join them by placing a "room" tile between them the ID of ALL tiles from each room becomes "0" and the "roomSize" is the sum of all tiles of that ID
-This is a script that is run in the creation event.
- roomID starts at 0 and increase by 1 for each new "room"

THE ISSUE

Because I check "up" "left" "down" and "right" they will either be an ID or "nooone"
any of those can also have the same "roomID" as any other eg. up.roomID == left.roomID
due to the new "tile" been placed on an interior corner. So in order to avoid dubleing up when counting " "roomSize" I need to check i have not checked that ID before and that it is not "noone".
So for "up" i'm using
if (up != noone && (right != noone && uRoomID != rRoomID)) || (up != noone && right == noone)
which is quite long. (and longer still when I get to "left")
one way I was hoping to tackle this was to create a var for each and have it set to -1 and change if say 'up != noon" but this still sounds like too many if statements. and is almost doubled up with a similar (but working) script I use to update the sprites too (see image).

So If anyone can come up with a more efficient slice of code for this, or just a new system of doing this all together I would be very thankful.


NOTE: THE LAST BITS ARE NOT FINISHED PAST "UP" (see where it says "//only up work ATM") THIS IS BECAUSE THE CODE IS ALMOST THE SAME AS UP AND I AM LIKELY REMOVING IT ANYWAY



Code:
//Checks for/toggles ajecent instances to add to the room size
var up, left, down, right, imageWidth, temp, uRoomID, lRoomID, dRoomID, rRoomID, temp;
increeseToRoomCount = 0;
imageWidth = sprite_width;

temp = global.roomIDGenerator;

up         = instance_place(x, y - imageWidth, object_index);
left       = instance_place(x - imageWidth, y, object_index);
down       = instance_place(x, y + imageWidth, object_index);
right      = instance_place(x + imageWidth, y, object_index);

//Checks the adjecent rooms and defines roomID accordingly 
if up != noone
    {
    temp = up.roomID;
    uRoomID = up.roomID;
    show_debug_message("up: " + string(up.roomID));
    }
if left != noone
    {
    lRoomID = left.roomID;
    show_debug_message("left: " + string(left.roomID));
    if is_undefined(temp) || left.roomID < temp
        {
        temp = left.roomID;
        }
    }
if down != noone
    {
    dRoomID = down.roomID;
    show_debug_message("down: " + string(down.roomID));
    if is_undefined(temp) || down.roomID < temp
        {
        temp = down.roomID;
        }
    }
if right != noone
    {
    rRoomID = right.roomID;
    show_debug_message("right: " + string(right.roomID));
    if is_undefined(temp) || right.roomID < temp
        {
        temp = right.roomID;
        }
    else
        {
        with(object_index)
            {
            if roomID == rRoomID
               {
               roomID = temp;
               other.increeseToRoomCount ++;
               }
            }
        }
    }   
    
    
    
if (up != noone && (right != noone && uRoomID != rRoomID)) || (up != noone && right == noone)
    {
    with(object_index)
        {
        if roomID == uRoomID
           {
           roomID = temp;
           other.increeseToRoomCount ++;
           show_debug_message("up works");
           }
        }
    }
//only up works ATM
if left != noone && (right != noone && lRoomID != rRoomID) && (up != noone && lRoomID != uRoomID)
    {
    with(object_index)
        {
        if roomID == lRoomID
           {
           roomID = temp;
           other.increeseToRoomCount ++;
           }
        }
    }
//only up works ATM
if down != noone && (right != noone && dRoomID != rRoomID) && (up != noone && dRoomID != uRoomID) && (left != noone && dRoomID != lRoomID)
    {
    with(object_index)
        {
        if roomID == dRoomID
           {
           roomID = temp;
           other.increeseToRoomCount ++;
           }
        }
    }


//Sets the roomID of the instance calling the event
if temp != noone
   {
   roomID = temp;
   }
else
    {
    roomID = temp;
    global.roomIDGenerator ++;
    }
roomSize = increeseToRoomCount;
show_debug_message("roomSize: " + string(roomSize));
Sorry if i'm been a pest,
Thank you again
Edeyz
 

Attachments

NicoDT

Member
I'm not exactly sure I understand what you're trying to do, but this is what I'd do.

Code:
check =0
check += place_meeting(x, y - imageWidth, object_index)*1000 //checking up
check += place_meeting(x - imageWidth, y, object_index)*100 //checking left
check += place_meeting(x, y + imageWidth, object_index)*10 //check down
check += place_meeting(x, y + imageWidth, object_index)*1  //check right
The place meeting would give you a 1 or 0, then when multiplied by the number you'd get a different number depending on the collision
for example 1010 means that there is something up and down, 1 means there's something only to the right.

Then you can do a switch

Code:
switch(check) {
    case 0:   //no collision  
    case 1:   //coll right
    case 10 : //coll down
    case 100:  //coll left
    case 1000: // coll up
    case 11:  //call right and down
    case 101:  //...
    case 1001:
    case 110:
    case 1010:
    ...
}


Then to make it prettier you can have the code in each case in different scripts.
 

rIKmAN

Member
I'm not exactly sure I understand what you're trying to do, but this is what I'd do.

Code:
check =0
check += place_meeting(x, y - imageWidth, object_index)*1000 //checking up
check += place_meeting(x - imageWidth, y, object_index)*100 //checking left
check += place_meeting(x, y + imageWidth, object_index)*10 //check down
check += place_meeting(x, y + imageWidth, object_index)*1  //check right
The place meeting would give you a 1 or 0, then when multiplied by the number you'd get a different number depending on the collision
for example 1010 means that there is something up and down, 1 means there's something only to the right.

Then you can do a switch

Code:
switch(check) {
    case 0:   //no collision
    case 1:   //coll right
    case 10 : //coll down
    case 100:  //coll left
    case 1000: // coll up
    case 11:  //call right and down
    case 101:  //...
    case 1001:
    case 110:
    case 1010:
    ...
}
Then to make it prettier you can have the code in each case in different scripts.
Wouldn't getting the 1 or 0 result from the place_meeting and adding it ( check+= ) just increment 'check' by one?
So you'd get a value between 0 and 4 unless you made 'check' a string and added the result to the string?

edit: ignore me I just saw your multiplication - my bad, doh!
 
Top