T
tijeseffff
Guest
I want to have a variable which has a name of array I want to use.
How I can do this?
How I can do this?
global.example = ["hello", "world"];
var arr = variable_global_get("example");
show_message(arr[0]); // hello
var arr = variable_global_get("example");
arr[@0] = "goodbye";
show_message(global.example[0]); // goodbye
Eh, I have a menu array,If you have a name reference in string form, use variable_global_get() or variable_instance_get() to get at the array, then subindex off it.
Assuming that you have something like this:
Example get using a string name:Code:global.example = ["hello", "world"];
Example set using a string name:Code:var arr = variable_global_get("example"); show_message(arr[0]); // hello
Code:var arr = variable_global_get("example"); arr[@0] = "goodbye"; show_message(global.example[0]); // goodbye
I am using GM:S 1.4 so I can't use global.exm = ["1", "2"]; vars.If you have a name reference in string form, use variable_global_get() or variable_instance_get() to get at the array, then subindex off it.
Assuming that you have something like this:
Example get using a string name:Code:global.example = ["hello", "world"];
Example set using a string name:Code:var arr = variable_global_get("example"); show_message(arr[0]); // hello
Code:var arr = variable_global_get("example"); arr[@0] = "goodbye"; show_message(global.example[0]); // goodbye
What does "@" do?Code:arr[@0] = "goodbye";
I am using GM:S 1.4. Array vars are not accessible to me. Do you know a different way?If you are referencing a var array created in a script that pulls its data from another array stored somewhere else the @ accessor makes sure you're changing the values of the array stored somewhere else, rather than the values in the temp array you created in the script.
It also happens without the @, if you call the script through an instance that contains the array. All the variables that are present in the calling instance are also present in the script.If you are referencing a var array created in a script that pulls its data from another array stored somewhere else the @ accessor makes sure you're changing the values of the array stored somewhere else, rather than the values in the temp array you created in the script.
I tried global.example = ["menu", "quitmenu"]; gm:s says unallowed character or something like thatWhat do you mean they are not accessible to you? Arrays are basically the same between gms1.4 and gms2...Do you know how arrays work properly? Do you know what a var is? (Also, I was responding to the person above my previous post, not to you).
global.example[0] = "menu";
global.example[1] = "quitmenu";
Eh, I mean I have two arrays. menu and quitmenuYes, that is not the correct way to reference an array in gms1.4.
Don't assume that because the first code you try doesn't work that it's a problem with GMS or that GMS doesn't support that thing, it's far more likely to be a problem with your codeCode:global.example[0] = "menu"; global.example[1] = "quitmenu";
var inner_menu;
inner_menu[0] = "New Game";
inner_menu[1] = "Load Game";
global.example[0] = inner_menu;
inner_menu = 0;
inner_menu[0] = "Quit Game";
inner_menu[1] = "Quit Options";
global.example[1] = inner_menu;
Yes, there are many situations where only the temp array will be changed and not the other array that you want to be changed. Also note, it's not that the variable isn't present, it's that you are creating a temp array that references another array, the @ accessor makes sure you are changing the referenced array and not the temp one. It is not a useless function at all.It also happens without the @, if you call the script through an instance that contains the array. All the variables that are present in the calling instance are also present in the script.
I guess it was not always that way?
And how to use it? Well at least with array_length_1d. And how to switch between them?Store an array in each array slot.
Code:var inner_menu; inner_menu[0] = "New Game"; inner_menu[1] = "Load Game"; global.example[0] = inner_menu; inner_menu = 0; inner_menu[0] = "Quit Game"; inner_menu[1] = "Quit Options"; global.example[1] = inner_menu;
menu_pos = 0;
var temp_arr = global.menu[menu_pos];
for (var i=0;i<array_length_1d(temp_arr);i++) {
//Access each inner menu and what you want to do with it here. I'll just pull the value from the inner menu and store it in another array called string
string[i] = string(temp_arr[i]);
}
It sort of works, it draws 5 positions, but I can select only between two. Like in quitmenu.Ok, so the only difference is that in order to access the inner array, you'll have to pull it into a temp variable first. And you don't need to 'switch' between them. You'll have a variable that holds what slot you are in with the outer array and then you simply check through the inner array stored at the global.menu position you want.
Now if you increase or decrease menu_pos, you'll be switching between your outer arrays ("menu" and "quitmenu") and then you just need to store that global.menu[menu_pos] in a temp array like above to access it.Code:menu_pos = 0; var temp_arr = global.menu[menu_pos]; for (var i=0;i<array_length_1d(temp_arr);i++) { //Access each inner menu and what you want to do with it here. I'll just pull the value from the inner menu and store it in another array called string string[i] = string(temp_arr[i]); }
var temp_arr = global.menuarray[0];
var k =0;
for (var i=0;i<array_length_1d(temp_arr);i++) {
k++;
}
show_debug_message("K = "+string(k));
Eh, Now I can access 5 selections, but when I select Quit, nothing happens.So if you run this code:
Does the console output "K = 2" or "K = 5"?Code:var temp_arr = global.menuarray[0]; var k =0; for (var i=0;i<array_length_1d(temp_arr);i++) { k++; } show_debug_message("K = "+string(k));
EDIT: Ok, good.
https://transfer.sh/Tag1Z/newtutorial_platformer2.gmzI'd have to see more of the code to tell what's going on. Playing around with arrays stored in arrays can become difficult. You've got to make sure that you totally understand what is being stored where in order for it to work correctly.
Hello again, I wrote this because menu selections are in Russian language, (also you can ask any question about russia if u want ;D)Ok, I'm heading to work now, I'll have a look at it when I get back.
So, do you find the problem? I tried. I can'tOk, I'm heading to work now, I'll have a look at it when I get back.
idk. just followed shaun's menu tutorial. rm_initialize creation code.Why did you make temp_arr global? It's explicitly in it's name "temp" array. And why are you using mpos in a switch statement? Also, where are you setting up global.temp_arr?
/// @description Create the arrays
// This stores whether you are on the New Game menu page or the Quit Game menu page.
menu_pos = 0;
// This stores which sub-menu option you have selected on the New Game or Quit Game menu page.
sub_menu_pos = 0;
/* First, we're creating a 2D array to store the top-level (New Game and Quit Game) of the menu.
We're using a 2D array because we want to have a title stored, plus an array of sub-menu options. */
menu_top[0,0] = "New Game"; //The title for the menu
menu_top[0,1] = array_create(1); //Create the array to store the sub-menu options and store it in the second slot of the 2D array.
menu_top[1,0] = "Quit Game"; // Not too sure why you want Quit Game as a menu header, but whatever.
menu_top[1,1] = array_create(1);
// Here we extract the above created array in order to populate it with sub-menu options for the New Game menu.
var temp_arr = menu_top[0,1];
temp_arr[@ 0] = "Quick Game"; // Because we're using a temp array, we make sure the use the accessor (@) to store the value, which makes sure the original array gets changed.
temp_arr[@ 1] = "Custom Game";
temp_arr[@ 2] = "Load Game";
// Here we extract the above created array in order to populate it with sub-menu options for the Quit Game menu.
var temp_arr = menu_top[1,1];
temp_arr[@ 0] = "Quick Quit";
temp_arr[@ 1] = "Save Then Quit";
menu_length = array_height_2d(menu_top); // Here we just store the length of the menu options because it will be useful later.
/// @description Catch input
menu_pos += keyboard_check_released(ord("D"))-keyboard_check_released(ord("A")); // Get direction for menu switching.
if (menu_pos < 0) {
menu_pos = menu_length-1; // Make sure we don't go below 0, which'll break the array.
}
menu_pos = menu_pos mod menu_length; // Mod the number to it's array length so it won't break the array.
var temp_arr = menu_top[menu_pos,1]; // Extract the current sub-menu array so we can figure out how big it is.
sub_menu_pos += keyboard_check_released(ord("S"))-keyboard_check_released(ord("W")); // Get direction for sub-menu switching.
var sub_length = array_length_1d(temp_arr); // Get the length of the sub-menu array.
if (sub_menu_pos < 0) {
sub_menu_pos = sub_length-1; // Make sure we don't go below 0.
}
sub_menu_pos = sub_menu_pos mod sub_length; // Mod to cap max number as above.
/// @description Draw the menu
var menu_options = menu_top[menu_pos,1]; // Get the sub menu options.
var sub_length = array_length_1d(menu_options); // Get the length of the sub menu options array.
var base_col, temp_col; // Create a base and temp colour var for later.
draw_set_halign(fa_center); // Text setup
draw_set_valign(fa_middle);
for (var i=0;i<array_height_2d(menu_top);i++) { // Loop through all your entries in the top level menu.
if (menu_pos == i) { // If menu_pos equals the menu option we are currently looping through
base_col = c_white; // Then set the base_col to white
for (var c=0;c<array_length_1d(menu_options);c++) { // And then draw the sub-menu options, by looping through them.
if (sub_menu_pos == c) { // If the sub_menu_pos equals the sub menu option we are currently looping through.
temp_col = c_orange; // Then set the temp_col to orange, which will be our colour to show currently selected options.
}
else {
temp_col = c_white; // Else make it white.
}
draw_set_color(temp_col); // Set the colour to the temp_col variable.
draw_text(room_width/2-150+(400*i),200+50*c,menu_options[c]); // Draw the the text for each sub-menu option. NOTE: This +(400*i) thing only works because there's two menu options, if there were more it'd have to be tweaked.
}
}
else {
base_col = c_gray; // If menu_pos doesn't equal i, then set the base_col to gray, to show that that menu option is not currently selected.
}
draw_set_color(base_col); // Set the draw colour.
draw_rectangle(room_width/2-200+(400*i),100,room_width/2-100+(400*i),150,true); // Draw a rectangle for the top level menu options.
draw_text(room_width/2-150+(400*i),125,menu_top[i,0]); // Draw the text for each top level menu option.
}
Thanks!Put that into a fresh object and put that object into a new room and run that room. Play around with it and read through my code, I documented it as heavily as I could. It might not be exactly what you are looking for, but everything you need to know about storing things and extracting things from arrays is all there and you should be able to use it as a foundation for what you need moving forward.
Your code is pretty easy except one linePut that into a fresh object and put that object into a new room and run that room. Play around with it and read through my code, I documented it as heavily as I could. It might not be exactly what you are looking for, but everything you need to know about storing things and extracting things from arrays is all there and you should be able to use it as a foundation for what you need moving forward.
var centerx = room_width/2; // Get the center of the room
var leftx = centerx - 200; // Change this to a large number to make the rectangle larger
var rightx = centerx - 100; // There's 100px difference between the leftx and rightx, which is how large the rectangle will be
var step = 400*i; // The 400 here, is because from leftx, it takes 200 pixels to meet the center of the room, and then another 200 pixels to be properly positioned on the right hand side. If you change the leftx number, you have to make the 400 here double the leftx number
draw_rectangle(leftx+step,100,rightx+step,150,true);
Well I'm very bad at math, or even "complicated math" but you docummented this code very good so maybe I will understand.Just change the 200 to a large number. Or you can just rewrite all the maths for the positioning to get it wherever you want. I'll rewrite that section of code to better explain what I'm doing:
Code:var centerx = room_width/2; // Get the center of the room var leftx = centerx - 200; // Change this to a large number to make the rectangle larger var rightx = centerx - 100; // There's 100px difference between the leftx and rightx, which is how large the rectangle will be var step = 400*i; // The 400 here, is because from leftx, it takes 200 pixels to meet the center of the room, and then another 200 pixels to be properly positioned on the right hand side. If you change the leftx number, you have to make the 400 here double the leftx number draw_rectangle(leftx+step,100,rightx+step,150,true);
/// @description Draw the menu
var menu_options = menu_top[menu_pos,1]; // Get the sub menu options.
var sub_length = array_length_1d(menu_options); // Get the length of the sub menu options array.
var base_col, temp_col; // Create a base and temp colour var for later.
var x_buffer = 50; // This is the space between your menu option boxes.
var x_start = 100; // This is the x coord the whole menu system will be aligned to
var text_width;
var y_buffer = 50; // How much space you want vertically between each sub-menu option.
var y_start = 200; // What y value you want the sub-menu options to start at.
draw_set_halign(fa_center); // Text setup
draw_set_valign(fa_middle);
draw_set_font(your_font); // Set the font you want here
for (var i=0;i<array_height_2d(menu_top);i++) { // Loop through all your entries in the top level menu.
text_width[i] = string_width(menu_top[i,0])+25; // Get the width in pixels of the current menu option.
var prev_width = 0; // Setup a variable to store all previous menu option sizes.
for (var k=0;k<i;k++) { // Loop from 0 until i-1.
prev_width += text_width[k]; // Add each previous menu option size to current_width
}
if (menu_pos == i) { // If menu_pos equals the menu option we are currently looping through
base_col = c_white; // Then set the base_col to white
for (var c=0;c<array_length_1d(menu_options);c++) { // And then draw the sub-menu options, by looping through them.
if (sub_menu_pos == c) { // If the sub_menu_pos equals the sub menu option we are currently looping through.
temp_col = c_orange; // Then set the temp_col to orange, which will be our colour to show currently selected options.
}
else {
temp_col = c_white; // Else make it white.
}
draw_set_color(temp_col); // Set the colour to the temp_col variable.
draw_text(x_start+prev_width+(x_buffer*i)+(text_width[i]/2),y_start+y_buffer*c,menu_options[c]); // Draw the the text for each sub-menu option. NOTE: This +(400*i) thing only works because there's two menu options, if there were more it'd have to be tweaked.
}
}
else {
base_col = c_gray; // If menu_pos doesn't equal i, then set the base_col to gray, to show that that menu option is not currently selected.
}
draw_set_color(base_col); // Set the draw colour.
draw_rectangle(x_start+prev_width+(x_buffer*i),100,x_start+prev_width+(x_buffer*i)+text_width[i],150,true); // We start the left hand side of the rectangle at x_start, then add the width of all the previous menu items (which stored in current_width) then add a small buffer to make sure the boxes are separated. For the right side, we do the same as the left side, but also add the width of the menu option that is currently being looped through (text_width[i]).
draw_text(x_start+prev_width+(x_buffer*i)+(text_width[i]/2),125,menu_top[i,0]); // Draw the text for each top level menu option at the some place as the rectangle above, but divide text_width[i] by half to get the center of the rectangle.
}
Thanks. The last question, how the react at the enter button press? I found only vk_up,down,left,right in the step event.Ok, here's something that should auto-resize:
Code:/// @description Draw the menu var menu_options = menu_top[menu_pos,1]; // Get the sub menu options. var sub_length = array_length_1d(menu_options); // Get the length of the sub menu options array. var base_col, temp_col; // Create a base and temp colour var for later. var x_buffer = 50; // This is the space between your menu option boxes. var x_start = 100; // This is the x coord the whole menu system will be aligned to var text_width; var y_buffer = 50; // How much space you want vertically between each sub-menu option. var y_start = 200; // What y value you want the sub-menu options to start at. draw_set_halign(fa_center); // Text setup draw_set_valign(fa_middle); draw_set_font(your_font); // Set the font you want here for (var i=0;i<array_height_2d(menu_top);i++) { // Loop through all your entries in the top level menu. text_width[i] = string_width(menu_top[i,0])+25; // Get the width in pixels of the current menu option. var prev_width = 0; // Setup a variable to store all previous menu option sizes. for (var k=0;k<i;k++) { // Loop from 0 until i-1. prev_width += text_width[k]; // Add each previous menu option size to current_width } if (menu_pos == i) { // If menu_pos equals the menu option we are currently looping through base_col = c_white; // Then set the base_col to white for (var c=0;c<array_length_1d(menu_options);c++) { // And then draw the sub-menu options, by looping through them. if (sub_menu_pos == c) { // If the sub_menu_pos equals the sub menu option we are currently looping through. temp_col = c_orange; // Then set the temp_col to orange, which will be our colour to show currently selected options. } else { temp_col = c_white; // Else make it white. } draw_set_color(temp_col); // Set the colour to the temp_col variable. draw_text(x_start+prev_width+(x_buffer*i)+(text_width[i]/2),y_start+y_buffer*c,menu_options[c]); // Draw the the text for each sub-menu option. NOTE: This +(400*i) thing only works because there's two menu options, if there were more it'd have to be tweaked. } } else { base_col = c_gray; // If menu_pos doesn't equal i, then set the base_col to gray, to show that that menu option is not currently selected. } draw_set_color(base_col); // Set the draw colour. draw_rectangle(x_start+prev_width+(x_buffer*i),100,x_start+prev_width+(x_buffer*i)+text_width[i],150,true); // We start the left hand side of the rectangle at x_start, then add the width of all the previous menu items (which stored in current_width) then add a small buffer to make sure the boxes are separated. For the right side, we do the same as the left side, but also add the width of the menu option that is currently being looped through (text_width[i]). draw_text(x_start+prev_width+(x_buffer*i)+(text_width[i]/2),125,menu_top[i,0]); // Draw the text for each top level menu option at the some place as the rectangle above, but divide text_width[i] by half to get the center of the rectangle. }
Yay! I did it myself. At first I copied push var from Shaun's codeOk, I'm heading off to work now, but I'll give ya one last bit of help when I get back. While I'm gone, you can try on your own. The key is to make the sub-menu options array a 2D array instead of a 1D array and store a script that has the code you want to happen for each sub-menu option in the second slot (you'll have to go through all the code and make it so that everything that references the sub-menu array properly deals with a 2D array instead of a 1D array). But basically, you just check what sub-menu slot is selected when you hit enter and then you script_execute the script that's attached to the 2nd column of the array.
var push = keyboard_check_released(vk_enter);
if (push == 1) scr_menu();
if (menu_pos == 0) {
switch (sub_menu_pos)
{
case 0: room_goto(rm_1); break;
case 1: game_end(); break;
case 2: game_end(); break;
default: break;
}
} else if (menu_pos == 1) {
switch (sub_menu_pos)
{
case 0: room_goto(thisisanoptions_crashthegame); break;
case 1: game_end(); break;
default: break;
}
}
if (global.issoundstopped == 0) and audio_is_playing(snd_menu) {}
else if (global.issoundstopped == 1) and (audio_is_playing(snd_menu)) {audio_stop_all();}
else if (global.issoundstopped == 0) and (!audio_is_playing(snd_menu)) {audio_play_sound(snd_menu,1,true);}
else if (global.issoundstopped == 1) and (!audio_is_playing(snd_menu)) {}
Thanks =) now it's time to implement Level Select, and also pause the whole game and displaying menu w/o going to rm_menu. Also I will need to design levels which is the bad part, my school "Art" grades are very bad, in some time my girlfriend helps me, but idk how long it will take to teach her the GM:S 1.4 Level Editor and the F5 button.Awesome job mate, I'm glad you were involved enough in understanding what I was doing to be able to adapt it. Next thing you know you'll be coding things like this without a second thought. Keep it up =)
Awesome job mate, I'm glad you were involved enough in understanding what I was doing to be able to adapt it. Next thing you know you'll be coding things like this without a second thought. Keep it up =)
if (global.paused == 0) global.paused = 1; else if (global.paused == 1) global.paused = 0; // pause the whole frickin' game
if (global.paused == 1) {
instance_create(100,100,obj_menu);
} else if (global.paused == 0) instance_destroy(obj_menu);
if (global.pause) {
if (global.pause == 1) {
if (!global.paused) {
if (global.pause == 0) {
I already know that, from Shaun's tutorials.As a tip to save a bit of typing,
Functions identically toCode:if (global.pause) {
AndCode:if (global.pause == 1) {
Functions identically toCode:if (!global.paused) {
Code:if (global.pause == 0) {
https://drive.google.com/open?id=1oui8WUX1woyIDPckGoPIMDtrddfWhSgD so here what I've done.As a tip to save a bit of typing,
Functions identically toCode:if (global.pause) {
AndCode:if (global.pause == 1) {
Functions identically toCode:if (!global.paused) {
Code:if (global.pause == 0) {