[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,424
    Likes Received:
    163
    Best Answers:
    18
    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 (Lua):
    1.  
    2. --##############################--
    3. --##SETTING SYSTEM BY COLANDUS##--
    4. --##############################--
    5.  
    6. -- Settings storage value
    7. SETTINGS_STORAGE_VALUE = 32444
    8.  
    9. -- Put all settings here. Always put new settings in BOTTOM, or you will fuck it all up!
    10. _SETTINGS = {
    11. --    HAS_FINISHED_ANIHILATOR = 1,
    12. --    HAS_CLICKED_DOOR = 2,
    13. --    CAN_ACCESS_QUEST = 3
    14.     MY_TEST_SETTING = 1
    15. }
    16.  
    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 (Lua):
    1.  
    2. --##############################--
    3. --##SETTING SYSTEM BY COLANDUS##--
    4. --##############################--
    5.  
    6. -- Returns true/false if player has the setting
    7. function getPlayerSettingStatus(cid, settingId)
    8.     -- Make sure player exist
    9.     if(not isPlayer(cid)) then
    10.         return false
    11.     end
    12.  
    13.     -- Make settingId to binary
    14.     settingId = settingId ^ 2
    15.     -- Retrieve players current settings
    16.     local settings = getPlayerStorageValue(cid, SETTINGS_STORAGE_VALUE)
    17.     -- Return and check whether player has this setting enabled
    18.     return bit.band(settings, settingId) == settingId
    19. end
    20.  
    21. -- Set a players setting status; "status" should be true/false
    22. function setPlayerSettingStatus(cid, settingId, status)
    23.     -- Make sure player exist
    24.     if(not isPlayer(cid)) then
    25.         return false
    26.     end
    27.    
    28.     -- Set only if he don't currently have this status already
    29.     if(getPlayerSettingStatus(cid, settingId) ~= status) then
    30.         -- Toggle setting status
    31.         doPlayerToggleSettingStatus(cid, settingId)
    32.         -- Success, return true
    33.         return true
    34.     end
    35.    
    36.     -- Everything was done without errors
    37.     return false
    38. end
    39.  
    40. -- Toggle a players setting status.
    41. function doPlayerToggleSettingStatus(cid, settingId)
    42.     -- Make sure player exist
    43.     if(not isPlayer(cid)) then
    44.         return false
    45.     end
    46.    
    47.     -- Make settingId to binary
    48.     settingId = settingId ^ 2
    49.    
    50.     -- Retrieve players current settings
    51.     local settings = getPlayerStorageValue(cid, SETTINGS_STORAGE_VALUE)
    52.     -- Reset settings if -1
    53.     if(settings < 0) then
    54.         settings = 0
    55.     end
    56.    
    57.     -- Toggle setting status
    58.     settings = bit.bxor(settings, settingId)
    59.     -- Save settings
    60.     setPlayerStorageValue(cid, SETTINGS_STORAGE_VALUE, settings)
    61.    
    62.     -- Everything was done without errors
    63.     return true
    64. end
    65.  
    Now open data.lua and add the following lines:
    Code (Lua):
    1.  
    2. dofile(getDataDir() .. "lib/settings.lua")
    3. dofile(getDataDir() .. "lib/setting_system.lua")
    4.  

    That's it! Now how to use them ?

    Example, talkaction.
    Code (Lua):
    1.  
    2. function onSay(cid)
    3.     doPlayerToggleSettingStatus(cid, MY_TEST_SETTING)
    4.     doCreatureSay(cid, "Setting is: " .. getPlayerSettingStatus(cid, MY_TEST_SETTING and "ON" or "OFF", 1)
    5.  
    6.     return TRUE
    7. end
    8.  
    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,338
    Likes Received:
    15
    Best Answers:
    0
    rox but no need to double post xd
     
  3. Sync

    Sync Ø,ø

    Joined:
    May 26, 2009
    Messages:
    1,869
    Likes Received:
    13
    Best Answers:
    0
    very nice ha
     
  4. Hazen

    Hazen Riariari dorgas mano

    Joined:
    Jun 27, 2009
    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    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
    Likes Received:
    94
    Best Answers:
    0
    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
    Likes Received:
    0
    Best Answers:
    0
    really nice .. im gonna use it for sure ;x
     
  7. Synthetic_

    Synthetic_ deathzot.net

    Joined:
    Dec 30, 2008
    Messages:
    2,494
    Likes Received:
    443
    Best Answers:
    0
    Good stuff, as always Colandus :p
     
  8. slawkens

    slawkens Well-Known Member

    Joined:
    Aug 27, 2007
    Messages:
    2,437
    Likes Received:
    160
    Best Answers:
    11
    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 (Text):
    1.  
    2. local myOptionsToSave = {
    3.     MISSION_FIRST_STATE = 1,
    4.     MISSION_SECOND_STATE = 2,
    5.     MISSION_TAKE_SHOVEL = 3
    6. }
    7.  
    8. local settings = Settings:new(123456)
    9. settings:set(MISSION_FIRST_STATE, 0)
    10. settings:set(MISSION_TAKE_SHOVEL, 1)
    11.  
    12. if(settings:get(MISSION_TAKE_SHOVEL)) -- have takken shovel already ;d
    13.     do_something()
    14. end
    15.  
     
  9. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    163
    Best Answers:
    18
  10. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    163
    Best Answers:
    18
    coolest.... bump
     
    zakius likes this.
  11. zakius

    zakius Enter the Ninja!

    Joined:
    Apr 30, 2009
    Messages:
    2,635
    Likes Received:
    63
    Best Answers:
    0
    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,424
    Likes Received:
    163
    Best Answers:
    18
    What does that mean ? :p
     
  13. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    163
    Best Answers:
    18
  14. Scarlet Ayleid

    Scarlet Ayleid Well-Known Member

    Joined:
    Jul 7, 2007
    Messages:
    4,057
    Likes Received:
    240
    Best Answers:
    0
    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,424
    Likes Received:
    163
    Best Answers:
    18
    bump
     
    StreamSide likes this.

Share This Page

Loading...