Insert Item (CMS)

From The Official Visionaire Studio: Adventure Game Engine Wiki
Name Type By
Insert Item Function Definition AFRLme

This script allows you to insert & place a new item into the inventory, before or after another item.


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.
2a. Example for placing new item before the initial item...

insertItem("nail", "hammer" false) -- place item "nail" before the hammer

2b. Example for placing new item after the initial item...

insertItem("nail", "hammer", true) -- place item "nail" after the hammer

Main Script

Insert item function [v2] (31/05/2014)
Written by AFRLme [Lee Clarke]
-- + --
-- + --
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.

-- * tables * --
local 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)

-- * function for inserting an item before or after another item * --
function insertItem( new, init, sta )
 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["nItm"] = nil -- default value of new item
 itm["val"] = 1 -- default value of item slot
 -- + --
 for i = 1, #(itm.gItm) do -- run a check to see if new item exists in game item table & if so, store item link in variable
  if itm.gItm[i]:getName() == new then itm.nItm = getObject("Game.GameItems[" .. new .. "]") break end
 -- + --
 for j = 1, #(itm.cItm) do -- if initial item exists and nItm is not empty then add new item before or after initial item
  if itm.cItm[j]:getName() == init and itm.nItm ~= nil then
   if sta then itm.val = (j + 1) else itm.val = j end -- if true then add after else add before initial item
   table.insert(itm.cItm, itm["val"], itm.nItm) -- insert new item into table
  break -- kill the for loop
 if itm.nItm ~= nil then game:getLink(VGameCurrentCharacter):setValue(VCharacterItems, itm.cItm) end -- update character items