Question 1: https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-390989
GML QUESTION: An ancient dispute: to make a colored rectangle, is it more efficient to use the draw rectangle function or to stretch a one-pixel sprite? If the latter, does it negate the efficiency by using a white one-pixel sprite and coloring it with the draw sprite function?
@RK says
It all depends on what you are asking by efficiency, on the CPU or the GPU, both will result in the same number of pixels rendered (so the same work on the GPU, but the main cost here would be if alpha blending is enabled at all, if you do not need it then disable alpha blending as it halts the speed of any GPU write operations) on the CPU side of things it all depends on the construction of the vertex buffer if you can have a frozen vertex buffer pre-created and reuse that then it would be even faster.
But the real question is why are you bothered? I doubt drawing the rectangle is the most expensive operation and I suspect that you are focussing on the wrong thing, profile the code and find out where the bottlenecks are used the built-in profiler for the GML code and I personally use RenderDoc (on Windows) to look at the render steps when profiling any rendering operations.
Question 2:https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-391218
GML QUESTION: I have a DS Grid I need to export to json. What is the best practice for handling that?
Already answered by Frosty Cat in https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-391224
@RK says
Frosty Cat has already given a good answer, I cannot really add anything to that.
Question 3: https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-391225
I've been using mp_grids not only for pathfinding, but also as part of a simple collision check system. I'm early in my project and haven't seen any problems with it but is there any disadvantage of getting a cell's status frequently through the mp_grid functions? As opposed to using a simple 2D array or the tile-based method mentioned in the blog?
@RK says
I suspect that long term tilemaps or a 2D array may well be faster in the long run but profile your code regularly to see where the slowdowns occur, and focus on the problem areas that actually exist, rather than worrying about code that you only suspect is causing slowdowns.
If you organise your code properly then your pathfinding and collision check system should be relatively easily replaced in the future when you discover that it is actually a problem area.
Question 4: https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-391897
I have a question about a limitation of GML. If I create a constant (e.g. #macro c_black32 make_colour_rgb(24,20,37)) GM will still error if I use it as a case for a switch statement.
E.g:
Switch (color) {
case c_black32:
dothing()
break;
}
will throw an error stating that you can only use constants as cases. However, I was under the impression that macros were constants. Am I wrong or is this an oversights?
Already answered in https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-391902
and https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-391902 and https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-394363
@RK says
The answers above are pretty comprehensive, Macros are not always constants and switch-case statements need to be constants, while the arguments to make_colour_rgb() are constants the function itself is not a compiler intrinsic so the compiler cannot turn it into a constant (the compiler does know a few intrinsic functions which it can process if it gets constant arguments to those functions, these are ord(), chr(), int64(), real(), string() ).
Question 5: https://forum.yoyogames.com/index.php?threads/best-practices-gml-follow-up-ama-2.65317/#post-394368
GML QUESTION: How expensive is creating and destroying objects in game. For single use instances such as bullets or FX is it better to destroy the instance or deactivate it. Should you create all required instances at room start and just deactivate/activate or recycle as needed. I know every situation is different but the garbage collector is a black box so I'm not sure which is best.
@RK says
I would always avoid allocating new instances at runtime as much as possible, use strategies like a simple Stack Allocation to recycle old objects, but a good rule of thumb is: only do as much work as you need to. Recycling objects is a good strategy and minimises Memory Allocation and construction overhead.