• Print

Author Topic: How to make sure a table doesn't reload itself?  (Read 4464 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
How to make sure a table doesn't reload itself?
« on: April 14, 2017, 07:44:49 am »
So an addon that I'm writing right now uses tables to store channel names, and I have a command that will add another channel to that, but I want to make sure that on a map change or server restart that the channel(s) added aren't removed.


This is what I have in place to make sure of that:
Code: Lua
  1. SVC = SVC or {}
  2.  
  3.  
  4. SVC.VoiceChannels = SVC.VoiceChannels or { -- The [ key ] is what is connected to, and the "Value" is the name of the actual group.
  5.    [ "default" ] = { name="Default", access="user" }, -- Do not change the key in this one, only the value.
  6.    [ "dead" ] = { name="Dead", access="user"}, -- Do not change the key in this one, only the value.
  7.    [ "english" ] = { name="English", access="user" },
  8.    [ "spanish" ] = { name="EspaƱol", access="user" },
  9.    [ "admin" ] = { name="Admins", access="operator" }
  10. }

Then, how I add them:
Code: Lua
  1. function SVC.AddChannel( calling_ply, channelName, accessKey, opAccess )
  2.         SVC.VoiceChannels[accessKey] = { name=channelName, access=opAccess }
  3. end
I have it set to itself or a table, will that work?
« Last Edit: April 14, 2017, 08:07:29 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: How to make sure a table doesn't reload itself?
« Reply #1 on: April 14, 2017, 02:35:40 pm »
data persistence like this through server sessions (map change/restart) need to be written to a file and loaded.

You can encode the table as a json blob and write it to a data file and then when the map/server loads you can read and decode the json back into a table.

This is literally the only way to do it.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: How to make sure a table doesn't reload itself?
« Reply #2 on: April 16, 2017, 05:22:15 am »
I thought so but I wanted to see if there was a lazy way. Ok then
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: How to make sure a table doesn't reload itself?
« Reply #3 on: April 16, 2017, 10:41:30 am »
Fun story related to this.. apparently a newer version of lua does in fact have state persistence which would do this, but gmod doesn't use it and I doubt they'll ever update to it.. :D But I suppose it IS possible in lua.. just not glua at the moment.

  • Print