April 19, 2024, 09:01:46 am

The Gang Garrison 2 Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

NOTICE: Wondering where all the forums have gone?

Join the community Discord server!

Pages: [1]

Author Topic: How to make a plugin: the basics  (Read 6575 times)

Lorgan

  • Retired Randomizer Mod Developer
  • Resident Miku
  • *****
  • Karma: 28
  • Offline Offline
  • Posts: 3625
    • My own website
How to make a plugin: the basics
« 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):
  • ind is the name of the object you want to add the code to. This can be the name of an existing object or the name of an object you created with the plugin.

  • evtype is the name of the event where you want to add the code to. They're usually the regular name used in the game maker editor with the 'ev_' prefix. Examples are ev_create, ev_step, ev_draw, ev_alarm,...
    There is also ev_other for the less used events. You can see the lists of all event names below this paragraph.

  • evnumb is used to specify what event you want to modify exactly. This is unused most of the time so you can set it to 0 if it's unused. If you're editing an alarm event this number specifies which one and if you're editing a step event it specifies what sort of step event (begin step, regular or end step). This is also used if your event is ev_other

  • codestr is just whatever you want the selected object to do. It should be enclosed by (double)quotes.

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!
Logged
Unfortunately, turning a section into a communist oppressive regime is not against the forum rules, so there is really nothing we can do.
Quote from: steam
21:08 - Hullusorsa: lorgan, when will you buy us the keys?
21:09 - Lorgan: i'm waiting for greece to collapse so the value of the euro drops
21:09 - Lorgan: not even joking

notajf

  • Guest
Re: How to make a plugin: the basics
« Reply #1 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:
« Last Edit: June 02, 2012, 03:27:18 pm by ß »
Logged

notajf

  • Guest
Re: How to make a plugin: the basics
« Reply #2 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
Logged

notajf

  • Guest
Re: How to make a plugin: the basics
« Reply #3 on: June 02, 2012, 03:27:38 pm »

we could also use a quick walkthrough on how to install plugins and such.
Onto it.
Logged

notajf

  • Guest
Logged

cmb

  • Full Member
  • ***
  • Karma: 0
  • Offline Offline
  • Posts: 331
  • Mario Bros
Re: How to make a plugin: the basics
« Reply #5 on: February 20, 2013, 06:26:32 pm »

I need help with ev_keypress, how do I tell which key I am editing.

Lorgan

  • Retired Randomizer Mod Developer
  • Resident Miku
  • *****
  • Karma: 28
  • Offline Offline
  • Posts: 3625
    • My own website
Re: How to make a plugin: the basics
« Reply #6 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).
Logged
Unfortunately, turning a section into a communist oppressive regime is not against the forum rules, so there is really nothing we can do.
Quote from: steam
21:08 - Hullusorsa: lorgan, when will you buy us the keys?
21:09 - Lorgan: i'm waiting for greece to collapse so the value of the euro drops
21:09 - Lorgan: not even joking
Pages: [1]
 

Page created in 0.034 seconds with 50 queries.