1
Modules & Plugins / [MODULE] Crafting 0.1 - Craft items in a second inventory window
« on: 29 Aug 2012, 17:58 »
This module handles moving multiple inventory items to a secondary inventory window and combining them into a new item.
In order to use it:
1. Get the module here: DOWNLOAD (DEMO VIDEO)
2. Import it into AGS by right clicking the Script node in the Project tree, selecting "Import script..." and choosing the file.
3. Add an InvWindow to a GUI in a way that the player can click it with an active InventoryItem as mouse cursor, then in its properties, name it something sensible like "InvCrafting". The easiest way to accomplish this is by adding it to the existing inventory GUI (gInventory).
Now add a dummy character to the game (or use an existing NPC) and set its ID as the InvWindow's CharacterID. (Don't use the player character!)
4. Add two buttons to a GUI. One is supposed to try and combine the selected items, the other is supposed to clear the selection. The buttons should have proper names, choose something like "btnCraft" and "btnCraftClear" or similar.
5. Create the click handler functions for the two buttons, either by double-clicking them or via their event pane.
6. In Globalscript.asc, find the two handler functions and link them to the module so the whole thing looks similar to this:
The latter one can of course contain additional code, for instance if the player is currently unable to craft items, the game could provide a message instead. Calling Crafting.Craft(); attempts to craft an item in the pool, regardless of other circumstances.
The following steps are all about adding commands to the game_start() function in GlobalScript.asc.
7. Some more preparation for the module is necessary; tell the module the names of the InvWindow, Craft and Clear Button:
(The buttons are disabled automatically if the crafting window is empty.)
8. Optionally, you can
a) allow the selection of multiple instances of the same item. Say building a box takes five pieces of wood, among other things. In order to not confuse the player, you should activate the following option in that case: General settings -> Inventory -> Display multiple icons for multiple items
b) change the default messages.
They are "You successfully craft a %s." and "You fail to craft anything."
Just call
9. Now the most important part: adding item recipes.
Start by adding the resulting InventoryItem:
The function returns an index number that's used by the module to identify items. I've chosen this route because there might be multiple recipes producing the same item. (Store the returned integer in a global variable in order to be able to change or deactivate the recipe later in the game.*)
Using the value returned by the function, you can now add parts. Optionally, state the number of required parts, if it is greater than one:
You can use that function to change the recipe later in the game, just call it with a different number as third parameter or 0 to remove the ingredient from the list of requirements altogether.
*To remove an item from the pool of possible items, use this function:
This will effectively remove the possibility to create the box from the game. You can of course also use this to change the resulting item; simply pass it to the function as second parameter.
Note: The player can move single items back to the main inventory by right-clicking them.
You can also check the requirement for an item in-game. If debug mode is activated, type !, then enter the number of the item. (The module's number, starting at 0, not AGS's inventory ID!)
ProTip: If you have lots of recipes, your game_start might get crowded. To avoid this:I'd be grateful if people tested this and provided some feedback. This is pretty much a beta-version, so use at your own risk 
The module can be altered freely, credit is welcome but not required.
Also, here's a short video demonstrating the module: http://www.youtube.com/watch?v=K4-ScC_nyhg
In order to use it:
1. Get the module here: DOWNLOAD (DEMO VIDEO)
2. Import it into AGS by right clicking the Script node in the Project tree, selecting "Import script..." and choosing the file.
3. Add an InvWindow to a GUI in a way that the player can click it with an active InventoryItem as mouse cursor, then in its properties, name it something sensible like "InvCrafting". The easiest way to accomplish this is by adding it to the existing inventory GUI (gInventory).
Now add a dummy character to the game (or use an existing NPC) and set its ID as the InvWindow's CharacterID. (Don't use the player character!)
4. Add two buttons to a GUI. One is supposed to try and combine the selected items, the other is supposed to clear the selection. The buttons should have proper names, choose something like "btnCraft" and "btnCraftClear" or similar.
5. Create the click handler functions for the two buttons, either by double-clicking them or via their event pane.
6. In Globalscript.asc, find the two handler functions and link them to the module so the whole thing looks similar to this:
Code: Adventure Game Studio
- function btnCraftClear_OnClick(GUIControl *control, MouseButton button) {
- if (button == eMouseLeft) Crafting.Clear(false); // <- false is crucial here
- }
- function btnCraft_OnClick(GUIControl *control, MouseButton button) {
- if (button == eMouseLeft) Crafting.Craft();
- }
The following steps are all about adding commands to the game_start() function in GlobalScript.asc.
7. Some more preparation for the module is necessary; tell the module the names of the InvWindow, Craft and Clear Button:
Code: Adventure Game Studio
- Crafting.SetGUIElements(InvCrafting, btnCraft, btnCraftClear);
8. Optionally, you can
a) allow the selection of multiple instances of the same item. Say building a box takes five pieces of wood, among other things. In order to not confuse the player, you should activate the following option in that case: General settings -> Inventory -> Display multiple icons for multiple items
Code: Adventure Game Studio
- Crafting.SetMultipleAllowed(true);
They are "You successfully craft a %s." and "You fail to craft anything."
Just call
Code: Adventure Game Studio
- Crafting.SetMessages("success message here, include item name using %s", "failure message here");
9. Now the most important part: adding item recipes.
Start by adding the resulting InventoryItem:
Code: Adventure Game Studio
- int box = Crafting.AddItem(iBox);
Using the value returned by the function, you can now add parts. Optionally, state the number of required parts, if it is greater than one:
Code: Adventure Game Studio
- Crafting.AddPart(box, iGlue);
- Crafting.AddPart(box, iWood, 5);
- Crafting.AddPart(box, iHandle);
*To remove an item from the pool of possible items, use this function:
Code: Adventure Game Studio
- Crafting.SetItem(box, null);
Note: The player can move single items back to the main inventory by right-clicking them.
You can also check the requirement for an item in-game. If debug mode is activated, type !, then enter the number of the item. (The module's number, starting at 0, not AGS's inventory ID!)
ProTip: If you have lots of recipes, your game_start might get crowded. To avoid this:

The module can be altered freely, credit is welcome but not required.
Also, here's a short video demonstrating the module: http://www.youtube.com/watch?v=K4-ScC_nyhg


)




