Endless Loop through script-execute in with

Amaror

Member
I have the following situation: I want a button that adds selects one object from a predetermined set and adds it to a list. If all objects already have been added to a list, it generates a new UI-object, which displays all the objects. If the player selects one of the objects from that display, then it gets removed from whatever list it is in and added to the list dependend on the originally clicked button.

To make this happen, when the button is pressed and no objects are free, another ui-object gets created which also creates a button for each of the objects. Each of those buttons also gets the object_index of the original button. When the new button gets pressed it uses with on the original button, adds it's selected object id to the variables of the original button and then tells it to run it's script again.
But with the "script_execute" within the with bracket it somehow creates an endless loop where it just keeps running that script. Anyone know what I can do about that?

Original Button commit - script:
GML:
function commit_add_agent_general() {
    if(subjectId == noone) subjectId = get_free_agent();
    if(subjectId != noone) {
        if(subjectPicker != noone) instance_destroy(subjectPicker);
        add_agent(agent_assignment_type.GENERAL, buttonId, subjectId);
        object.update = true;
        object.assignedAgents = get_assigned_agent_Ids(agent_assignment_type.GENERAL, buttonId);
    } else if(subjectId == noone) {
        subjectPicker = new_agent_selector(x2, x2 + AGENT_SELECTOR_WIDTH, y2, y2 + AGENT_SELECTOR_HEIGHT, object_index, get_general_local(gen_local.AGENT_SELECTOR), UI_LAYER_2);
    }  
}
New button commit-script:
GML:
function commit_agent_selection() {
    var _subjectId = buttonId;
    with(object) {
        subjectId = _subjectId;
        script_execute(script);
    }
}
 

Nidoking

Member
Is the value of the "script" variable the commit_agent_selection script/function, by any chance? If so, then you've created an infinitely recursive loop.
 

Amaror

Member
Is the value of the "script" variable the commit_agent_selection script/function, by any chance? If so, then you've created an infinitely recursive loop.
Within "object" the value of "script" is "commit_add_agent_general". I have also debugged it and know it runs the "commit_add_agent_general" function. The problem is after finishing the "script_execute(script);" part it just jumps back into the subjectId = _subjectId line and just keeps running the with bracket over and over again.
 

Amaror

Member
Found the culprit.
I gave the new_agent_selector the object_index which it further gave to the buttons it creates. That means the with was done on the object_index, meaning it ran on EVERY BUTTON I had at the time, not just one specific button. If I give it the id, instead of the object_index it works as intended.
 
Top