GameMaker How to create an algorithm

B

blabluxd

Guest
Hello dear community :)

I really need help with this peace of code. I have a Level room where there is an Object that checks, when created, how much stars (0-3) each level has saved on the ini. Then it changes the image_index of each level button to match the amout of stars. 3 stars are yellow, 1-2 stars are blue and 0 stars are grey.

My problem is that my code is very inefficient. Right now I have six levels for testing but later on I will need a lot more. How could I make an algorithm to do the same thing in less code?

CODE:

ini_open("saveData.ini");
level1 = ini_read_real("Stars", "level1", 0);
level2 = ini_read_real("Stars", "level2", 0);
level3 = ini_read_real("Stars", "level3", 0);
level4 = ini_read_real("Stars", "level4", 0);
level5 = ini_read_real("Stars", "level5", 0);
level6 = ini_read_real("Stars", "level6", 0);
global.coins = ini_read_real("Player", "Coins", 0);
ini_close();

if (level1 > 0 and level1 <3)
{
with (obj_level1) image_index = 1;
} else if (level1 == 3)
{
with(obj_level1) image_index = 0;
}


if (level2 > 0 and level2 <3)
{
with (obj_level2) image_index = 1;
} else if (level2 == 3)
{
with(obj_level2) image_index = 0;
}

I maybe thought of doing it with a repeat but it didnt work:

var i = 0;
var level = "level"+string(i);

repeat(6)
{
i += 1;
if (level+string(i) > 0 and level+string(i))
{
with (obj_level+string(i)) image_index = 1;
}
}

etc.....

but it didnt work now Im askig you if you could help me creating such an algorithm like I tried correctly? Because I dont know how :(

Thank you very much :eek:

 
What you want to do is use a 1D array to store your "level[x]" variables. You can also have a global variable track how many levels you have. Other kinds of loops would work perfectly fine, but a for loop would help in your case.
Code:
ini_open("saveData.ini");
for (var i = 0; i < global.total_levels; ++i) {
  level[i] = ini_read_real("Stars", "level"+string(i+1), 0);
}
global.coins = ini_read_real("Player", "Coins", 0);
ini_close();
You can read more about for loops and how they work in the manual here under the "for" section.
 

2Dcube

Member
You are not too far off.

Like nacho_chicken said, look into how a "for loop" works. You almost made one with repeat and i+=1.
 
H

Homunculus

Guest
The first thing I'd do is store the the stars you read from the ini file into an array instead of numbered variables (as @nacho_chicken suggested). This way, knowing in advance the total number of levels you have, you can read the values in a loop instead of one by one (being also forced to add a line of code for every new level you add to the game).
Keep a variable holding the total number of levels to avoid hardcoding that number in every place where you may need it.

The logic to show the right image index for every level can also be condensed, but first of all I'd consider using a single object for the level buttons instead of an object per level. You may have good reason to do so, but if all the difference between those objects is pointing to a different room or showing a different text, having just one object with a bit more logic may help streamline your code.
If you follow this route, you should assign a level number variable to every instance of the level buttons (possibly using variables definitions in the object / room editor, or by programmatically creating the buttons) and refer to that number to look for the number of stars in your array.
 
Last edited by a moderator:
B

blabluxd

Guest
What you want to do is use a 1D array to store your "level[x]" variables. You can also have a global variable track how many levels you have. Other kinds of loops would work perfectly fine, but a for loop would help in your case.
Code:
ini_open("saveData.ini");
for (var i = 0; i < global.total_levels; ++i) {
  level[i] = ini_read_real("Stars", "level"+string(i+1), 0);
}
global.coins = ini_read_real("Player", "Coins", 0);
ini_close();
You can read more about for loops and how they work in the manual here under the "for" section.
Oh man it worked fine! I created that global variable. Now I know that this is called loop and not algorithm I can learn more about it. Thank you soooo much! Have a nice day <3
 
B

blabluxd

Guest
The first thing I'd do is store the the stars you read from the ini file into an array instead of numbered variables (as @nacho_chicken suggested). This way, knowing in advance the total number of levels you have, you can read the values in a loop instead of one by one (being also forced to add a line of code for every new level you add to the game).
Keep a variable holding the total number of levels to avoid hardcoding that number in every where place you need it.

The logic to show the right image index for every level can also be condensed, but first of all I'd consider using a single object for the level buttons instead of an object per level. You may have good reason to do so, but if all the difference between those objects is pointing to a different room or showing a different text, having just one object with a bit more logic may help streamline your code.
If you follow this route, you should assign a level number variable to every instance of the level buttons (possibly using object variables definitions in the room editor) and refer to that number to look for the number of stars in your stars array.
Thanks. About the buttons. I have so many buttons because I dont know how I could differenciate them with code. They all do go to the same room but before they go to the room they run a script that changes variables that decide the propretys of the level. If I placed 6 times only one button i wouldnt know how to make gms know which one should run scr_level1 when clicked and which one scr_level2 :)
 
H

Homunculus

Guest
Thanks. About the buttons. I have so many buttons because I dont know how I could differenciate them with code. They all do go to the same room but before they go to the room they run a script that changes variables that decide the propretys of the level. If I placed 6 times only one button i wouldnt know how to make gms know which one should run scr_level1 when clicked and which one scr_level2 :)
A single variable, representing the level number, in every instance of the same button can be enough to be able to differentiate between them. If you want to create the buttons by dragging them into the room editor, variable definitions can do the trick.
As said above another way to do it is creating the button instances programmatically from the controller object, but it's up to you to chose the method you prefer.
 
Last edited by a moderator:
B

blabluxd

Guest
A single variable, representing the level number, in every instance of the same button can be enough to be able to differentiate between them. If you want to create the buttons by dragging them into the room editor, variable definitions can do the trick.
As said above another way to do it is creating the button instances programmatically from the controller object, but it's up to you to chose the method you prefer.
I did it with the code you explained: I create the buttons in the controller object, save their id, change the level variable of the instance. In the object of the button is a code that runs the script depending on the level variable. So it works perfectly. Thank you for that clever method of handling a lot of buttons. Greetings from Tanzania bro :)
 
Top