Simple Reference Question

H

HUMMAN

Guest
Well the question is simple, but i couldnt find an exact answer with research.
I want a functionality like this:

number=5;
reference_number=number;
reference_number=10;

Output:
number=10

Is it possible to do it in GML with a syntax like macro, var or something?
 

chamaeleon

Member
Well the question is simple, but i couldnt find an exact answer with research.
I want a functionality like this:

number=5;
reference_number=number;
reference_number=10;

Output:
number=10

Is it possible to do it in GML with a syntax like macro, var or something?
No, GMS does not have references or a syntax for it. Most time I see someone asking about it, I immediately think that some kind of data structure would be a more natural way to go about it in most cases. Having said that, if you are determined to do this anyway and you're dealing with instance or global variables or structs, you have the option of using the instance, global, and struct get/set functions using a string as the variable name and by passing different strings you can get or set a different global or instance variable. Additionally, arrays are passed by reference, and using the @ accessor modifies the referenced array for the given position rather than using copy-on-write.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
The described effect is possible with macros, although I'm not sure if it's exactly what you're after. You can define a macro as a short-hand name of another variable (among other things). That would be called an "alias", not a "reference" (as in "pass by reference"), though. It won't be a reference to anything, it will merely replace all instances of the macro key (PLAYER_HP) with its value (global.player_character_health_points).

GML:
global.player_character_health_points = 5;

#macro PLAYER_HP global.player_character_health_points

PLAYER_HP = 10;

show_debug_message(global.player_character_health_points); // 10

Structs (and only structs) are always passed by reference. Data structures could be remotely described as similar to pass by reference - they are an edge case, as you are really passing around their ID and then accessing the actual structure by using the ID as a handle. Arrays behave similarly to pass by reference, but only when written to via their @ accessor (this will write to the original, referenced array), as they are otherwise copy-on-write (which will create a copy of the original array in its current state, unlink the copy and write to it).

Edit: Clarity.
 
Last edited:
H

HUMMAN

Guest
If you are any interested here is my below code:
GML:
function d_road_construct(args_d_v_dr_s_w_r){
    var day_wait=args_d_v_dr_s_w_r[0];
    var vill_gone=args_d_v_dr_s_w_r[1];
    var day_reducement=args_d_v_dr_s_w_r[2];
    var stone_requirement=args_d_v_dr_s_w_r[3];
    var wood_requirement=args_d_v_dr_s_w_r[4];
    var rank=args_d_v_dr_s_w_r[5];
   
   
   
    var district_manage=noone;
    with(oGUI)
    {
    district_manage=selected_district;   
    }
   
   
    var complete=false;
    var under_work=false;
    with(district_manage)
    {
        complete=buttons[rank][4]; //this is how i would like to handle particular array member but cant refer
        under_work=buttons[rank][5];//this is how i would like to handle particular array member but cant refer
       
    }
   
   
with(oPopulation)

{
   
if(idle_pop>=vill_gone) && (stone >= stone_requirement) &&
(wood>=wood_requirement) &&(!district_manage.buttons[rank][BUTTON.COMPLETE]) &&(!district_manage.buttons[rank][BUTTON.UNDER_WORK])
{    with(oGUI)
    {    can_execute=true;
    }
    district_manage.buttons[rank][BUTTON.UNDER_WORK]=true;  // under work
    order_number++;
    idle_pop-=vill_gone;
    stone-=stone_requirement;
    wood-=wood_requirement;
   
   
order[order_number][VARIABLES.DAY]=day_wait;
order[order_number][VARIABLES.POP]=vill_gone;
args=[district_manage,day_reducement,rank];
var sc=d_road_completed;
order[order_number][VARIABLES.TRIGGER]=[sc,args];



order[order_number][VARIABLES.E_SIZE] = 0;
}

else
{
    with(oGUI)
    {    can_execute=false;
    }
}

}


}
For now, i assigned structs for the particular array members: complete and under_work, while it is clumsy i will menage with it.
 
Last edited by a moderator:
Top