GM:S 1.4 Making Groups of Objects Recognize Each Other & React To Changes

Discussion in 'Programming' started by Chris Smith, May 10, 2017.

Tags:
  1. Chris Smith

    Chris Smith Member

    Joined:
    Jul 18, 2016
    Posts:
    86
    What I am trying to do is use groups of the same object, for clarity I call the object obj_alphablock, to create a similar effect to what you see in newer mario sidescrollers, where you walk through a part of the wall and the wall goes transparent.

    Essentially I want a collision between the player and any single block in a group, to make the entire group's image_alpha drop, and then when the player is no longer touching any of the blocks, their alpha goes back to 1.

    I have it kind of working. But I have no idea what is causing it's strange behavior. If and entire column is touched by the player, their alpha wont' return to 1, and other similar behaviors.

    How would you do this?

    I've included a video showing what is happening. And my code is below.


    Code:
    if touched = true image_alpha = 0.2
    if touched = false image_alpha = 1
    
    instright = instance_place(x+1,y,obj_alphablock) //check to the right/left/top/bottom
    instleft = instance_place(x-1,y,obj_alphablock)  //to see if there is another alphablock int he group
    instdown = instance_place(x,y+1,obj_alphablock)
    instup = instance_place(x,y-1,obj_alphablock)
    
    
    if place_meeting(x,y,obj_player)  //if the player is behind the block, lower the alpha
        {
        touched = true
        }
    else if !place_meeting(x,y,obj_player) //if the player is not behind the block, check to see if he is behind another block in the group
        {
        touched = false //if player is not touching, set alpha to 1
      
        if instright != noone //if there is a block to the right
            {
            if instright.touched = true //and if it's 'touched' variable is true, make mine true too
                {
                touched = true
                }
                else touched = false
            }
        if instleft != noone
            {
            if instleft.touched = true
                {
                touched = true
                }
                else touched = false 
            }
        if instdown != noone
            {
            if instdown.touched = true
                {
                touched = true
                }
                else touched = false
            }
        if instup != noone
            {
            if instup.touched = true
                {
                touched = true
                }
                else touched = false
            }
        }
     
  2. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    When the player touch a block it set the touched=true.
    If the player set the full y row or when the run order is "wrong" you will get a locked situation.
    When the player is gone. You check if surrounding blocks is set to touched=true. This is your problem.
    The bottom block check if the above block is touched=true. The above block check if the bottom block is touched=true. So both block will not turn touched=false.
    It works as long the run order is right. If a block with touched=false is processed before a near block with touched=true.
     
  3. Joshua Allen

    Joshua Allen Member

    Joined:
    Jun 20, 2016
    Posts:
    46
    What you're asking is actually a networking problem, it's very similar to a project I had to do back when I was in school.
    I recommend that you change how the blocks work because to get this to work nicely the way it is now will take a lot work.

    What I would do is create a controller object for each group.
    When the player hits a block, that block tells the controller object that it was hit.
    Each block looks to the controller object to see if it should be transparent out or not.

    If you want I can write some code for you but doing it that way will be much easier.
     
  4. Chris Smith

    Chris Smith Member

    Joined:
    Jul 18, 2016
    Posts:
    86
    That... that's really smart. The controller can easily control more than one group as well, using creation code.
    I'm going to try this tomorrow, I'll come back if I need help.

    Thank you!
     
  5. Chris Smith

    Chris Smith Member

    Joined:
    Jul 18, 2016
    Posts:
    86
    I made it work.
    For anybody in the future wondering what I needed to do. The code is below and is commented fairly well. If you have any questions about it, just ask.

    obj_alphablock_controller CREATE
    Code:
    cluster1inv = false //if true, the corresponding cluster will change alpha due to code in the block's step event
    cluster2inv = false
    cluster3inv = false
    meeting = noone //initialize this variable with noone to prevent crashes
    obj_alphablock CREATE
    Code:
    //if the controller doesnt exist, create one
    if !instance_exists(obj_alphablock_controller) instance_create(x,y,obj_alphablock_controller)
    cluster = 1 //default cluster is 1, change to 2 or 3 in creation code
    alph = 0.4 //alpha to change to
    

    Code:
    //if the player is touching a block, set that block's id to the meeting variable
    if place_meeting(x,y,obj_player) obj_alphablock_controller.meeting = id
    
    if cluster = 1  //control a cluster of blocks, change this variable in creation code
        {
        if obj_alphablock_controller.meeting = id //if the last block touched is this block
            {                                     //and the player is no longer touching it, set the cluster1inv variable to false
            if !place_meeting(x,y,obj_player) obj_alphablock_controller.cluster1inv = false
            }
        if place_meeting(x,y,obj_player) obj_alphablock_controller.cluster1inv = true //allow alpha change
        if obj_alphablock_controller.cluster1inv = true image_alpha = alph //change the alpha
        if obj_alphablock_controller.cluster1inv = false image_alpha = 1 //when no longer touching the cluster, change the alpha back to 1
        }
      
    //I included 2 more clusters because I can imagine having up to 3 in one room, but don't think I'd have more than that. 
    if cluster = 2 //do same thing for these clusters
        {
        if obj_alphablock_controller.meeting = id
            {
            if !place_meeting(x,y,obj_player) obj_alphablock_controller.cluster2inv = false
            }
        if place_meeting(x,y,obj_player) obj_alphablock_controller.cluster2inv = true
        if obj_alphablock_controller.cluster2inv = true image_alpha = alph
        if obj_alphablock_controller.cluster2inv = false image_alpha = 1
        }
      
    if cluster = 3
        {
        if obj_alphablock_controller.meeting = id
            {
            if !place_meeting(x,y,obj_player) obj_alphablock_controller.cluster3inv = false
            }
        if place_meeting(x,y,obj_player) obj_alphablock_controller.cluster3inv = true
        if obj_alphablock_controller.cluster3inv = true image_alpha = alph
        if obj_alphablock_controller.cluster3inv = false image_alpha = 1
        }
      
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice