This will be a very simple and well-described tutorial on using the storage system in OpenTibia
Descriptive Introduction
What does storage mean?
It is the control of retrievable data on a computer or other electronic system; in other words, memory.
In OpenTibia, we use storage to hold numeric values (StorageValues), which we would later use to control things in-game.
When it comes to managing storage, we will mostly worry about two things: StorageID and StorageValues.
What are these two things?
There are two different kinds of storage:
Player storage - a StorageValue can be stored in a StorageID that is specific to a player
Global storage - a StorageValue can be stored in a StorageID that is used globally in the game (pretty much applies to every player)
How do we manage storage in Lua?
To get the storage value:
These functions return the StorageValue of the specified StorageID
To set the storage value:
These functions store the StorageValue in StorageID
Examples
Scenario 1: Let's say we want to give a player a storage with an ID 4200 and give it a storage value of 3.
Initial: The player has nothing (default storage value: -1)
We set storageID 4200 to have storage value of 3:
Final: The player now has a storage with an ID 4200 and it has a storage value of 3
Scenario 2: Let's have a player click on a statue and have it print "Bazinga!" IF AND ONLY IF the player's storage value in 5400 is -1 or less, else print "Aww shucks!". If the statue says "Bazinga!", we set the player's storage value in 5400 to 1.
The understanding to this scenario is that we want the player to use something only once.
In order to do that, we will use a storageID to hold a value that will help us determine if a player has used the statue or not.
If the storage value is -1 or less, the statue will say "Bazinga!", else it will say "Aww shucks!".
Initial: The player has nothing (default storage value: -1)
When we use the statue, we want to know which message to print ("Bazinga!" or "Aww shucks!").
We said if the storage value of 5400 is -1 or less, then print "Bazinga!", else print "Aww shucks!".
In order to check if a player has storage value of -1 or less in storage 5400, we will do something like this:
Then if it is true, we will print "Bazinga!" and then give the player a storage value of 1 in storage 5400.
If the player has a storage value in 5400 that is anything other than 0, we need to print "Aww shucks!", we will use this:
This happened because when we used the statue with storage value 0 in storage 5400, it will print "Bazinga!" and change the storage value in 5400 to 1. Since 1 does not equal any number less than 0, it fails the initial condition and moves to the else, which then would print "Aww shucks!".
Scenario 3: Let's create something where a player can only do something once per day.
Something like this would be ideal for tasks where you want players to complete only once per day (like the Rottin Wood quest).
It's fairly simple to do with storage and storage values. We just need the quest to check if it has been 24 hours since the last use.
And we will be doing that using storage values and the operating system time!
Things to know:
Here is the full Lua script for something like this scenario:
The variable dayStorage will hold the storage value of storage 7300.
The variable secondsInADay will hold the value 86400, which is the number of seconds in a day
os.time() returns the number of seconds since 00:00:00 UTC, January 1, 1970. This will help use keep track of time.
Initial: The player has nothing in storage 7300
When you use the script, it will check if os.time() is greater than dayStorage + secondsInADay.
Therefore we will -- do something and set the player's storage 7300 value to os.time().
Now, let's say the player uses this script again after 12 hours (which is before 1 day has passed).
When you use the script, it will check if os.time() is greater than dayStorage + secondsInADay.
Therefore it will skip the initial condition and move to the else will -- print "You must wait 24 hours before doing it again"
It will continue to do this until os.time() is greater than the storage value in storage 7300.
I think that is all that I should share.
If you have any questions, comments, concerns, or spot any mistakes in this thread (hope not), PLEASE POST!
Descriptive Introduction
What does storage mean?
It is the control of retrievable data on a computer or other electronic system; in other words, memory.
In OpenTibia, we use storage to hold numeric values (StorageValues), which we would later use to control things in-game.
When it comes to managing storage, we will mostly worry about two things: StorageID and StorageValues.
What are these two things?
- StorageID is a space in memory that holds a numeric value (Storage Value). It is named with a pre-defined ID number.
- StorageValue is a number that will be stored in a space in memory (StorageID)
There are two different kinds of storage:
Player storage - a StorageValue can be stored in a StorageID that is specific to a player
Global storage - a StorageValue can be stored in a StorageID that is used globally in the game (pretty much applies to every player)
How do we manage storage in Lua?
To get the storage value:
These functions return the StorageValue of the specified StorageID
To set the storage value:
These functions store the StorageValue in StorageID
Examples
Scenario 1: Let's say we want to give a player a storage with an ID 4200 and give it a storage value of 3.
Initial: The player has nothing (default storage value: -1)
We set storageID 4200 to have storage value of 3:
Final: The player now has a storage with an ID 4200 and it has a storage value of 3
Scenario 2: Let's have a player click on a statue and have it print "Bazinga!" IF AND ONLY IF the player's storage value in 5400 is -1 or less, else print "Aww shucks!". If the statue says "Bazinga!", we set the player's storage value in 5400 to 1.
The understanding to this scenario is that we want the player to use something only once.
In order to do that, we will use a storageID to hold a value that will help us determine if a player has used the statue or not.
If the storage value is -1 or less, the statue will say "Bazinga!", else it will say "Aww shucks!".
Initial: The player has nothing (default storage value: -1)
When we use the statue, we want to know which message to print ("Bazinga!" or "Aww shucks!").
We said if the storage value of 5400 is -1 or less, then print "Bazinga!", else print "Aww shucks!".
In order to check if a player has storage value of -1 or less in storage 5400, we will do something like this:
Then if it is true, we will print "Bazinga!" and then give the player a storage value of 1 in storage 5400.
Lua:
if (getCreatureStorage(cid, 5400) < 0) then
-- print "Bazinga!"
doCreatureSetStorage(cid, 5400, 1)
If the player has a storage value in 5400 that is anything other than 0, we need to print "Aww shucks!", we will use this:
Lua:
if (getCreatureStorage(cid, 5400) < 0) then
-- print "Bazinga!"
doCreatureSetStorage(cid, 5400, 1)
else
-- print "Aww shucks!"
end
This happened because when we used the statue with storage value 0 in storage 5400, it will print "Bazinga!" and change the storage value in 5400 to 1. Since 1 does not equal any number less than 0, it fails the initial condition and moves to the else, which then would print "Aww shucks!".
Scenario 3: Let's create something where a player can only do something once per day.
Something like this would be ideal for tasks where you want players to complete only once per day (like the Rottin Wood quest).
It's fairly simple to do with storage and storage values. We just need the quest to check if it has been 24 hours since the last use.
And we will be doing that using storage values and the operating system time!
Things to know:
- There are 60 seconds in a minute
- There are 3600 seconds in an hour
- There are 86400 seconds in a day
Here is the full Lua script for something like this scenario:
Lua:
local dayStorage = getCreatureStorage(cid, 7300)
local secondsInADay = 86400
if (os.time() > dayStorage + secondsInADay) then
-- do something
doCreatureSetStorage(cid, 7300, os.time())
else
-- print "You must wait 24 hours before doing it again"
end
The variable secondsInADay will hold the value 86400, which is the number of seconds in a day
os.time() returns the number of seconds since 00:00:00 UTC, January 1, 1970. This will help use keep track of time.
Initial: The player has nothing in storage 7300
When you use the script, it will check if os.time() is greater than dayStorage + secondsInADay.
- Current os.time() in this moment is: 1368219623.
- dayStorage is the storage value in 7300, which is 0.
- secondsInADay is defined as the number of seconds in a day, which is 86400.
Therefore we will -- do something and set the player's storage 7300 value to os.time().
Now, let's say the player uses this script again after 12 hours (which is before 1 day has passed).
When you use the script, it will check if os.time() is greater than dayStorage + secondsInADay.
- The os.time() after 12 hours is: 1368262823.
- dayStorage is the storage value in 7300, which is 1368219623.
- secondsInADay is defined as the number of seconds in a day, which is 86400.
Therefore it will skip the initial condition and move to the else will -- print "You must wait 24 hours before doing it again"
It will continue to do this until os.time() is greater than the storage value in storage 7300.
I think that is all that I should share.
If you have any questions, comments, concerns, or spot any mistakes in this thread (hope not), PLEASE POST!
Last edited: