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

Discussion in 'Mods & Lua Functions' started by Colandus, Sep 29, 2009.

  1. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    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:
    Code:
    --##############################--
    --##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:
    Code:
    --##############################--
    --##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:
    Code:
    dofile(getDataDir() .. "lib/settings.lua")
    dofile(getDataDir() .. "lib/setting_system.lua")
    

    That's it! Now how to use them ?

    Example, talkaction.
    Code:
    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: Dec 13, 2016
    Forrest likes this.
  2. Master-m

    Master-m Need help? Just ask!

    Joined:
    May 28, 2007
    Messages:
    4,343
    rox but no need to double post xd
     
  3. Sync

    Sync Ø,ø

    Joined:
    May 26, 2009
    Messages:
    1,868
    very nice ha
     
  4. Hazen

    Hazen Riariari dorgas mano

    Joined:
    Jun 27, 2009
    Messages:
    39
    Very nice[2]
    Rsrs i'll never use random storages now ;P
    Thanks.. '-'
     
  5. Mock

    Mock Mock the bear (MTB)

    Joined:
    Jul 29, 2008
    Messages:
    619
    Wow colandus great!
    it's the first time i see someone using bit lib in lua ;D
     
  6. babalow

    babalow Nobody o/

    Joined:
    Nov 7, 2007
    Messages:
    392
    really nice .. im gonna use it for sure ;x
     
  7. Synthetic_

    Synthetic_ deathzot.net

    Joined:
    Dec 30, 2008
    Messages:
    2,494
    Good stuff, as always Colandus :p
     
  8. slawkens

    slawkens Well-Known Member

    Joined:
    Aug 27, 2007
    Messages:
    2,215
    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
    
     
  9. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
  10. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    coolest.... bump
     
    zakius likes this.
  11. zakius

    zakius Enter the Ninja!

    Joined:
    Apr 30, 2009
    Messages:
    2,635
    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: Aug 30, 2010
  12. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    What does that mean ? :p
     
  13. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
  14. Scarlet Ayleid

    Scarlet Ayleid Well-Known Member

    Joined:
    Jul 7, 2007
    Messages:
    4,058
    using binary to store this kind of information, very nice, very nice indeed :)
     
  15. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    bump
     
    StreamSide likes this.

Share This Page