I wish I had the skill of @FrostyCat and @BPOutlaws_Jeff. I'd love to be able to code like that.
lol I'm super flattered! But I'm like a massive amateur & self-taught so I spend half my time looking like the "I have no idea what I'm doing" dog meme lol I'm always trying to get better though. I went through a phase earlier in the year where I was trying to obsessively overly-optimize every single line and reduce everything into as little code as possible like some amazing hacker pro "look at me I'm John Carmack" lol but then I went back to code like this a couple months later:
GML:
if ((ANIM_CUR == ANIM_LAST && ANIM_DONE)) {exit;}
ANIM_NUM_LOOPS -= ANIM_NUM_LOOPS > 0;
if (ANIM_CUR < ANIM_LAST && ANIM_DONE)
{ANIM_CUR += ANIM_DONE - ANIM_LAST ? ANIM_CUR : ANIM_CUR;}
if (ANIM_NUM_LOOPS != 0) {scr_anim_play();}
//scr_anim_play(ANIM_CUR + (ANIM_CUR < ANIM_LAST));//force anim
...and was like ya I have literally NO idea wtf this does anymore and I had to spend an hour trying to decipher it and re-writing it out but expanded and step by step to try to follow my previous self's insanity all just to change some tiny thing lol
So now I'm trying to just write my code as clean and readable as possible. That's why I'm loving this struct stuff, getting rid of all that "///@arg _whatever var _whatever = argument3;" stuff and getting rid of bulky looking 2D arrays full of enums and being able to just use ".update()" on a dozen things instead of "scr_button_update()" "scr_player_update()" and "scr_anim_update()" etc looks so much cleaner and minimalist without being hard to understand when you just glance through it.
Also it's cool to see the pro level coders here getting to finally drop some of their wisdom bombs on us, I know they've been waiting for years for these kinds of features because other languages have them so us getting to see what kind of cool things they can show us that we can go apply with all this is awesome. Like
@FrostyCat showing
@Mool's code can be reduced even more to just direct calls on the last page...that stuff is eye-opening!
And on the subject of learning, I was experimenting with the imgui concept back when I was looking into efficient ways to make menus and I tried roughing out an understanding of how I could do an imgui with this new GML stuff and ended up with this general idea (again I'm sure the syntax is way off, I don't actually have the beta yet I'm just trying to get a head start learning):
GML:
//some gui Draw Event:
gui_x = 0;
gui_y = 0;
if (new Button(gui_x, gui_y, "File").is_pressed()) {
if (new Button(gui_x, gui_y, "New" ).is_pressed()) {scr_new();}
if (new Button(gui_x, gui_y, "Open").is_pressed()) {scr_open();}
if (new Button(gui_x, gui_y, "Save").is_pressed()) {
if (new Button(gui_x, gui_y, "Some other button").is_pressed()) {
scr_save();
}
}
if (new Button(gui_x, gui_y, "Quit").is_pressed()) {scr_quit();}
}
//--------------------------------------------------
function Button(_x1, _y1, _label) constructor {
var _width = 100,
_height = 20;
is_pressed = function() {
var _is_pressed = update();//check for interaction
draw();
gui_y += _height;//next button will be beneath this one
return _is_pressed;
}
update = function() {
bg_color = c_white;
//hovering
if (point_in_rectangle(mouse_x, mouse_y, _x1, _y1, _x1 + _width, _y1 + _height)) {
bg_color = hovering_color;
//clicked
if (mouse_check_button_pressed(mb_left)) {
bg_color = clicked_color;
return true;
}
}
}
draw = function() {
draw_set_color(bg_color);
draw_rectangle(_x1, _y1, _x1 + _width, _y1 + _height, false);
draw_set_color(c_black);
draw_text(_x1, _y1, _label);
}
}
And that experiment got me wondering some quick questions for anyone who's more experienced:
1) Would the above be super slow because it's making all these "new Button" structs every step?
2) Would doing something with "static" fix that? It seems to be something to do with only doing things once but I don't understand exactly what it's for yet...
3) Would you need to delete the structs each step memory-wise, like is this making a million button structs in memory? lol
4) ...or is trying to use structs for an imgui a case of the right tool for the wrong job? Doing it with scripts was pretty straightforward and you get to just do "if (scr_button(x, y, "Label")) {}" instead of having to write "new" and call a function inside it to get it to do something, plus the potential memory/slowness issues I'm referring to in the above 3 questions lol This is more just an exercise in curiosity & learning.
Actually I guess if something just exists for one frame there's not really any point using a struct for it since you're just calling all the functions at once...might as well just use a script for that. A struct probably only makes sense if you need the thing to stick around hmm...so ya this might be the wrong tool for doing an imgui
5) Also can you do something like this in a struct where some actual code (not just assigning variables) is run only once upon creation of the struct:
GML:
function Button(_x1, _y1, _label) constructor {
var _width = 100,
_height = 20;
//would this block of code below that's not inside any function only run once
//upon creation, removing the need for the ".is_pressed()" in the other version?:
var _is_pressed = update();
draw();
gui_y += height;
return _is_pressed;
update = function() {return whatever;}
draw = function() {draw whatever;}
}
...or does a struct always need something external to call at LEAST one function inside it (and from there it can call its own other functions)?