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

Create chats easier!

Started by Mads, 04 September 2012 - 04:36 PM
Mads #1
Posted 04 September 2012 - 06:36 PM
Why use Rednet for chatting? Or HTTP? Why all of that, when you can just use the current running "virtual machine"(too much Java)?

Say, I have this program on one computer:


_G["x"] = 12 -- same as "x = 12"
function print_var(_x)
    print(_x)
end

And then this program on another computer:


print_var(x)

If we then run the program on the first computer first, and then run the program on the second computer, it will output this:

12

This can be used in a chat, ofcourse. And very easily too!
Example of a simple message queue:


-- Server
local messages = {"Initiated"}
local function on_new_message()
-- Send a notification to all clients
end
function push_msg(msg)
if type(msg) ~= "string" then
  error "Bad argument #1 to 'push_msg()': argument must be of type 'string'"
end
for i = #messages, 1, -1 do
  messages[i + 1] = messages[i]
end
messages[1] = msg
on_new_message()
end

Please tell me if you make anything with this.

Thank for reading, I hope this will help you develop great chat applications soon!
Jan #2
Posted 04 September 2012 - 07:01 PM
It doesnt magicly save the variables on both computers :D/>/>
I get attempt to call nil. Because the function print_var is not defined on the second computer.
What could I've done wrong?
Mads #3
Posted 04 September 2012 - 07:11 PM
I don't know.

I just based this theory on tests which I have made in previous versions of CC, and my general knowledge of Lua(And LuaJ).
cant_delete_account #4
Posted 04 September 2012 - 08:24 PM
It doesnt magicly save the variables on both computers :D/>/>
I get attempt to call nil. Because the function print_var is not defined on the second computer.
What could I've done wrong?
It never defines that function for the other computers, you have to create that on the other computer, and also for this to work you might to to unprotect(_G) in bios.lua, at least from my testing.
Mads #5
Posted 05 September 2012 - 05:25 PM
That could be a pretty good answer to this…
D3matt #6
Posted 05 September 2012 - 11:00 PM
This feels a lot lke cheating :D/>/>
Mads #7
Posted 06 September 2012 - 07:53 AM
Why? It is just using the advantages of how the mod is screwed together :D/>/>
Cloudy #8
Posted 06 September 2012 - 09:27 AM
This also doesn't work. They don't even share the same global table.
Mads #9
Posted 06 September 2012 - 11:51 AM
That's weird… I remember making a chat with this method once.
ElvishJerricco #10
Posted 07 September 2012 - 12:44 AM
This does not work. The computers all have separate instances of the Lua runtime. They do not share global tables and therefore share absolutely no data atuomatically like you're suggesting.
Mads #11
Posted 07 September 2012 - 07:28 AM
This does not work. The computers all have separate instances of the Lua runtime. They do not share global tables and therefore share absolutely no data atuomatically like you're suggesting.

If you had read my comments, you would know that this was based on tests, which was all done in much earlier versions of ComputerCraft.
Cloudy #12
Posted 07 September 2012 - 07:38 AM
But earlier versions of ComputerCraft worked the same way.
Mads #13
Posted 07 September 2012 - 07:47 AM
On the server that I played on it didn't. Might have had something to do with a modification to the original mod.
D3matt #14
Posted 07 September 2012 - 08:25 AM
On the server that I played on it didn't. Might have had something to do with a modification to the original mod.
Would have to be a SERIOUS modification for something like that to happen.

Like, total rewrite of core files.
Mads #15
Posted 07 September 2012 - 08:56 AM
Yes, I am aware of that, Lua is not the only language that I use :D/>/>.