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

Instance API!

Started by LeDark Lua, 13 June 2015 - 11:08 AM
LeDark Lua #1
Posted 13 June 2015 - 01:08 PM
Did you ever wanted to created two things with the same code?

Or you want to have OOP ( object oriented programming ) in your game/OS or program?

Well this API does just that!


InstanceAPI

The InstanceAPI is easy to use and powerful! You can create objects, create instances and destroy them.


InstanceAPI pastebin:



pastebin get TPvfZRMD instance

Example program



Get this file if you wan't:

pastebin get 5jS6b73w test

Code:

Spoiler

os.loadAPI("instance")

local w, h = term.getSize()
local running = true

objButton = instance.objectAdd(w/2, h/2, colors.orange)--Create object objButton

function objButton:create()--objButton creation code
  self.width=6
  self.heigth=3
  self.x=self.x-self.width/2
  self.y=self.y-self.heigth/2
  self.text="Random: "..tostring(math.random(1000))
  self.textLen=string.len(self.text)
  self.rxVel=math.random(-2, 2)
  self.ryVel=math.random(-2, 2)
end

function objButton:update()--objButton step code
  self.x=self.x+self.rxVel
  self.y=self.y+self.ryVel
  if(self.x<=1 or self.x>=w-self.width+1) then self.rxVel=-self.rxVel end
  if(self.y<=1 or self.y>=h-self.heigth+1) then self.ryVel=-self.ryVel end
end

function objButton:draw()--objButton draw code
  for xx=self.x, self.x+self.width do
   for yy=self.y, self.y+self.heigth do
	  paintutils.drawPixel(xx, yy, colors.orange)
	  term.setTextColor(colors.black)
	  term.setCursorPos((self.x+self.width/2)-self.textLen/2, (self.y+self.heigth/2))
	  term.write(self.text)
	end
  end
end

local function screenClear(col)
  term.setBackgroundColor(col)
  term.clear()
end

function main()
  screenClear(colors.white)
  local tmp=instance.create(objButton)
  local count=0
  instance.updateAll()
  while running do
	screenClear(colors.white)

	--After everything is done, update the instance's!
	instance.updateAll()
	count=count+1
	if(count>30) then
	  count=0
	  instance.create(objButton, math.random(w), math.random(h))
	  if(instance.exists(tmp)) then
		instance.destroy(tmp)
	  end
	end
	os.sleep(.1)
  end
end

main()

Yup as you can see, you only suply the object, but create Instances with unique id's and unique code!



Functions


Spoiler

If you want to add the object:

instance.objectAdd(x, y, color) –x y color are not required, if you suply the instance function!

Returns: objects table


If you want to create a instance:

instance.create(object, x, y, color) –x y color are not required, if you suply the object function!

Returns: instances table


If you want to check if the instance exists:

instance.exists(instance id)

Returns: true if exists, false if not


If you want to know how many instances are in the world:

instance.count()

Returns: INTEGER | how many instances are created!


If you want to destroy a instance:

instance.destroy(instance id) –If you are destroying the instance inside it, do: 'self.id' this will get its id and destroy itself

Returns: nothing


This functon will update all instances:

instance.updateAll()–Updates all instances!

Returns: nothing



Special thanks to


They helped me to fix the bugs :)/>


If you find this helpful, please comment!

And if you will use it in your projects, do whatever you wan't credit me or not, it doesn't matter!
Edited on 21 July 2015 - 12:15 PM
flaghacker #2
Posted 13 June 2015 - 04:18 PM
Why return the id, and not the object itself?

I don't think instace in a good name, as it is a synonym of object in real object-oriented languages. Maybe class is a better name?
Edited on 13 June 2015 - 02:18 PM
LeDark Lua #3
Posted 13 June 2015 - 04:23 PM
flaghacker

First of all: it returns objects and/or instances TABLE!
Second: named it instances because this API works as it says!

Reposted the InstanceAPI pastebin and re-explained functions. Plus new functions: instance.exists and instance.count!
Edited on 13 June 2015 - 02:30 PM
LeDark Lua #4
Posted 13 June 2015 - 08:33 PM
Hey guys, I fixed the instance.destroy and instance.updateAll functions!
flaghacker #5
Posted 13 June 2015 - 09:29 PM
Hey guys, I fixed the instance.destroy and instance.updateAll functions!

What do they do? The documentation doesn't say anything about it. It also doesn't say what the api actually does. Is it a gui api? Or is the first code just an example?
LeDark Lua #6
Posted 14 June 2015 - 05:57 AM
Ok, im literally crying because you don't know what Instance is… ok…
UPDATED THE THREAD!
Edited on 14 June 2015 - 04:12 AM
biggest yikes #7
Posted 14 June 2015 - 11:58 PM
Ok, im literally crying because you don't know what Instance
Woah, what?
LeDark Lua #8
Posted 15 June 2015 - 09:57 AM
Ok I see no one has interests in this thread, ok everyone just comes, checks what comments the thread has, comment the creator for what a mistake he did and leave without looking at the actual API….
com_kieffer #9
Posted 15 June 2015 - 05:19 PM
I think most people are just confused as to what your API is supposed to do.

This doesn't help either.

Ok, im literally crying because you don't know what Instance is… ok…
UPDATED THE THREAD!

It just makes you look smug and superior. If you want people to use your API you need to explain it ! Your documentation tells me what the individual functions do but it doesn't tell me why I need your API. Give people a compelling use case. This applies to may projects here. Writing the code is only half the battle !
biggest yikes #10
Posted 15 June 2015 - 07:02 PM
It looks like I might have underestimated this API. It looks like it could be a cool rendering helper. I probably won't use this for my programs, but I suppose there's one bad thing about it: the render lag. When the instance is refreshed, it takes a small amount of time to reload it, causing a moment of simply white.
LeDark Lua #11
Posted 15 June 2015 - 08:56 PM
com_kieffer everything is explained at the top of the thread… I dont know what to say else.

Atenefyr ok im going to rewrite the rendering script, I will try to render them at the same time, not loop them.
biggest yikes #12
Posted 15 June 2015 - 09:21 PM
com_kieffer everything is explained at the top of the thread… I dont know what to say else.

Atenefyr ok im going to rewrite the rendering script, I will try to render them at the same time, not loop them.
There's a quote button if you want to make a reference to other people. Just press the quote button on the bottom-right hand side of the post.
Edited on 15 June 2015 - 07:21 PM
LeDark Lua #13
Posted 19 June 2015 - 07:57 AM
It looks like I might have underestimated this API. It looks like it could be a cool rendering helper. I probably won't use this for my programs, but I suppose there's one bad thing about it: the render lag. When the instance is refreshed, it takes a small amount of time to reload it, causing a moment of simply white.
As I saw that my API is slow, I tried various ways to fix that, and I did. Now the refreshing is faster!
LeDark Lua #14
Posted 05 July 2015 - 06:54 PM
Ok this is not so usefull in here then, wel nvm, this is the last post :)/>
biggest yikes #15
Posted 07 July 2015 - 04:05 PM
Report your topic if you want a moderator to close it
LeDark Lua #16
Posted 07 July 2015 - 06:42 PM
I might just leave it if somebody needs it …
LeDark Lua #17
Posted 21 July 2015 - 02:12 PM
Guys best update is here. The instance API is faster then ever before!

pastebin get TPvfZRMD instance

Thanks for using it :)/>