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

Changing variables in a program via a pocket comp

Started by Dustmuz, 24 February 2015 - 08:06 PM
Dustmuz #1
Posted 24 February 2015 - 09:06 PM
i must say i have no idea on how to do this..
so i might as well ask before starting on the program..

im making a computer at a shop, which shows who owns which plot, and which plots are for rent..

i would like to encase these fully, but still be able to change what is on the screen..

i was thinking of doing by having the text saved in the variables, and then printing the variable

something like this

local plot1 = "For rent"
mon.write(plot1)


and then be able to change what plot1 is..

how would i be able to do that from a pocket computer (no interaction with the actual computer running the screen program)
Dog #2
Posted 24 February 2015 - 09:17 PM
To send information back and forth between computers you'll want to look into the Modem API and the Rednet API and choose which suits your need better. Since you have no code, I'm not sure what else you'll need help with, but the rest *should* be pretty self explanatory if you're reasonably familiar with CC Lua. If you have further questions, please post away and I or someone will help point you in the right direction.

A simple example with Rednet - this will change plot1 from "For rent" to "Rented" - no user interaction in this example…

Pocket Computer

local serverID = 7 --# this should be set to whatever the computer # is for the server (this can be found out by typing ID at the prompt)

for _, side in pairs(rs.getSides()) do --# find the modem
  if perihperal.isPresent(side) and peripheral.getType(side) == "modem" then
  rednet.open(modemSide) --# initialize the modem for use with Rednet
  break
end

local plot1 = "Rented"

rednet.send(serverID, plot1)

Server

local clientID = 5 --#  this should be set to whatever the computer # is for the pocket computer (this can be found out by typing ID at the prompt)
local plot1 = "For Rent" --# initialize our plot1 variable with the string "For Rent"

for _, side in pairs(rs.getSides()) do --# find the modem
  if perihperal.isPresent(side) and peripheral.getType(side) == "modem" then
  rednet.open(modemSide) --# initialize the modem for use with Rednet
  break
end

local id, message = rednet.receive() --# wait for a Rednet message
if id == clientID then --# if the sender if our pocket computer
  plot1 = message   --# update plot1 with the new value
end

Clearly you'll need to change this to suit your need - for example, you'll probably want to use tables so you can track and transmit each plot and it's status more easily.

A simple table for holding your plots could look like this…

local plots = { [1] = "For Rent", [2] = "For Rent", [3] = "For Rent" }

The above table hold three plots, numbered 1, 2, 3 - each with the status for that plot. Then you would adjust the above example accordingly…

Pocket Computer

local serverID = 7 --# this should be set to whatever the computer # is for the server (this can be found out by typing ID at the prompt)
local plotSelected, plotStatus --# variables used to store user input for selected plot number and its status

for _, side in pairs(rs.getSides()) do --# find the modem
  if perihperal.isPresent(side) and peripheral.getType(side) == "modem" then
  rednet.open(modemSide) --# initialize the modem for use with Rednet
  break
end

local plots = { [1] = "For Rent", [2] = "For Rent", [3] = "For Rent" }

... user input here - we'll assume the user selected plot 1 and changed it to "Rented" ...

local message = { plotSelected, plotStatus } --# create a table with our selected plot as the first entry and the plot's status as the second entry

rednet.send(serverID, message)

Server

local clientID = 5 --#  this should be set to whatever the computer # is for the pocket computer (this can be found out by typing ID at the prompt)

local plots = { [1] = "For Rent", [2] = "For Rent", [3] = "For Rent" }
local plot, status --# variables we'll use to hold the data from the table sent over rednet

for _, side in pairs(rs.getSides()) do --# find the modem
  if perihperal.isPresent(side) and peripheral.getType(side) == "modem" then
  rednet.open(modemSide) --# initialize the modem for use with Rednet
  break
end

local id, message = rednet.receive() --# wait for a Rednet message
if id == clientID then --# if the sender if our pocket computer
  plot = message[1] --# take the first table entry as our plot number
  status = message[2] --# take the second table entry as our status for that plot number
end

plots[plot] = status --# update our plots table with the new status

After the above example, the example table would look like this…

local plots = { [1] = "Rented", [2] = "For Rent", [3] = "For Rent" }
Edited on 24 February 2015 - 08:47 PM
TheOddByte #3
Posted 24 February 2015 - 09:33 PM
Well you could do this with rednet, I'll show you an example because Dog hasn't done that yet :P/>

This would be the program that would draw to the monitor

--# ID = 1 or whatever it is you have
local mon = peripheral.find( "monitor" ) --# This function automatically finds and wraps the monitor
local plot = "For rent"


while true do
	mon.clear()
	mon.setCursorPos( 1, 1 )
	mon.write( plot )
	local event, id, message = os.pullEvent( "rednet_message" ) --# Wait for a rednet message
	plot = message --# Change the variable to the message you just received
end



And this would be the one who sent the message to that computer

rednet.open( "top" ) --# Open rednet on the side where you have a modem
rednet.send( 1, "Hello" ) --# Send the 'Hello' to the computer with the ID 1

Edit: As said by others, if you have multiple plots then it's MUCH better to use tables.
Edited on 24 February 2015 - 08:51 PM
Quintuple Agent #4
Posted 24 February 2015 - 09:33 PM
As a suggestion, since you will have multiple plots and you wish to change them using rednet messages, instead of have variables like plot1,plot2,plot3,ect. You should use a table to store them all, it will be easier to code and to lookup and change who owns the plot.
Ex:
instead of

local plot1="For Rent"
local plot2="For Rent"
local plot3="Owned by DOG"
local plot4="Owned by Dan"
you could do

local plots={
"For Rent",
"For Rent",
"Owned by DOG",
"Owned by Dan"
}
Dustmuz #5
Posted 24 February 2015 - 09:43 PM
3 great answers :D/> i will see what i come up with, looks like i have a lot of work ahead of me :D/>

thanks a lot to all 3 of you..

and i will surely return with any questions :)/>