1. Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice
  2. NOTICE: We will be applying a Xenforo update on Tuesday 25th of February. This means that from approximately 10:00 to 14:00 BST the forums will be offline (or possibly longer). Sorry for the inconvenience! Official Announcement here.

Deleting tasks from the list and replacing them with an inferior task

Discussion in 'Programming' started by 9lov, Jan 25, 2020.

Tags:
  1. 9lov

    9lov Member

    Joined:
    Jan 25, 2020
    Posts:
    4
    Hi everyone, I am recently studying GMS 2, and ran into some problems. I apologize in advance for my poor English.
    I'm making a "game" where the NEW button creates a task and clicking on the task deletes it.
    Tasks are added below each other. When creating a new task, the coordinates of the previous one are taken into account. Here is an example of adding and removing tasks
    There are 2 messages that show the status of the list: the first shows the number of items in the list, and the second shows the content: instance id of each task:

    Here I add tasks to the list, and then delete them in the order of the reverse addition
    [​IMG]

    If I delete a task from the middle of the list, then I want all the lower tasks to go higher, replacing the place of the deleted task.

    When you delete a task from the middle, all the lower tasks disappear, and they are not deleted. Debug messages show this. As I understand it, instances disappear from the room, but instances id remains in the list:
    [​IMG]
    After this deletion (from the middle), an error occurs when creating a new task.

    This is how the task is created: when you click the "NEW"button, a new instance of the task is created, and its ID is placed in the "global.TaskList"
    Object of the "NEW" button":
    The Event Left Pressed:
    Code:
    /// @description Adding a new task under the previous one
    
    var lastPositionY;
    if !ds_list_empty(global.TaskList)
        lastPositionY = ds_list_find_value(global.TaskList, ds_list_size(global.TaskList)-1).y;
    else
        lastPositionY = 10;
    
    ds_list_add(global.Ta
    lastPositionY - the variable checks whether the added task is the first, if so, the y coordinate of the new task is fixed, otherwise the y coordinate of the last task is placed in the variable to create a new one under the previous one.

    Deleting a task:
    To delete a task, click on it. This will launch code that finds all the tasks below to raise them. After that, the task is deleted, first from the list, and then from the room:

    Obj_task object:
    Step Event:
    Code:
    taskCurrentPos = ds_list_find_index(global.TaskList, id);
    taskCurrentPos is a variable that stores the position of a list item. It is necessary to determine which tasks are located lower in the list, which would only move up

    The Event Left Pressed:
    Code:
    with(obj_task) //For all instances...
    {
        //If the current instance has a position in the list lower than the task that was clicked
        if taskCurrentPos > other.taskCurrentPos
        y -= 60; //Raise this instance by 60 pixels (this is how much the deleted task took)
    }
    //Delete a Task from the list and then from the room
    ds_list_delete(global.TaskList, taskCurrentPos);
    instance_destroy();
    Please, help me!
    Link to the project: https :/ /drive.google.com/open?id= 1J1OYv8SeNDLQ1alVJDxyawjNUuR4dzR-
     
  2. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    768
    The link doesn't work - I don't have any advice from what you posted so far. I thought it might be regarding "taskCurrentPos" not being updated for each instance but you're updating it every step for each instance.
     
  3. 9lov

    9lov Member

    Joined:
    Jan 25, 2020
    Posts:
    4
    I added show_message( string(taskCurrentPos)) to the very beginning of Left Pressed.
    The idea is that now, before deleting a task, a message will be displayed with the task's position in the list. But here's what was really happening:
    [​IMG]

    This means that the Left Pressed event is applied not only to the pressed task, but also to all the tasks below it. The cycle does not apply to the top tasks.
    I will reflect.
     
  4. 9lov

    9lov Member

    Joined:
    Jan 25, 2020
    Posts:
    4
    It turns out that if several objects are at the same depth, then clicking one at a time will trigger all objects. When I click the mouse on the object, the first other up, and then delete, but this time 2 challenges on one level, and delete triggers on both tasks, because of this, the next(third) task comes up and also removed, pushing for the other tasks. I will try to change the depth of each new instance
    update----------------------------------------
    OK I realized that depth doesn't affect it. Why does clicking on 1 object trigger even if it is blocked by another object? How do I make clicking on an object apply only to the top one? Adjusting the depth doesn't help. If there are 2 objects in one position, and their depth is different, then clicking on 1 object will work on the other?
    ---------Solved------------------------------

    I made sure that the task was destroyed first, and only then the other tasks were raised. Before deleting, I passed the position of the deleted task to the controller, and at each step of the controller, the variable is checked for storing the position. It turns out that after deleting a task, the controller notices the value in its variable, and shifts all tasks with higher position values up. Everything works as it should.
    Only I do not know how to make it so that clicking on an object, this click does not reach other objects behind the top one.
     
    Last edited: Jan 25, 2020
  5. Nidoking

    Nidoking Member

    Joined:
    Nov 20, 2019
    Posts:
    119
    I ran into this issue with a similar type of task - when you move instances during the click event and put a new instance where the one you clicked on was, it applies the click to the new instance as well. What I did was put a ClickProcessed variable in a persistent instance that I use instead of global variables. In that instance, the Begin Step event includes
    Code:
    ClickProcessed = false;
    That resets the variable each step. Now, in the Click event for each instance, the first thing I do is
    Code:
    if (Controller.ClickProcessed) exit;
    Controller.ClickProcessed = true;
    So only one instance will process the click. Any future instances that receive a click during the same step will ignore it.
     
    9lov likes this.
  6. 9lov

    9lov Member

    Joined:
    Jan 25, 2020
    Posts:
    4
    Thanks
     

Share This Page