Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

Question

5 answers to this question

Recommended Posts

  • 0
  • Solution

Sleep is a wonderful thing! - The answer to your question is Concatantaion, in Lua it is very simple to do here is an example:

local word = "a string of text"
local num = 5

print(word..num)

-- to have fixed text just do
print("This is always the same text, with the number stored in num "..num)

Because when I first saw this post I was half asleep and not paying attention below this line, in the spoiler, is a semi solution. To see my original post with a full example of what I'd try to do for you issue click the spoiler button - if you want the challenge of doing it your self (always more rewarding) then don't!  :D
 


Not sure what the question is but based on the Wiki you forgot to open the port to broadcast on:
 

-- Hook components
-- (the Wiki has the event too but that is for the message receive part so i dropped it)
local component = require("component")
local m = component.modem -- get primary modem component

local time = 5 -- time to wait between ping messages
local count = 1 -- intialise count to 1

-- Open the port on the network and print out if it is open (should print true or this wont work)
m.open(123)
print(m.isOpen(123)) -- true

-- Do a ping test (sort of)

while count < 5 do
    m.broadcast(321, "ping "..count)
    count = count + 1
    os.sleep(time)
end

-- close the network port
m.close()

Your receiving server will just simply listen with the event system... I don't really know why you'd want to do this but it's what you asked.
 

-- Hook the Events library
local event = require ("event")

-- Wait for a message from another network card.
local _, _, from, port, _, message = event.pull("modem_message")
local data = tostring(message)

-- check that the received event was a ping message, like ping 1 and do something
if(data == "ping 1") then
    print "I received a ping, it was the first ping!"
end

Not tested this and it's currently 01:15 local time so I'm heading to bed, but hope this helps.

Link to post
Share on other sites
  • 0

It's all about scope, or better memory management. If you are only doing a small program it doesn't matter but good practice is important.

Lua code runs in blocks, local variables are only available in the current block, a global variable is always available. What this means in terms of memory is a global variable, once created will sit around in memory until the program ends or you set the variable to nil.

The Lua manual explains this with examples but I am posting from my phone so don't have access to the link at the moment.

Here is the link to what I am explaining: http://www.lua.org/pil/4.2.html

Link to post
Share on other sites
  • 0

It's not just a memory thing - though memory is a much bigger concern on OpenComputers than on a real computer - using global variables for everything clutters the global namespace and can cause odd bugs if you use the same name in different places. Because every computer has a single global namespace for every script (unless the script is called using pcall, which gives it an own "global" namespace) this means that a global variable from an external library can "leak" into your program, or even into other libraries.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.