Difference between revisions of "Cycle Inventory Items (CMS)"

From The Official Visionaire Studio: Adventure Game Engine Wiki
Line 3: Line 3:
 
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By
 
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By
 
|-
 
|-
| Cycle Inventory Items || Definition || AFRLme
+
| Replace Item Function || Definition || AFRLme
 
|}
 
|}
  
This script allows you to cycle through the items in your inventory system.
+
This script allows you to replace one item with another while keeping the items position in the inventory. This script is especially useful for when you are replacing a manipulated item such as an empty bucket that has just been filled with water. By default Visionaire Studio would add the new item to the end of the inventory.
  
 
== Instructions ==
 
== Instructions ==
 
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/>
 
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/>
2. Edit the '''slots = 0''' value inside of the script to the amount of inventory slots you added to your interface.<br/>
+
2. To use this function you have to include both the names of the item you want to replace, & the item that that you will be replacing it with; item names are case & character sensitive.
3. By default the script will only allow you to cycle the inventory items when the amount of items in the inventory equals the same or greater than the amount of inventory slots. You can toggle between the default mode & always cycle by creating an ''execute a script'' action containing:
 
 
<syntaxhighlight>
 
<syntaxhighlight>
toggleCycleMode()
+
replaceItem("empty_bucket", "bucket_of_water")
</syntaxhighlight>
 
4. To cycle inventory items forward (ascending) you should create an ''execute a script'' action containing:
 
<syntaxhighlight>
 
cycleItems(true)
 
</syntaxhighlight>
 
5. To cycle inventory items backwards (descending) you should create an ''execute a script'' action containing:
 
<syntaxhighlight>
 
cycleItems(false)
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 27: Line 18:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
--[[
 
--[[
Cycle inventory items by ascending or descending order [v2] (12/02/2014)
+
Replace item function [v1] (31/05/2014)
 
Written by AFRLme [Lee Clarke]
 
Written by AFRLme [Lee Clarke]
 
-- + --
 
-- + --
Line 37: Line 28:
 
--]]
 
--]]
  
-- * local variables * --
+
-- * function for swapping 2 items while keeping the position of the current item * --
local char, items, item, amt -- empty variables
+
function replaceItem( init, repl )
local slots = 0 -- define inventory slot amount here
+
itm = {} -- creates an empty table
local cycle = false -- false = only cycle inventory when item amount equals same as or more than inventory slots. true = always cycle
+
itm["_temporary_"] = "" -- set the table as temporary (we don't want it to store any data in the save files)
 
+
  itm["gItm"] = game:getLinks(VGameItems) -- get all game items & store them in a table
-- * function for changing cycle mode * --
+
  itm["cItm"] = game:getLink(VGameCurrentCharacter):getLinks(VCharacterItems) -- get all character items & store them in a table
function toggleCycleMode()
+
  itm["rItm"] = nil
  if cycle then cycle = false else cycle = true end
+
-- + --
end
+
for i = 1, #(itm.gItm) do -- run a check to see if replacement item exists in game item table & if so, store item link in variable
 
+
   if itm.gItm[i]:getName() == repl then itm["rItm"] = getObject("Game.GameItems[" .. repl .. "]") break end
-- * function for cycling through the inventory items * --
+
end
function cycleItems(asc)
+
-- + --
  char = game:getLink(VGameCurrentCharacter) -- store current character
+
for j = 1, #(itm.cItm) do -- if initial item exists and rItm is not empty then replace init item with replacement item
items = char:getLinks(VCharacterItems) -- store inventory items into a table
+
   if itm.cItm[j]:getName() == init and itm.rItm ~= nil then itm.cItm[j] = itm.rItm break end
  amt = table.maxn(items) -- get index number of last item
 
if cycle or amt >= slots then
 
   if asc then item = items[amt]; table.remove(items, amt); table.insert(items, 1, item) else item = items[1]; table.remove(items, 1); table.insert(items, (table.maxn(items)+1), item) end
 
   char:setValue(VCharacterItems, items) -- update the inventory
 
 
  end
 
  end
 +
-- + --
 +
if itm.rItm ~= nil then game:getLink(VGameCurrentCharacter):setValue(VCharacterItems, itm.cItm) end -- update character items
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 18:25, 31 May 2014

Name Type By
Replace Item Function Definition AFRLme

This script allows you to replace one item with another while keeping the items position in the inventory. This script is especially useful for when you are replacing a manipulated item such as an empty bucket that has just been filled with water. By default Visionaire Studio would add the new item to the end of the inventory.

Instructions

1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2. To use this function you have to include both the names of the item you want to replace, & the item that that you will be replacing it with; item names are case & character sensitive.

replaceItem("empty_bucket", "bucket_of_water")

Main Script

--[[
Replace item function [v1] (31/05/2014)
Written by AFRLme [Lee Clarke]
-- + --
alternatingfrequencies@hotmail.com | skype @ AFRLme
-- + --
This script is donation optional. In game credit is non-negotiable.
You are free to: ¹ use it in your game(s). ² modify the script.
Do not remove - or edit - this comment block.
--]]

-- * function for swapping 2 items while keeping the position of the current item * --
function replaceItem( init, repl )
 itm = {} -- creates an empty table
 itm["_temporary_"] = "" -- set the table as temporary (we don't want it to store any data in the save files)
 itm["gItm"] = game:getLinks(VGameItems) -- get all game items & store them in a table
 itm["cItm"] = game:getLink(VGameCurrentCharacter):getLinks(VCharacterItems) -- get all character items & store them in a table
 itm["rItm"] = nil
 -- + --
 for i = 1, #(itm.gItm) do -- run a check to see if replacement item exists in game item table & if so, store item link in variable
  if itm.gItm[i]:getName() == repl then itm["rItm"] = getObject("Game.GameItems[" .. repl .. "]") break end
 end
 -- + --
 for j = 1, #(itm.cItm) do -- if initial item exists and rItm is not empty then replace init item with replacement item
  if itm.cItm[j]:getName() == init and itm.rItm ~= nil then itm.cItm[j] = itm.rItm break end
 end
 -- + --
 if itm.rItm ~= nil then game:getLink(VGameCurrentCharacter):setValue(VCharacterItems, itm.cItm) end -- update character items
end