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

[semi-solved]peripheral.wrap() handle equals "nill"

Started by qwerty, 21 February 2020 - 11:08 AM
qwerty #1
Posted 21 February 2020 - 12:08 PM
I'm currently making a storage manipulation api for use on the lurcraft server, and for some odd reason the peripheral.wrap() handle returns a nill when it was working just fine when manually written in the lua prompt. The handle retuns what is expected however nickname (i.e. the wrapped handle) returns a table, but when called returns nill.

the code:

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

--#setting global variables for referance#--
crate = "immersiveengineering:woodencrate"
cache = "thermalexpansion:storage_cache"
chest = "minecraft:chest"
Qchest = "quark:quark_chest"


--#wraping the inventory with apropreate id#--
function wrapID( name , id , nickname )

  handle = tostring( name .. "_" .. id )
  nickname = peripheral.wrap(handle)

--/debug\--
  return handle, nickname
--\debug/--

end

function getDocs()
print("when using wrapID(), you need to pass a name, inventory id and a nickname for the wraper")
end


P.S. The relevant mods installed are; CCTweaked, ThermalExpansion, ImmersiveEngineering, Quark and more, but that's for future implementation.
editedd for ease of accsess
Edited on 24 February 2020 - 10:06 PM
Lupus590 #2
Posted 23 February 2020 - 02:50 PM
What does your network setup look like? I suspect that you are getting the name or id wrong somewhere
qwerty #3
Posted 23 February 2020 - 08:35 PM
found here; https://ibb.co/TLt53jP

Now while I've taken what you said into consideration, I personally didn't find anything wrong and triple checked all connections and properly wrapped them
Lupus590 #4
Posted 23 February 2020 - 09:35 PM
Can you post all the code?
qwerty #5
Posted 24 February 2020 - 09:33 AM
Can you post all the code?
that is all the code above. haven't touched it for a while since I got stuck, but you might appreciate a more detailed explanation of what I'm aiming for.

these are used as shorthand because typing >immersiveengineering:woodencrate:< is just too teadious when you have like 40 of 'em

--#setting global variables for referance#--
crate = "immersiveengineering:woodencrate"
cache = "thermalexpansion:storage_cache"
chest = "minecraft:chest"
Qchest = "quark:quark_chest"


this right here is the api call I want to implement.
you add >name< from reference(plain var), >id< from attached modem in numbers and >nickname< for ease of use and control abilities.

--#wraping the inventory with apropreate id#--
function wrapID( name , id , nickname )

  handle = tostring( name .. "_" .. id )
  nickname = peripheral.wrap(handle)

end

the use of handle is either to be wrapped in nickname or to be used in *nickname*.pullItems(handle,*slot*) or others

and that summarizes all of my code.

with regards
-qwerty
Edited on 24 February 2020 - 11:12 AM
Lupus590 #6
Posted 24 February 2020 - 02:35 PM
Variables passed to functions are copied meaning that if the function modifies the variable then the outside value that was passed in stays the same as it was when passed. An exception to this is tables, functions, and coroutines as variables containing these actually contain a pointer which is what gets copied, if 5he function modifies the pointer (assign a new table for example) then the outside value is the same but changing the object that the pointer refers to does get outside as both the function and whatever passed it are using the same thing, if by a different copy of the reference

Basically, if you return nickname does it have the value you expect?
qwerty #7
Posted 24 February 2020 - 10:55 PM
Basically, if you return nickname does it have the value you expect?

Negative Chief, this was actually my main complaint. Also now that I tried running wrapID(), the debug worked just fine(printing the inventory in handle and table in nickname), but outside it returned nil on both.

I tried to do;

var,  temp = wrapID(crate,0,"crt")
and that worked, but it's also counter productive since I wanted all that to happen inside the function.

But if I must, I will.

<edit> I've made the decision to just simply use it like regular peripheral.wrap() </edit>

with regards
-qwerty
Edited on 24 February 2020 - 10:06 PM
Lupus590 #8
Posted 25 February 2020 - 07:55 AM
I explained why it doesn't work but I will try again, Lua doesn't pass by reference, it copies function arguments. This means that the only ways to get information out of a function is to return it properly, modify an upvalue or modify a passed in table.


Local X = f() --# function f() return true end

Local t = {}
F(t) --# function f(tab) tab.x = true end

Local up
F() --# function f() up = true end

Those and variation will work, you could also pollute the global environment too.
qwerty #9
Posted 25 February 2020 - 08:24 AM
I explained why it doesn't work but I will try again, Lua doesn't pass by reference, it copies function arguments. This means that the only ways to get information out of a function is to return it properly, modify an upvalue or modify a passed in table.


Local X = f() --# function f() return true end

Local t = {}
F(t) --# function f(tab) tab.x = true end

Local up
F() --# function f() up = true end

Those and variation will work, you could also pollute the global environment too.

thank you for your time and effort! man sleep deprivation really messes with one's head

with gratitude
-qwerty