S
Sam (Deleted User)
Guest
Taken over by @Evanski
originally by Samual Venable
The gmc Code bank. Just a place to share and talk about useful code snippets, whether raw GML or stuff useful to build upon for native extensions.
Not all code snippets must be written by advanced users! Most notably, GML can be shared here--and if it's useful--doesn't matter if it was easy to write.
Please give explanations of what your code does and how to use it. This'll be a good resource.
Just post it!
Have questions about a code snippet? Do not clog or distract the goodness of this topic. PM the post's author, and hope they get back to you.
Have fun and I hope this topic gets a lot of love.
Thread Index
A index project, to index and host a "database" of the code snippets in this topic, Im currently only adding the GML code to the index
originally by Samual Venable
The gmc Code bank. Just a place to share and talk about useful code snippets, whether raw GML or stuff useful to build upon for native extensions.
Not all code snippets must be written by advanced users! Most notably, GML can be shared here--and if it's useful--doesn't matter if it was easy to write.
Please give explanations of what your code does and how to use it. This'll be a good resource.
Just post it!
Have questions about a code snippet? Do not clog or distract the goodness of this topic. PM the post's author, and hope they get back to you.
Have fun and I hope this topic gets a lot of love.
Thread Index
A index project, to index and host a "database" of the code snippets in this topic, Im currently only adding the GML code to the index
GitHub - EvanSkiStudios/GMC-Code-Bank: An index of the GMC_code-bank_topic
An index of the GMC_code-bank_topic. Contribute to EvanSkiStudios/GMC-Code-Bank development by creating an account on GitHub.
github.com
Similar to the VBForums code bank, we could seriously use one. Just a place to share and talk about useful code snippets, whether raw GML or stuff useful to build upon for native extensions. I'm only putting this in the advanced forum because native extension code is permitted to be shared here. Not all code snippets must be written by advanced users! Most notably, GML can be shared here--and if it's useful--doesn't matter if it was easy to write. Please give explanations of what your code does and how to use it. This'll be a good resource.
Just post it!
Have questions about a code snippet? Do not clog or distract the goodness of this topic. PM the post's author, and hope they get back to you.
------------------------
I'll start.
For example, here's a snippet I wrote for a much larger project recently, but is useful for much more than being shared as an extension on its own; I'm using it quite a lot for a Linux re-write of my Dialog Module extension:
main.cpp
Again, please give explanations of what your code does and how to use it. Allow me to start by explaining the code above. It evaluates shell scripts, while similar to my "famous" Execute Shell extension, not only can it execute from the shell, it can also read any output from it (not including errors, but that could easily be added with a small tweak). Compile it with the G++ GNU C++ Compiler, using the following in any Unix terminal:
Replace "/Path/To/EvaluateShell/" with the path to a folder containing the C++ code file and make sure that file is named "main.cpp". You will need an output folder in that same directory named "EvaluateShell.x64". Notice the x64, this will be for 64-bit builds and requires building on a 64-bit Unix OS (whether macOS, Linux, BSD, Solaris, or whatever).
If you want 32-bit support of this extension sample, the same rules apply. Install G++ on your OS as needed, cd to the correct path, and run the commands one line at a time on your terminal application. Create the same folder, but this time have it named "EvaluateShell.x86". Depending on your OS, you will need the proper file extension for the shared library. Linux as you can see is *.so, macOS is *.dylib, Windows is *.dll and so forth:
Notice in the above terminal commands snippet, this time I used the dylib extenson - "EvaluateShell.dylib" - this is good for compiling on macOS right out of the box as soon as you have G++ via the HomeBrew package manager. On Linux you can compile the code in the snippet previous to the one directly above and you can use the extension via GameMaker Studio 1.4, as the Linux runner is 32-bit in all versions of Studio prior to 2.x. Mac is 64-bit in both runners as of 1.49999 to Studio 2.x onward.
Should you compile for Studio 2 on Linux, you will need to use the -m64 flag instead of -m32, via modifying the terminal snippet, depending on which one of the two you copy/paste. Create an extension in Studio from the Extensions node of the resource tree in the IDE. Get the library file imported and create the function. Make sure the internal and external names of the function are "evaluate_shell". The return value must be a string. Add an argument and make it a string. The help line should be:
Now you are ready to test it. command is the terminal command or shell script contents you wish to execute and evaluate.
------------------------
Have fun and I hope this topic gets a lot of love.
Edit:
I discovered Windows does not have the getline() POSIX function, so to port this properly, I'll need to write my own. I'll share it here when I have a thoroughly tested and working version of that.
MOD EDIT:
Index:
Just post it!
Have questions about a code snippet? Do not clog or distract the goodness of this topic. PM the post's author, and hope they get back to you.
------------------------
I'll start.
For example, here's a snippet I wrote for a much larger project recently, but is useful for much more than being shared as an extension on its own; I'm using it quite a lot for a Linux re-write of my Dialog Module extension:
main.cpp
Code:
/*
MIT License
Copyright © 2019 Samuel Venable
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <cstdio>
#include <cstdlib>
#include <string>
using std::string;
#define EXPORTED_FUNCTION extern "C" __attribute__((visibility("default")))
EXPORTED_FUNCTION char *evaluate_shell(char *command) {
char *buffer = NULL;
size_t buffer_size = 0;
string str_command = command;
string str_buffer = "";
FILE *file = popen(str_command.c_str(), "r");
while (getline(&buffer, &buffer_size, file) != -1)
str_buffer += buffer;
free(buffer);
pclose(file);
if (str_buffer.back() == '\n')
str_buffer.pop_back();
static string str_result;
str_result = str_buffer;
return (char *)str_result.c_str();
}
Code:
cd /Path/To/EvaluateShell/
g++ -c -std=c++11 main.cpp -fPIC -m64
g++ main.o -o EvaluateShell.x64/EvaluateShell.so -shared -fPIC -m64
If you want 32-bit support of this extension sample, the same rules apply. Install G++ on your OS as needed, cd to the correct path, and run the commands one line at a time on your terminal application. Create the same folder, but this time have it named "EvaluateShell.x86". Depending on your OS, you will need the proper file extension for the shared library. Linux as you can see is *.so, macOS is *.dylib, Windows is *.dll and so forth:
Code:
cd /Path/To/EvaluateShell/
g++ -c -std=c++11 main.cpp -fPIC -m32
g++ main.o -o EvaluateShell.x86/EvaluateShell.dylib -shared -fPIC -m32
Should you compile for Studio 2 on Linux, you will need to use the -m64 flag instead of -m32, via modifying the terminal snippet, depending on which one of the two you copy/paste. Create an extension in Studio from the Extensions node of the resource tree in the IDE. Get the library file imported and create the function. Make sure the internal and external names of the function are "evaluate_shell". The return value must be a string. Add an argument and make it a string. The help line should be:
Code:
evaluate_shell(command)
------------------------
Have fun and I hope this topic gets a lot of love.
Edit:
I discovered Windows does not have the getline() POSIX function, so to port this properly, I'll need to write my own. I'll share it here when I have a thoroughly tested and working version of that.
MOD EDIT:
Index:
I actually thought this the other day, it really should be
I was going to request if it could be, also I thought an index on the first post would be really useful, like a list of each code post and description, I know it'd be work to keep up to date, but it'd make the thread a lot more helpful, cus it is hard to scroll through the whole thing and spot all the things that could be useful to you
I dont have time to do that, but the moderators and admins are welcome to add that to the OP If they like, maybe do as they feel lead and not on a schedule so it feels like less of a chore. (Not expecting them to have the time for that, either, tho)
Last edited by a moderator: