GMS 2 Can a Object Move to Different Room?

Discussion in 'Programming' started by RifleFire, Sep 11, 2019.

  1. RifleFire

    RifleFire Member

    Joined:
    Jun 22, 2016
    Posts:
    22
    Hi Everyone,
    I am using an array of rooms. I would like to have NPC objects move from room to room without the player object being in the same room as the npc. How can this be done? Thanks for any help you can give me...
    RF
     
  2. spoonsinbunnies

    spoonsinbunnies Member

    Joined:
    Aug 24, 2018
    Posts:
    109
    without using persistent rooms, which is a bad idea usually, the best way is to make a global variable that's a timer, then simply load the character based on a schedule, for example
    0-1000 in spot a in room a, 1000-1500 have the npc moving to room b and to the spot in room b, 1500-2500 standing in spot b in room b, 2500-3000 moving from b to a, the secret is when the character is in a different room they essentially don't exsist, you simply load them when they would be in the same room as the player, or based on the timer at a doorway when they would enter your room. By using different schedules you can have multiple wandering npcs all using the same clock variable. your just load people when you enter a room and have a check in step to load anyone that would enter the room.
     
  3. RifleFire

    RifleFire Member

    Joined:
    Jun 22, 2016
    Posts:
    22
    I am using persistent rooms. I wanted to have the npc's attack a base in room that doesn't have the player in it. Then when the attack happens, the player receives a msg that an attack is underway in say [1,4] and he/she should warp there to defend the base. Is this possible and if so, how??
    RF
     
  4. Fabseven

    Fabseven Member

    Joined:
    Oct 7, 2016
    Posts:
    350
    I think Persistent rooms are hard to use,
    but about your question i think it's possible.
    When the obj is set to persistent then when changing room the obj is still in the new room
    so given you have a random process to launch the attack just do it normally ?

    It's just a though but if you game is something like a city-defender and if you created a room for "building management" or "science management" or "something management" then you should NOT manage theses things in a different room but in the same room.
     
  5. RifleFire

    RifleFire Member

    Joined:
    Jun 22, 2016
    Posts:
    22
    The problem is that i do not want the current room enemy instances to teleport over to other rooms with the player object. The only way i want a enemy object in a new room is if the enemy object instances are MOVED thru code to a new room themselves independent of the player object. Non-Persistent makes them not teleport with the player object but it also doesn't allow them to exist in a new room without the player object??? I need the enemy objects to both move to and exist in a new room and also to not teleport with the player object when the player object moves to the new room. Hope that makes sense... Any way of doing this? Thanks for any help yall can give me...
    RF
     
  6. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    689
    You have to think more abstractly about how to represent what’s going on in the larger game world if it is segmented into rooms. There will be no code for any instances of enemies running if your current room is not the room with the enemies. Putting enemies into some room for later doesn’t have them attacking the base behind the scenes.

    You will need to have global variables, likely data structures or arrays, and a controlling object that is charge of simulating what is going on in each room. For example:

    objects:
    obj_enemy_controller
    obj_enemy (you may already have this - just whatever the enemy object is)

    global data structures:
    Code:
    global.enemy_data = ds_grid_create(2, X) //the 2 is for a grid 2 wide (0 column = room, 1 column= enemy state) X is for how many enemies you have (height of grid)
    global.base_health[0]=100 //this could be an array that stores the health of all bases. Fill up the array with your base numbers and starting health.
    The idea is, if you want enemy 3 in room 2, you can set the grid values like:
    Code:
    global.enemy_data[# 0, 3]= 2
    If you want that enemy to be attacking the base in that room, it would look like:
    Code:
    global.enemy_data[# 1, 3]= "attack" //or even better, use enums, buts that's another discussion
    Then perhaps there is an alarm in the obj_enemy_controller that cycles through all enemies once every two seconds, checking which rooms are under attack
    Code:
    for (var i=0;i<ds_grid_height(global.enemy_data);i++) {//check this syntax I usually use GM's auto insert code structure
    if global.enemy_data[#1,i] = "attack" {//checks whether this enemy is in an attack state
    var _base = global.enemy_data[# 0,i]//grab which room the enemy is in
    global.base_health[_base] -=10; //takes 10 health off the base in room _base
    }
    }
    You are getting into the powerful, yet daunting, aspect of game development - manipulating abstract data and features without any obvious visual response on screen. Make use of the debugger and/or make your own visual cues (such as that message you wanted to appear when a base is under attack) to work through and test your systems.

    Edit: Other thoughts:
    have the controller object spawn the correct number enemies when the player enters a room by iterating over the grid to find how many enemies are in this room.
    If a player kills an enemy, it has to be removed from the grid. This might be setting the enemy state to "dead" in the grid. Or perhaps you want to be able to add and remove enemies from the grid dynamically - which might need an extra column in the grid to keep track of the enemy id.
    If the player is in a room, perhaps you don't want the base to be damaged by the controller obj, but by the actual instances of the enemies.

    LOTS to think about and manage when you want things to be happening in other rooms.
     
    Last edited: Sep 12, 2019
    RifleFire and Catan like this.
  7. spoonsinbunnies

    spoonsinbunnies Member

    Joined:
    Aug 24, 2018
    Posts:
    109
    while Ive never used persistent rooms so in all honesty my process is still the only thing I can think to use that would work,
    when an enemy leaves the current room figure out where it would spawn in the new room, then calculate how much time it would take to get to what its attacking,
    then its just a matter of sending a message after an alarm that your base is under attack, so say it takes 100 frames from room enter to your base for the enemy, when the enemy leaves the room save its type and anything special about it (ie anything different from a new create state) into ds_lists in your character the actual enemy gets destroyed however because the timer exists you can use math so when you jump to the other level to spawn the enemy where it would be based on simply(how long since it left last level*speed torwards castle if its bigger than that that number goes to calculate how many times it would have attacked.) So the long and short is as far as I know theres no way to make an enemy teleport seperatly of other persistent objects, you essentially have to save anything special and use math to cheat it when new rooms are loaded.

    knowing the attack rate means you also know how much hp to drain when the player jumps to the other room.
     
  8. RifleFire

    RifleFire Member

    Joined:
    Jun 22, 2016
    Posts:
    22
    Thank You Relic for the wonderfully detailed info post and also thank you Spoonsinbunnies. God bless you both. Humm. Not sure but maybe a single huge room divided into sectors might be easier and have more visual confirmations. I will explore both possibilities. Thanks again to everyone for your comments.
    RF
     
    Last edited: Sep 13, 2019 at 4:44 AM

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