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

fluid transfer program

Question

I'm new to lua, so forgive me if i'm being stupid.

I'm trying to write a program that will let me transfer specific amounts of fluid from one tank to another using a transposer. Ideally I would be able to input "ingot" measurements instead of buckets, ex. Ingot = 144mb, nugget = 16mb, etc. but it won't work, I've uploaded what little code i have and the error message it gives back, p.s. I'd also like to have the program repeat until i input something like "end"

2018-09-22_17.03.41.png

2018-09-22_17.03.18.png

Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 1
  • Solution

`io.read()` returns a string, and you want a number, there is a core lua function to convert strings to numbers: `tonumber(string): number`

so something like:

`local num = tonumber(io.read())`

would be fine

note that tonumber returns nil when the input can't be converted.

 

Also, I strongly recommend you get in the habbit of declaring everything local, unless you are intended to modify the global state of the operating system, and future scripts that you run, at least until the next time you boot. Thus:

local component = require("component")
local sides = require("sides")

local ingot_mb = 144
local nugget_mb = 16
local block_mb = 1296

while true do
  local arg = io.read()
  if not arg or arg == "end" then
    break
  end
  local num = tonumber(arg)
  if num then
  	component.transposer.transferFluid(sides.south, sides.north, num)
  else
    print("could not understand", arg)
  end
end

 

Link to post
Share on other sites
  • 1

my code sample isn't using the predefined millibucket values at all. It reads user input and tries to convert it to a number. If you want to build something more clever that does a lookup for predefined sizes, you'll have to design that yourself. Here are a couple of snippets that would do some of what you're saying.

 

local size = tonumber(arg)
if not size then
	if arg == "ingot" then
		size = 144
	end
end

or

local predefined_sizes = {
	ingot = 144
}

local size = predefined_sizes[arg] or tonumber(arg)

I could write the whole program, but you'll learn more if  you test your ideas yourself.

There is a good online manual for learning lua: https://www.lua.org/pil/

Link to post
Share on other sites
  • 0
11 minutes ago, Michiyo said:

When writing a script you have to require the component library with

component = require("component")

When running code directly from the Lua interpreter this line isn't needed.

yay, progress! thanks, i added that to the top but now i get this

2018-09-22_17.28.00.png

so as far as i can tell it's either mistaking the "amnt" variable for a string, or I didn't set up my variables right

2018-09-22_17.30.15.png

Link to post
Share on other sites
  • 0
1 hour ago, payonel said:

`io.read()` returns a string, and you want a number, there is a core lua function to convert strings to numbers: `tonumber(string): number`

so something like:

`local num = tonumber(io.read())`

would be fine

note that tonumber returns nil when the input can't be converted.

 

Also, I strongly recommend you get in the habbit of declaring everything local, unless you are intended to modify the global state of the operating system, and future scripts that you run, at least until the next time you boot. Thus:


local component = require("component")
local sides = require("sides")

local ingot_mb = 144
local nugget_mb = 16
local block_mb = 1296

while true do
  local arg = io.read()
  if not arg or arg == "end" then
    break
  end
  local num = tonumber(arg)
  if num then
  	component.transposer.transferFluid(sides.south, sides.north, num)
  else
    print("could not understand", arg)
  end
end

 

thanks, this works with numerical values, but the program isn't reading the variables right, if i input ingot it skips straight to "could not understand" do i need to place the variable values somewhere else in the code, or is there some other way i have to do this. but at least i have a working prototype, so thanks!

 

after replacing arg with num in the print section, it now reads "could not understand nil" so, that means that it isn't recognizing that ingot = 144 right?

Edited by Quelfyre
additional info
Link to post
Share on other sites
  • 0
15 hours ago, payonel said:

my code sample isn't using the predefined millibucket values at all. It reads user input and tries to convert it to a number. If you want to build something more clever that does a lookup for predefined sizes, you'll have to design that yourself. Here are a couple of snippets that would do some of what you're saying.

 


local size = tonumber(arg)
if not size then
	if arg == "ingot" then
		size = 144
	end
end

or


local predefined_sizes = {
	ingot = 144
}

local size = predefined_sizes[arg] or tonumber(arg)

I could write the whole program, but you'll learn more if  you test your ideas yourself.

There is a good online manual for learning lua: https://www.lua.org/pil/

thank you for all of your help, looking back over this i might as well have posted this in requests.

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.