Difference between revisions of "Replace Item (CMS)"
From The Official Visionaire Studio: Adventure Game Engine Wiki
(Created page with "{| class="wikitable" style="width:100%" |- ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By |- | Replace Item Function || Def...") |
m |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | {| class=" | + | {| class="ts" style="width:100%" |
|- | |- | ||
− | ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | | + | ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | |
|- | |- | ||
− | | Replace Item Function || Definition || AFRLme | + | | Replace Item Function || Definition || [https://www.patreon.com/AFRLme 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. | + | 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. This script requires Visionaire Studio 5.x plus. |
== 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. 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. | 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. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
replaceItem("empty_bucket", "bucket_of_water") | replaceItem("empty_bucket", "bucket_of_water") | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Main Script == | == Main Script == | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
--[[ | --[[ | ||
− | Replace item function [ | + | Replace item function [v2] (15/09/2022) |
Written by AFRLme [Lee Clarke] | Written by AFRLme [Lee Clarke] | ||
-- + -- | -- + -- | ||
− | + | email: afrlme@outlook.com | |
+ | paypal: afrlme@zoho.com- | ||
+ | ko-fi: https://ko-fi.com/afrlme | ||
+ | patreon: https://www.patreon.com/AFRLme | ||
+ | portfolio: https://afrl.me | ||
-- + -- | -- + -- | ||
This script is donation optional. In game credit is non-negotiable. | This script is donation optional. In game credit is non-negotiable. | ||
Line 34: | Line 38: | ||
-- * function for swapping 2 items while keeping the position of the current item * -- | -- * function for swapping 2 items while keeping the position of the current item * -- | ||
function replaceItem( init, repl ) | function replaceItem( init, repl ) | ||
− | itm["gItm"] = game | + | itm["gItm"] = game.Items -- get all game items & store them in a table |
− | itm["cItm"] = game | + | itm["cItm"] = game.CurrentCharacter.Items -- get all character items & store them in a table |
itm["rItm"] = nil | 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 | 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"] = | + | if itm.gItm[i]:getName() == repl then itm["rItm"] = game.Items[repl]; break end |
end | end | ||
-- + -- | -- + -- | ||
for j = 1, #(itm.cItm) do -- if initial item exists and rItm is not empty then replace init item with replacement item | 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 | + | if itm.cItm[j]:getName() == init and itm.rItm ~= nil then itm.cItm[j] = itm.rItm; break end |
end | end | ||
-- + -- | -- + -- | ||
− | if itm.rItm ~= nil then game | + | if itm.rItm ~= nil then game.CurrentCharacter.Items = itm.cItm end -- update character items |
end | end | ||
− | </syntaxhighlight> | + | </syntaxhighlight>{{toc}} |
Latest revision as of 13:17, 17 February 2023
Name | Type | |
---|---|---|
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. This script requires Visionaire Studio 5.x plus.
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 [v2] (15/09/2022)
Written by AFRLme [Lee Clarke]
-- + --
email: afrlme@outlook.com
paypal: afrlme@zoho.com-
ko-fi: https://ko-fi.com/afrlme
patreon: https://www.patreon.com/AFRLme
portfolio: https://afrl.me
-- + --
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 swapping 2 items while keeping the position of the current item * --
function replaceItem( init, repl )
itm["gItm"] = game.Items -- get all game items & store them in a table
itm["cItm"] = game.CurrentCharacter.Items -- 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"] = game.Items[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.CurrentCharacter.Items = itm.cItm end -- update character items
end