ULX

Author Topic: File.Read/Write help.  (Read 317 times)

0 Members and 1 Guest are viewing this topic.

Offline Manix84

  • Newbie
  • *
  • Posts: 2
  • Karma: 1
File.Read/Write help.
« on: June 30, 2020, 07:28:13 AM »
Hi All,
I'm going to try to describe this as best I can, but I'm aware it's both a complex and unusual problem. Here we go...

Some background, to help explain how I got here... I have a script (https://github.com/manix84/discord_gmod_addon/blob/master/lua/autorun/shared.lua) which stores a Table as JSON, containing {"SteamID":"DiscordID"}. This table drives a bot which Mutes players in Discord when they die in TTT/TTT2/Murder/etc. I've finished the script and I'm relatively happy with it, even if it needs some major cleanup. This is my first GMod addon, and my first time playing with (G)Lua.

Recently I've been trying to add ULX menu support. I successfully managed to add the commands (see: https://imgur.com/Mm17dmY), which gave me something of a boost in confidence, as I've been mostly reading other developers code, and trying to understand it. My next step was to try to introduce a settings menu (similar to how the TTT and Randomat ULX menus work). After some trial and error, I actually managed it, and added a new menu under Settings (see: https://imgur.com/qjtIImn). Again, I was elated, as this was hugely laborious work, reading through other examples, and even the source code for ULX itself.

The issue came when I tried to create a user friendly method for Admin's to add Discord ID's against Steam ID's, in the ULX menu system. What I want to do, is add a list of users who are connected, with the Discord ID listed next to there names (see: https://imgur.com/ZDkKIOU, sorry i scrubbed my ID for reasons). This works so far, when I hard code the ID JSON (EG: {"SteamID":"DiscordID"}). However, inside (https://github.com/manix84/discord_gmod_addon/blob/master/lua/ulx/xgui/settings/discord.lua), when I try to collect the cache of ID's from the file system, I get back Nil. Even when using the exact same util function (see: https://github.com/manix84/discord_gmod_addon/blob/master/lua/utils/connectionTable.lua) from the Shared.lua script. However, if I write to the file first, and then read from it, I can see the JSON, but it doesn't match the JSON in the file on the server.

What seems to be happening is File.Read/Write and ULib.fileRead/fileWrite, when in the XGUI folder, seem to be writing to some kind of Memcache layer. I don't know if this is true or not, as my experience is very small, but the evidence is pointing to that.

Side Note on why I want admins to add the connections manually from the menu: The issue comes from my own experience running a server for after work games, and a lot of these extremely intelligent programmers, don't understand how to type "!discord MyDiscordName" into chat without a lot of hand holding. Night now, I have to manually edit the connections_cache.json file and add the SteamID: DiscordID connections for new team members playing. I wanted to get away from that if I could.

If ANYONE can give me a hand figuring this out, I would be extremely grateful.
-Manix84  :)

TL;DR: I want to read and write a json file on my server. I can't seem to read/write the same file from inside lua/autorun/shared.lua and lua/ulx/xgui/settings/discord.lua. I'm really confused. For a better explanation, read above.
« Last Edit: June 30, 2020, 07:32:17 AM by Manix84 »

Offline Manix84

  • Newbie
  • *
  • Posts: 2
  • Karma: 1
Re: File.Read/Write help.
« Reply #1 on: July 01, 2020, 04:18:14 AM »
So, very kindly, Timmy gave me some help on the Discord server.

The core of my problem is that i'm using a read-write command on the Server in `lua/autorun/shared.lua`, and then attempting to do the same read-write on the client side in `lua/ulx/xgui/settings/discord.lua`. Meaning the latter was read-writing on my own machine, rather than the remote server (I suspect).

The solution is to use https://wiki.facepunch.com/gmod/net.Receive to pass the table from the server side to the client side for modification, which I will be doing later tonight :).

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 167
  • Code monkey
Re: File.Read/Write help.
« Reply #2 on: July 01, 2020, 04:25:02 AM »
Start of conversation on Discord: https://discord.com/channels/158022958940553216/599649577381003265/727820293363728414

Exactly! The file library reads/writes files depending on the Lua state. For example: calling file.Read on the server will read a file on the server, while calling file.Read on the client will read a file on the client.

Props to you for providing detailed info and all the relevant code, which made it trivial to troubleshoot!