• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[CHECK!] get/set/toggle player setting status USE ONLY ONE STORAGE VALUE

Colandus

Advanced OT User
Senator
Joined
Jun 6, 2007
Messages
2,434
Solutions
19
Reaction score
219
Location
Sweden
Hey! This great system will allow you to save "settings" into a players storage value using ONLY 1 STORAGE VALUE! However this will only store boolean values (true/false).

So, if you use storage value 82000 for your settings, then you will be able to store an infinite amount of settings in just that single storage value!

For example, if you want to check if a player has clicked on a door before, or whatever! :p Couldn't figure any example hehe :D

However, here goes the code! :D

First, go to data/lib folder, create new file called "settings.lua", in it write:
Lua:
--##############################--
--##SETTING SYSTEM BY COLANDUS##--
--##############################--

-- Settings storage value
SETTINGS_STORAGE_VALUE = 32444

-- Put all settings here. Always put new settings in BOTTOM, or you will fuck it all up!
_SETTINGS = {
--    HAS_FINISHED_ANIHILATOR = 1,
--    HAS_CLICKED_DOOR = 2,
--    CAN_ACCESS_QUEST = 3
    MY_TEST_SETTING = 1
}

In there you will place yours! Those were only examples!

Now create another new file, in same folder, called "setting_system.lua", in it paste:
Lua:
--##############################--
--##SETTING SYSTEM BY COLANDUS##--
--##############################--

-- Returns true/false if player has the setting
function getPlayerSettingStatus(cid, settingId)
    -- Make sure player exist
    if(not isPlayer(cid)) then
        return false
    end

    -- Make settingId to binary
    settingId = settingId ^ 2
    -- Retrieve players current settings
    local settings = getPlayerStorageValue(cid, SETTINGS_STORAGE_VALUE)
    -- Return and check whether player has this setting enabled
    return bit.band(settings, settingId) == settingId
end

-- Set a players setting status; "status" should be true/false
function setPlayerSettingStatus(cid, settingId, status)
    -- Make sure player exist
    if(not isPlayer(cid)) then
        return false
    end
   
    -- Set only if he don't currently have this status already
    if(getPlayerSettingStatus(cid, settingId) ~= status) then
        -- Toggle setting status
        doPlayerToggleSettingStatus(cid, settingId)
        -- Success, return true
        return true
    end
   
    -- Everything was done without errors
    return false
end

-- Toggle a players setting status.
function doPlayerToggleSettingStatus(cid, settingId)
    -- Make sure player exist
    if(not isPlayer(cid)) then
        return false
    end
   
    -- Make settingId to binary
    settingId = settingId ^ 2
   
    -- Retrieve players current settings
    local settings = getPlayerStorageValue(cid, SETTINGS_STORAGE_VALUE)
    -- Reset settings if -1
    if(settings < 0) then
        settings = 0
    end
   
    -- Toggle setting status
    settings = bit.bxor(settings, settingId)
    -- Save settings
    setPlayerStorageValue(cid, SETTINGS_STORAGE_VALUE, settings)
   
    -- Everything was done without errors
    return true
end

Now open data.lua and add the following lines:
Lua:
dofile(getDataDir() .. "lib/settings.lua")
dofile(getDataDir() .. "lib/setting_system.lua")


That's it! Now how to use them ?

Example, talkaction.
Lua:
function onSay(cid)
    doPlayerToggleSettingStatus(cid, MY_TEST_SETTING)
    doCreatureSay(cid, "Setting is: " .. getPlayerSettingStatus(cid, MY_TEST_SETTING and "ON" or "OFF", 1)

    return TRUE
end

This will turn on/off each time you use it. Because it will always toggle. But you can use only setPlayerSettingStatus(cid, MY_TEST_SETTING, true) and it will be on, and you write false to turn it off.

Hope you understood!


Regards,
Colandus
 
Last edited:
Very nice[2]
Rsrs i'll never use random storages now ;P
Thanks.. '-'
 
Wow colandus great!
it's the first time i see someone using bit lib in lua ;D
 
Its great system but what could be done to be it was more flexible: so you could use it for example in different things. I know now its limited but how much settings can be saved in 1 storage?

My example
Code:
local myOptionsToSave = {
	MISSION_FIRST_STATE = 1,
	MISSION_SECOND_STATE = 2,
	MISSION_TAKE_SHOVEL = 3
}

local settings = Settings:new(123456)
settings:set(MISSION_FIRST_STATE, 0)
settings:set(MISSION_TAKE_SHOVEL, 1)

if(settings:get(MISSION_TAKE_SHOVEL)) -- have takken shovel already ;d
	do_something()
end
 
epic, I guess that over 90% of storages are boolean, maybe you could also code patch for quest log for this?
edit: missing space
I mean if you could do some patch making it useable with questlog, now it bases on numeric storages only, so if I'd like to use this system for all boolean storages I'll lose questlog(umm, yeah, I don't have questlog yet but you should get what I mean)
 
Last edited:
Back
Top