Triciya Posted July 9, 2019 Share Posted July 9, 2019 (edited) Hi all! Im new at lua programming, so mb the question will be dumb. I tried to create a modification which drops items from player's inventory when he is going offline, but not all of them. I need to put off only certain things like eyebrella or walrushat. Firstly I tried to rewrite a mod Empty Inventory by adding "player.components.inventory:DropEverythingWithTag" instead of "player.components.inventory:DropEverything", but then found that there's no tags for each item. And there's question: Can I use AddTag for existing game items inside the mod code? Or is there any another way to drop certain things? Edited July 9, 2019 by Triciya Link to comment Share on other sites More sharing options...
Triciya Posted July 9, 2019 Author Share Posted July 9, 2019 Well, I found the way to create tags untill moderator approved me here But there's even more important next question: all whitelists I saw in mods created temporal list of local player numbers instead of external txt list with user KU. How can I create external not temporal whitelist for mod? Link to comment Share on other sites More sharing options...
Ultroman Posted July 9, 2019 Share Posted July 9, 2019 Here's a tutorial about writing to a file using Lua. Easiest Google search I've done in a while That said, I don't know if you are allowed to do I/O operations within the context of the game. I kind of doubt it. So, you've successfully done what you wanted with the drop-when-you-leave-the-server mod? And are now asking how to do a whitelist mod...for what exactly? What is your end goal? Link to comment Share on other sites More sharing options...
Triciya Posted July 9, 2019 Author Share Posted July 9, 2019 Yeah I saw this tutorial, but actually io doesnt work in game context untill you define local io as a GLOBAL.io. And that I did. But this tutorial isnt exact thing I need. Now Im trying to create external table or array with structure like List = { KU_.... = true } where true is a value of dontdrop function for player, and KU is a key. I hope this could allow me to have a permanent WhiteList (for people who won't drop their items) which I can load from the external file every time I reboot my server by using k,v cycle. But something is going wrong .The idea is to drop certain player items at despawn event (done) and create permanent WhiteList as an external table. I hope I described it clear. Link to comment Share on other sites More sharing options...
Ultroman Posted July 9, 2019 Share Posted July 9, 2019 Is the mod just for you (no intention of releasing it)? Do you want to be able to edit the file while the server is running? You don't need the data in the file to be code. Each line should just be a player ID. Then you can do this. Link to comment Share on other sites More sharing options...
Triciya Posted July 9, 2019 Author Share Posted July 9, 2019 1. Firstly, this mod is supposed to be for my server, which Im hosting for one big discord-community. But mb in future I will polish it and release (not sure, really) 2. Yeah, thats the idea, and thats why I cant fix players KU in mod body. Thanks <3 I will try this feature and report later! Link to comment Share on other sites More sharing options...
Ultroman Posted July 9, 2019 Share Posted July 9, 2019 6 minutes ago, Triciya said: Thanks <3 I will try this feature and report later! Please do. The more solutions we can have on the forum, the better Link to comment Share on other sites More sharing options...
Triciya Posted July 28, 2019 Author Share Posted July 28, 2019 (edited) Hello again! During this time I have rewritten the code multiple times until I realized that io.open and write commands dont work. At the present moment I have a file "List.txt" which consists of some random strings (1111,2222) in mod directory. I define var WL as "List.txt": local WL = 'List.txt' then I read contents of the file with the function function lines_from(WL) if not file_exists(WL) then return {} end lines = {} for line in io.lines(WL) do lines[#lines + 1] = line end return lines end and then I try to add userid there by this way: local function file_write(WL, player.userid) local f = io.open(WL, "a") f:write("\n", player.userid) if f then f:close() end return f ~= nil end aaaand it doesnt work =_= I tested it and as I told before it seems the cause is IO P.S: I have the following string in code: local io = GLOBAL.io P.S.S: Forgot to mention the file_exists function: function file_exists(WL) local f = io.open(WL, "rb") if f then f:close() end return f ~= nil end Edited July 28, 2019 by Triciya Link to comment Share on other sites More sharing options...
Ultroman Posted July 28, 2019 Share Posted July 28, 2019 (edited) It should just be local function file_write(WL, userid) local f = io.open(WL, "a") f:write("\n", userid) if f then f:close() end return f ~= nil end You can't have dots in a parameter name when declaring a function. Only when you're calling the function, e.g.: file_write("somefile.txt", player.userid) Edited July 28, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Triciya Posted July 28, 2019 Author Share Posted July 28, 2019 Thanks! Fixed this thing... But it also doesnt recognize f = io.open(smthng) as a new local/global var: "attempt to index local/global 'f' (a nil value)", however it perfectly worked in interpretator Link to comment Share on other sites More sharing options...
Ultroman Posted July 28, 2019 Share Posted July 28, 2019 Can you post the complete file where you're doing this? It sounds really weird and like a syntax or structure problem. Link to comment Share on other sites More sharing options...
Triciya Posted July 28, 2019 Author Share Posted July 28, 2019 (edited) Im really sorry for bothering you Edited July 28, 2019 by Triciya Link to comment Share on other sites More sharing options...
Ultroman Posted July 28, 2019 Share Posted July 28, 2019 You removed the "local" from your declaration of the variable "f" in the scope of your function. local function file_write(userid) local f = io.open("List.txt", "a") f:write("\n", userid) if f then f:close() end return f ~= nil end Remember to also check whether "f" is nil before using it here, so: local function file_write(userid) local f = io.open("List.txt", "a") if f then f:write("\n", userid) f:close() end return f ~= nil end Link to comment Share on other sites More sharing options...
Triciya Posted July 28, 2019 Author Share Posted July 28, 2019 (edited) Thank you. Now there's no error, but it still doesnt work for writing in List.txt , and now I even have no idea why: local function Whitelist(num) local player = GLOBAL.AllPlayers[num] if player then player.dontdrop = true file_write(player.userid) <-- command totally ignors this print("Player ", player.name, " using ", player.prefab, " with id ", player.userid, " got added to the whitelist.") end end Edited July 28, 2019 by Triciya Link to comment Share on other sites More sharing options...
Ultroman Posted July 28, 2019 Share Posted July 28, 2019 (edited) Try this instead file_write(tostring(player.userid)) It can't "ignore" the line with your function call, as such. Something goes wrong, and the function doesn't do anything. Use the debugging tricks, namely the print-statements, mentioned in the newcomer post to see what your function is doing. It is very possible that the game simply doesn't have writing privileges to the harddrive, but I'm just pretty sure I've seen other mods do something like that. Edited July 28, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Triciya Posted July 30, 2019 Author Share Posted July 30, 2019 Hello again! I debugged things with io and file reading-writing. Now it works perfectly... And I've got another problem: To check player.userid I tried to listen to playerspawn event, but it turns out that game hasn't any information about player when this event happens (no player.prefab, player.name and, the most important, player.userid). So, I suppose, I should listen to playeractivated event, as a later one, but I can't find Prefab or Component I should use to call this event in the code: AddPrefabPostInit("???", function(inst) or AddComponentPostInit("???", function(inst) inst:ListenForEvent("playeractivated", function (inst, player) May you please give a hint? Link to comment Share on other sites More sharing options...
Ultroman Posted July 30, 2019 Share Posted July 30, 2019 AddPlayerPostInit would be a good one. You'll have all that information in there. Link to comment Share on other sites More sharing options...
Triciya Posted July 31, 2019 Author Share Posted July 31, 2019 Sorry, I still can't find any systematic information about AddPlayerPostInit and its working principles. Could you recommend any good documentation of those? Cause all pieces of knowledge I found are just some side discussions, modutil and some modmain files from another mods. Link to comment Share on other sites More sharing options...
Ultroman Posted July 31, 2019 Share Posted July 31, 2019 Well, that's a lot of information you already have. What are you confused about? Link to comment Share on other sites More sharing options...
Triciya Posted July 31, 2019 Author Share Posted July 31, 2019 I am trying to follow the logic of other PostInits and execute the code below, but it does nothing. AddPlayerPostInit(function(inst) inst:ListenForEvent("playeractivated", function(inst, player) print('---------------------------BEEP----------------------') --I omitted some code here just to demostrate a minimal example end) end) I tried to use AddPrefabPostInitAny as specified in modutils.lua: env.AddPrefabPostInitAny(function(inst) if inst and inst:HasTag("player") then print("Player ", inst.name, " using ", inst.prefab, " with id ", inst.userid, " got added to the whitelist.") end end) If I understand correctly, it allows us to retrieve some prefab information, associated with the player character. But it has no info about the player's name and userid. So, in the end, I do not understand why my "ListenForEvent" doesn't work here. Link to comment Share on other sites More sharing options...
Ultroman Posted July 31, 2019 Share Posted July 31, 2019 (edited) That AddPlayerPostInit should be working. Those BEEP lines should be showing up in your server logs (not your client log, but check anyway). Is it placed in your modmain.lua? If so, can you attach your modmain.lua to a reply here? The postinits are executed just after the player had been initialized, but before they get "put onto the server" (I think) so some of that information is missing. If you need that information, but want to trigger it on player postinit, all you have to do is delay the execution of your code until the next frame by leveraging the power of DoTaskInTime. Giving it a delay of 0 makes it fire on the next frame. AddPlayerPostInit(function(inst) inst:DoTaskInTime(0, function(inst) print('Reached first frame') end) end) Edited July 31, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Triciya Posted July 31, 2019 Author Share Posted July 31, 2019 (edited) AddPlayerPostInit starts from line 66. Both logs do not show the BEEP line. Edited July 31, 2019 by Triciya Link to comment Share on other sites More sharing options...
Ultroman Posted July 31, 2019 Share Posted July 31, 2019 The beeps should actually show up in your client log. No idea why. The code you have works for me. Link to comment Share on other sites More sharing options...
Triciya Posted August 1, 2019 Author Share Posted August 1, 2019 Well, tried to run on a different machine and with another server config. Still see nothing in server and client logs. Could you please send your logs and the server config you used? Link to comment Share on other sites More sharing options...
Ultroman Posted August 1, 2019 Share Posted August 1, 2019 Sorry, I deleted the things. What do you mean by "server config"? I just hosted a new server from within the game with the smallest forest and the smallest caves, and put in a mod where the modmain.lua just had this code: AddPlayerPostInit(function(inst) inst:ListenForEvent("playeractivated", function(inst, player) print("poopispower") end) end) That line shows up in my client log. The second my player spawns in the game, I can pull out the in-game log and the line is there. Then I quit and checked my client_log.txt file, and the line was there. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now