The Gang Garrison 2 Forum

Gang Garrison Development => Game Modifications => Add-Ons and Plugins => Topic started by: Lorgan on May 30, 2012, 02:23:11 pm

Title: How to make a plugin: the basics
Post by: Lorgan on May 30, 2012, 02:23:11 pm
If you want your plugin to not only change 1 variable and then call it a day, you probably want to add objects and/or insert extra code in already existing objects. Here I'll explain how to do that. Keep in mind that making a plugin is harder than just modding the game and thus it's recommended that you have some experience modding gg2 first.

The function that pretty much all plugins use is object_event_add(ind, evtype, evnumb, codestr):
List of all events (spoilered):
(click to show/hide)

(click to show/hide)

(click to show/hide)

This function only adds code and doesn't overwrite any old code that might be in that event already. If you need to change old code then you have to clear the old code in that event first and then re-add all code in that event. The function used to do this is object_event_clear(ind, evtype, evnumb). Keep in mind that this breaks any other plugins that happen to edit the same event.

To add a new object you have to use object_add(). It returns the id of the new object so you need to add the name of the object first (object_name = object_add()). If you plan on using the new object anywhere outside the object you've created it in you need to use global. This is basically always so it's best to always make it a global object.

These are all the tools you get to make your plugins. This means that you can't edit scripts. It's possible to clear and change the object that executes the script but this often gives errors or bugs so it's usually not worth doing. Try thinking of a better way or consider modding the game instead of making it a plugin.

Making the plugin is best done in game maker or another code editor. Then when you're ready to test it you have to make a new text document, paste your code in it and save it as a .gml file and you're done!
Title: Re: How to make a plugin: the basics
Post by: notajf on May 31, 2012, 04:47:04 am
OK, time for some menus!

For 2.4.3, Medo and me overhauled the system used to construct menus in GG2. I replaced the existing copy-pasted and inconsistent code using user-defined events that was a pain to manage, with a new menu API and common Menu code. Medo built upon the work I did, making several improvements.

This new system allows you to programmatically (with functions) add menu items.

Unlike the old menu system, this menu system is very developer friendly, and should make it much easier for plugin authors to add menus, and menu items.

Here's as a sample "gib everything" plugin:

Code: [Select]
object_event_add(InGameMenuController, ev_create, 0, '
    menu_addlink("Gib all", "
        with (Character) {
            hp = 0;
            lastDamageSource = Rocket;
        }
    ");
');

First, notice object_event_add. This adds to the create event of the in-game menu. We can't just use menu functions directly against objects, we need to use them on instances, so we're adding an event.

To object_event_add, we pass a script. This script calls menu_addlink. menu_addlink is a menu function to add a "link", an item with just a name and an action (doesn't take text/number input). To this we pass the name ("Gib all"), and a script. This script, well, gibs everyone.

Now what if we want to add a text field? It's quite simple. Here's an example from the host options menu:
Code: [Select]
menu_addedit_text("Password:", "global.serverPassword", '
    gg2_write_ini("Server", "Password", argument0);
');
(this would have to be put in an event like before)

menu_addedit_text adds an editable text field to a menu. "Password:" is the title of the field, the bit on the left. The next bit is the name of the variable it will change. In this case, global.serverPassword.

Now we have a script. This script is passed in argument0, the new value of the edit box. We use gg2_write_ini (a new convenience function) to change the server password in the INI.

The procedure for a numeric field is very similar.

I hope you find this useful. Go out there and be awesome with awesome plugins :z6:
Title: Re: How to make a plugin: the basics
Post by: notajf on May 31, 2012, 04:52:50 am
Also, btw, GM's code editor has a save button to save a GML file. No need to copy and paste :P
Title: Re: How to make a plugin: the basics
Post by: notajf on June 02, 2012, 03:27:38 pm
we could also use a quick walkthrough on how to install plugins and such.
Onto it.
Title: Re: How to make a plugin: the basics
Post by: notajf on June 02, 2012, 03:32:33 pm
man why arent you a moderator
http://www.ganggarrison.com/forums/index.php?topic=32100.0 (http://www.ganggarrison.com/forums/index.php?topic=32100.0)
Title: Re: How to make a plugin: the basics
Post by: cmb on February 20, 2013, 06:26:32 pm
I need help with ev_keypress, how do I tell which key I am editing.
Title: Re: How to make a plugin: the basics
Post by: Lorgan on February 21, 2013, 07:19:00 am
Use the evnumb parameter:
object_event_add(object,ev_keypress,ord("V"),"show_message('v was pressed')");
object_event_add(object,ev_keypress,vk_left,"show_message('left arrow was pressed')");

Make sure to use 'V' and not 'v' with the ord function. You can find more details in game maker help (press F1 in gm).