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

Changing object variables from api functions

Started by SuperDyl19, 04 March 2018 - 03:46 AM
SuperDyl19 #1
Posted 04 March 2018 - 04:46 AM
I tried poking around the internet for a solution but came up flat. What I need is for an api function to affect a variable for the object. Here's what I want the program to do:

Spoiler

x=track
x.setPos(1,1,1)
print(x.x)

Here's the program as I have it:

track program

pos = {x=0,y=0,z=0}
rotation = 1

function setPos(x,y,z)
   if typeCheck(x,y,z) then
	  pos.x = x
	  pos.y = y
	  pos.z =z
   else
	  return false
   end
   return true
end

local function typeCheck(x,y,z)
   if type(x) == type(y) and type(y) == type(z) and type(z) == "string" then
	  return true
   else
	  return false
   end
end

There's more to the program I'm working on, but this is the piece I'm having trouble on. I'm working on understand tables and apis right now and this has gotten me quite stuck.

Also, I understand the code's not very efficient right now. You can nitpick it, but understand that I already see some of the glaring issues with it.
Edited on 04 March 2018 - 05:21 AM
Lupus590 #2
Posted 04 March 2018 - 12:48 PM
APIs in CC are tables, so focus on learning tables and you should be able to figure out APIs (writing one is a bit odd though, but using one is basically using a table).

As for learning tables, have a look at this: http://lua-users.org/wiki/TablesTutorial
and maybe this: https://www.lua.org/pil/2.5.html
SuperDyl19 #3
Posted 05 March 2018 - 05:45 AM
APIs in CC are tables, so focus on learning tables and you should be able to figure out APIs (writing one is a bit odd though, but using one is basically using a table).

As for learning tables, have a look at this: http://lua-users.org/wiki/TablesTutorial
and maybe this: https://www.lua.org/pil/2.5.html
Thanks for the links, the second link to the Lua book is answering my question. Here's a link for anyone else who needs help with this in the future: https://www.lua.org/pil/16.html#ObjectSec . To fix my type of problem, I believe I need to change

 
function setPos(x,y,z)
   if typeCheck(x,y,z) then
          pos.x = x
          pos.y = y
          pos.z = z
   else
          return false
   end
   return true
end
to be

function track:setPos(x,y,z)
   if typeCheck(x,y,z) then
          self.x = x
          self.y = y
          self.z = z
   else
          return false
   end
   return true
end

I'll need to read a bit more and test this first then I can finish posting what worked so anyone else with my same problem can find a solution. Once again, thanks for the resource!
SuperDyl19 #4
Posted 06 March 2018 - 05:50 AM
I can finally work on my program now. The page at https://www.lua.org/....html#ObjectSec and the few following pages have lots of great information about creating sudo-classes within lua. The code that I posted looks like this now:
Spoiler

local pos = {x=0,y=0,z=0,rotation=1}

function initialize() --part of protecting the main array from being abused. I'm still looking into this.
   return pos
end

function pos:setPos(x,y,z)
   if typeCheck(x,y,z) then
		  self.x = x
		  self.y = y
		  self.z = z
		  return true
   end
		  return false
end

local function typeCheck(x,y,z) --To check if x,y, and z are all numbers
   if  type(x) == type(y) and type(y) == type(z) and type(z) == "number" then
	  return true
   end
   return false
end

My program is named "track", so I'll use that as the name of the api. After being added to the available apis with os.loadAPI("track"), the program can be used as such:

Spoiler

a = track.initialize() --This will store the table "pos" as a
a:setPos(1,2,3) -- Setting new values for the keys x, y, and z a's table.
print(a[x]) -- Print the value of the key x on table a, which would output "1" in this case.

The changes I needed to make for this code to work were to use the colon for functions such as setPos() and to call the table as "self" within those functions. Using these two conventions allows any part of the function which edits "self" to edit the table of the variable running the function as long as the variable uses a colon when calling the function instead of a period. This is why a is first assigned a table from the program instead of being assigned a function type. This means that the colon at "a:setPos(1,2,3)" allows the function "setPos()" to edit a's table through the use of the keyword "self". Hopefully this thread helps anyone else who ends up with my same problem and as stuck as I was. This thread should now be closed.