Windows dynamic object creation

scorpafied

Member
ok so what im trying to do is create a global array, and assign each index with a new object so i can put variables on. the goal of this essentially is im trying to create a better level unlocking system then i currently have which isnt quite so linear.

im still new to gml, in other languages id write this:
Code:
for(var i =1; i>=4; i++){
    global.lvlData[i] = new Object();
    if(i!=1){
        global.lvlData[i].unlocked = 0;
        global.lvlData[i].beat = 0;
    } else global.lvlData[i].unlocked = 1;
}
then in the draw event i can check if something is unlocked or beaten. and assign the sprite accordingly. trying to work out how to do it gml, but cant for the life of me work it out. can someone help?
 
C

Catastrophe

Guest
Well if you're just storing information, ds_maps (hashmaps basically) are a good way to do this. E.g.

Code:
for(var i =1; i>=4; i++){
   global.lvlData[i] = ds_map_create();
   if(i!=1){
         ds_map_add(global.lvlData[i],"unlocked",0)
          ds_map_add(global.lvlData[i],"beat",0)
   } else ds_map_add(global.lvlData[i],"unlocked",1)
}
 
Last edited by a moderator:

Joe Ellis

Member
And arrays are the best way, plus they have the same syntax that you're using, but it's better to declare a local var of said array before reading\writing to, for performance.
And you can use the special "repeat" loop, It's basically a for loop at compiled level, telling it to loop n times. But gml code is simpler and nicer with these instead of for loops, plus for loops in gml are wrapped and aren't actually real for loops in c++ standard, whereas repeat loops are. Which is why they're the fastest.
 

scorpafied

Member
Well if you're just storing information, ds_maps (hashmaps basically) are a good way to do this. E.g.

Code:
for(var i =1; i>=4; i++){
   global.lvlData[i] = ds_map_create();
   if(i!=1){
         ds_map_add(global.lvlData[i],"unlocked",0)
          ds_map_add(global.lvlData[i],"beat",0)
   } else ds_map_add(global.lvlData[i],"unlocked",1)
}
thanks for the reply. i was initially having trouble with the ranges when refencing. but discovered issue was with the loop. as it was starting at 0, but refering a fourth index which didnt exist. this is now solved.

thanks guys

And arrays are the best way, plus they have the same syntax that you're using, but it's better to declare a local var of said array before reading\writing to, for performance.
And you can use the special "repeat" loop, It's basically a for loop at compiled level, telling it to loop n times. But gml code is simpler and nicer with these instead of for loops, plus for loops in gml are wrapped and aren't actually real for loops in c++ standard, whereas repeat loops are. Which is why they're the fastest.
due to the fact ill be making changes to the array after a level is beaten which will be in a different room. i think it will have to remain global if im to reference it. but i can definitely use a repeat loop if that will be faster.

thanks for the tip
 
Last edited:
Top