This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CreeperGoBoom's profile picture

[solved] code works in singleplayer but not multiplayer

Started by CreeperGoBoom, 06 May 2014 - 05:49 AM
CreeperGoBoom #1
Posted 06 May 2014 - 07:49 AM
can i use something like this to set a variable? where output is the variable I'm targeting?

function getstr(file,output)
local f=fs.open(file,"r")
output=f.readAll()
f.close()
end

anything else i can do to simplify the process of changing such vars without using all the same sections of code like the one above?

or do i just need to read everything i need from all the files i need into a table? am relatively new to lua btw
Edited on 08 May 2014 - 06:02 AM
Lyqyd #2
Posted 06 May 2014 - 08:10 AM
You're asking a very specific question that I don't think really gets to the heart of what you're trying to do. That above code wouldn't work (you'd need to return the value and have the calling code catch it), but you're going to get much better answers if you give a more thorough description of what you're trying to accomplish. Start your explanation/question from the beginning. That way, you can take advantage of the maximum amount of our shared experience and we can point out anywhere in your approach that you're making things needlessly complex or difficult.
CreeperGoBoom #3
Posted 06 May 2014 - 08:29 AM
thank you
Lyqyd :D/>

here is my code:it is only starter (not quite finished but working…to a point)

due to its size here is a pastebin link:
http://pastebin.com/ENmxUZim

edit,: sigh ive done it again..not being thorough enough….

at the moment, at launch, it checks if a file called config is present, if not runs a first time config to set preferences for that pc and saves all the vars to files

problem im having is, the code im using doesnt seem to be correctly reading the contents of said files, so after the config and reboot there is a screen: server:<id>:ACTivE

nothing is being written to id, its just using the default set var of " " so i see
server: :ACTIVE after reboot

and i have checked the files, they all have the intended data saved :)/>
Edited on 06 May 2014 - 07:07 AM
CreeperGoBoom #4
Posted 06 May 2014 - 12:40 PM
You're asking a very specific question that I don't think really gets to the heart of what you're trying to do. That above code wouldn't work (you'd need to return the value and have the calling code catch it), but you're going to get much better answers if you give a more thorough description of what you're trying to accomplish. Start your explanation/question from the beginning. That way, you can take advantage of the maximum amount of our shared experience and we can point out anywhere in your approach that you're making things needlessly complex or difficult.

just read over this again and found i had to do this:
function getstr(file) --gets a var and stores it at output var..well, thats what i want to do
    local f=fs.open(file,"r")
    output=f.readAll()
    f.close()
return output
end

and then catch it later with this:
function startup ()-- should be getting info from files and storing to vars
    getstr(idfile,id)
    id = output
    getstr(mfile,mside)
    mside = output
end

everything is cool now program works perfect

thanks again Lyqyd :D/> i now understand
MKlegoman357 #5
Posted 06 May 2014 - 01:30 PM
It's working, but I don't think you understand why and how it is working. In the above code, if you were to remove return output it would still work. That's because in your getstr function you set the global output to the contents of the file. By doing that you are able to access it by doing id = output. You should use local variables instead:


a = 1 --// This is a global variable
local b = "private" --// This is a local variable

Getting back to what Lyqyd said. He advised to return the content of the file using return output. To actually catch that return value (output) you would have to do something like this:


local output = getstr("file.txt")

Note: Don't forget that you first have to define a local variable before you try to use it.

So your fixed code would look something like this:


local function getstr(file) --// Localize the function too, define it before we use it in 'startup' function
  local f = fs.open(file,"r")
  local output = f.readAll() --// Make 'output' be local too
  f.close()
  return output --// Return the value of 'output'
end

local function startup () --// Localize the function
  local id = getstr(idfile) --// Call 'getstr' and put it's returning value (contents of file) into the variable 'id'
  local mside = getstr(mfile) --// Call 'getstr' and put it's returning value (contents of file) into the variable 'mside'
end
Edited on 06 May 2014 - 11:32 AM
CreeperGoBoom #6
Posted 07 May 2014 - 08:13 AM
thanks man now im having a different problem

i managed to get the client and server to communicate no probs

now, for some reason:
1, the code for the server isnt working correctly, getting error: peripheral:20:expected string
for the life of me i cannot see what would be causing this error, getting a massive headache, been over the code step by step 10 times now

2.was having some probems getting the client to transmit the server number over rednet to verify that that server exists, and got it to a point where the client pc would reboot if theres no response after 3 seconds
for troubleshooting i set the server pc to print the channel number as should be set with the get str function

if i were to use a bogus number on the client, it would restart without an issue, but if i use the correct channel number, it errored with expected number, which means it was bypassing the part that writes the channel number to file then reboots the pc, this is where i looked into more code online and somehow ended up with the error on server on 1. (thats the odd part, NO CODE was changed on the server pc before the error, or maybe ive missed something

3. either the client isnt transmitting the channel number or the server just isnt receiving, this is where i started messing around and ended up with the errors above, maybe you guys can see what ive done wrong

heres the code links:

server

client

thanks in advance
CometWolf #7
Posted 07 May 2014 - 10:00 AM
The peripheral error comes from your one call to the peripheral api, wrap. Which you attempt to pass the variable mside to, however this variable is nil because you are not returning values from getstr properly. The rest of your issues probably stem from the same problem. I'd suggest you read this again.
It's working, but I don't think you understand why and how it is working. In the above code, if you were to remove return output it would still work. That's because in your getstr function you set the global output to the contents of the file. By doing that you are able to access it by doing id = output. You should use local variables instead:


a = 1 --// This is a global variable
local b = "private" --// This is a local variable

Getting back to what Lyqyd said. He advised to return the content of the file using return output. To actually catch that return value (output) you would have to do something like this:


local output = getstr("file.txt")

Note: Don't forget that you first have to define a local variable before you try to use it.

So your fixed code would look something like this:


local function getstr(file) --// Localize the function too, define it before we use it in 'startup' function
  local f = fs.open(file,"r")
  local output = f.readAll() --// Make 'output' be local too
  f.close()
  return output --// Return the value of 'output'
end

local function startup () --// Localize the function
  local id = getstr(idfile) --// Call 'getstr' and put it's returning value (contents of file) into the variable 'id'
  local mside = getstr(mfile) --// Call 'getstr' and put it's returning value (contents of file) into the variable 'mside'
end
Edited on 07 May 2014 - 08:01 AM
CreeperGoBoom #8
Posted 08 May 2014 - 03:32 AM
question, every time i set a value even though its already localized do i have to put local before it again?

example

function suchandsuch()
  local var=something
end

----

local var = somethingelse

just to verify
theoriginalbit #9
Posted 08 May 2014 - 03:55 AM
no, if you do that it will make it local to the scope it is within, meaning it won't modify it in the other locations.

Example

Without local (correct)

local foo = "hello"

local function bar()
  foo = "world"
  print(foo) --# world
end

print(foo) --# hello
bar()
print(foo) --# world


With local (incorrect)

local foo = "hello"

local function bar()
  local foo = "world"
  print(foo) --# world
end

print(foo) --# hello
bar()
print(foo) --# hello
CreeperGoBoom #10
Posted 08 May 2014 - 04:04 AM
Ah I see

thank you guys, its a good thing it was something easily overlooked

thanks for your time and help on this matter, really do appreciate it

code links updated just in case you guys were curios on my end result
(yay now i can finally roll out the programs on my friends server he's been waiting a few days for em :D/>)

edit: you guys are gonna be proud, ive swapped to using an array writing to a single file instead of writing each var to its own individual file :D/> (not bad for a newb eh? :D/>)
check out server link

lol i know the saying goes if it aint broke dont fix it, well this just makes my setup more dynamic :D/>
Edited on 08 May 2014 - 03:40 AM
CreeperGoBoom #11
Posted 08 May 2014 - 07:07 AM
oh my god, , ok server code works fine in single player

but, when i pastebin the same code into my friends server, it errors on modem.open on line 72, why is this? and yes i have deleted the local program and redownloaded via pastebin

this makes no sense, why would the same code work in singleplayer but not multiplayer?

sorry about double posting but this is stupid

edit: attempt to index ? a nil value on modem.open(channel) (line 72)

heres the link again
server

edit:verified that the info has written to file on server, maybe something on server is denying access to filereading?

edit3: i helped build the server, so mod version shouldn't be an issue (we are using CC1.63)
Edited on 08 May 2014 - 05:42 AM
CometWolf #12
Posted 08 May 2014 - 07:43 AM
There's nothing that could prevent read-acess. The error indicates that there is no modem on the given side.
CreeperGoBoom #13
Posted 08 May 2014 - 08:02 AM
phew!! thanks!!! :D/>