Computercraft Lua change nil to 0 - lua

I am using Computercraft, a Minecraft mod, and I needed help with something. I am attempting to find all peripherals of a type, get the energy from them, and add them together. However, I get a "attempt to perform arithmetic on nill" or something error. Here is my code:
local periList = peripheral.getNames()
energy = 0
totalenergy = 0
for i = 1, #periList do
if peripheral.getType(periList[i]) == "cofh_thermalexpansion_energycell" then
local cell = peripheral.wrap(periList[i])
print(periList[i])
if cell.getEnergyStored("potato") ~= "nil" then
energy = cell.getEnergyStored("potato")
print(cell.getEnergyStored("potato"))
else
energy = 0
print(0)
end
totalenergy = totalenergy + energy
end
end
print(totalenergy)
Sorry, codebox didn't work
Anyways, does anyone know how to fix this?

nil and "nil" are two different things.
The former is the nil type "singleton" the latter is a string of three characters. They are not equivalent.
Try dropping the quotes from the if line.
Also you can assign (the potential) nil to energy and then directly energy and set it to 0 if it is nil (or even just use
energy = cell.getEnergyStored("potato") or 0
directly since nil is a "false-y" value so nil or 0 evaluates to 0).

Related

Lua math.random explain

sorry for asking this question but I couldn't understand it
-- but i don't understand this code
ballDX = math.random(2) == 1 and 100 or -100
--here ballDY will give value between -50 to 50
ballDY = math.random(-50, 50)
I don't understand the structure what is (2) and why it's == 1
Thank you a lot
math.random(x) will randomly return an integer between 1 and x.
So math.random(2) will randomly return 1 or 2.
If it returns 1 (== 1), ballDX will be set to 100.
If it returns 2 (~= 1), ballDX will be set to -100.
A simple way to make a 50-50 chance.
That is a very common way of assigning variables in Lua based on conditionals. It’s the same you’d do, for example, in Python with “foo = a if x else b”:
The first function, math.random(2), returns either 1 or 2. So, if it returns 1 the part math.random(2) == 1 is true and so you assign 100 to the variable ballDX. Otherwise, assign -100 to it.
In lua
result = condition and first or second
basically means the same as
if condition and first ~= nil and first ~= false then
result = first
else
result = second
end
So in your case
if math.random(2) == 1 then
ballDX = 100
else
ballDX = -100
end
in other words, there is a 50/50 chance for ballDX to become 100 or -100
For a better understanding, a look at lua documentation helps a lot :
https://www.lua.org/pil/3.3.html
You can read:
The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
So if the random number is 1 it will return the first argument (100) of the "or" otherwise it will return the second argument (-100).

Inexplicable syntax error in Lua for logitech gaming software

I've being trying this language and i keep getting random syntax errors and i cant figure out why, this is a simple recoil script. It keeps telling me i have a syntax error on line 39, everything is green so i can still try it out while i try to fix. I can post the rest of the code if someone needs.
Code:
function recoil_values()
if gun_mode = "gun1" then
if round = "attack" then
--Ash R4C--
x_recoil = -1
y_recoil = 4
sleep_value = 10
else
--Twitch F2--
x_recoil = -10
y_recoil = 0
sleep_value = 10
end
else
if round = "attack" then
--Jager 41C--
x_recoil = 10
y_recoil = 0
sleep_value = 10
else
--Bandit MP7--
x_recoil = 0
y_recoil = 10
sleep_value = 10
end
end
end
Your if statements contain =, which is used for assignment. Lua does not allow assignments between if and then.
If you want to compare two values for equality, use ==.

How to check only numbers in value - Lua

I want to make sure players can't put letters or symbols in the value, how do I check if it is a number only?
function seed1()
ESX.UI.Menu.CloseAll()
ESX.UI.Menu.Open('dialog', GetCurrentResourceName(), 'amountseed1', {
title = 'Shop'
}, function(data, menu)
local amount = tostring(data.value)
if amount == nil then
...
else
[[What should i put here to check its only contain number ?]]
end
end, function(data, menu)
menu.close()
end)
end
I can put something like this, but maybe this isn't a good way to do that:
else
if amount > 0 and amount < 9999 then
...
else
print('Invalid amount or amount higher than 9999')
end
end
Since you only care about the number, there is no need to convert the value to string:
local amount = tostring(data.value)
-- ^^^^^^^^ partially useless
Instead, go for the number right away:
local amount = tonumber(data.value)
if amount == nil then
-- Not a number
else
-- A number
end
In the end, remember that tonumber attempts to convert the value to a number and returns nil in case of a failure.
Simply replace tostring with tonumber. This will turn strings into numbers if possible, and return nil if it can't.
Keep in mind: tonumber won't just take the largest valid prefix of a string, so tonumber("20 foo") will return nil and not 20. It also supports all ways to write number literals in Lua, so tonumber("2.3e2") will return 230 and tonumber("0xff") will return 255.

lua overload: possibilities?

My group is currently working with Lua,creating an android game. One thing we've run into is the appearant inability to create overload constructors.
I'm used to having an object set up with default values, that then get overloaded if need be.
ex:
apples()
{
taste="yum";
amount = 0;
}
apples(string taste, int num)
{
taste=taste;
amount=num;
}
However, with the inability to do that, we have these lare if/else sections for initialization that look like this
if velX ~= nil then
self.velX = velX
else
self.velX = 0
end
if velY ~= nil then
self.velY = velY
else
self.velY = 0
end
Is there a better way to set this up in Lua?
Instead of using if/else statements, you can initialize your variables with a condition providing the default value.
function apples(taste, num)
taste = taste or "yum"
amount = num or 0
-- ...
end
Lua's or operator evaluates and returns its first operand unless it is nil or false, otherwise it evaluates and returns its second operand. That leads to the above idiom for default values.

Lua Problems with Arguments

What's wrong with this lua code, my argument is never converted to a number or recognized as a number no matter what I type?
I tried "distance = tonumber(arg[0]) or 0" as well.
--Args
local args = {...}
--Variables
local distance = 0
if #args > 0 and type(args[0])=="string" then args[0] = tonumber(args[0]) end
if #args > 0 and type(args[0])=="number" then distance = args[0] end
print("Distance: "..distance)
Lua uses 1-based indices for its arrays. args[0] is nil, and therefore has the type "nil".
By the way, this condition is entirely unnecessary. tonumber will check to see if its argument is a number and simply return it if needed. It will return nil if the argument cannot be converted to a number. So just use:
distance = tonumber(args[1])
You don't even need to check the length of args; if no arguments were provided, it will be nil, and tonumber will return nil. Thus, just check to see if distance is nil.

Resources