Reading a screen pixel

I don't think it is possible right? To read a pixel from the current screen render?

Not sure if there is a hidden function that allows you to read the screen buffer.
BUT you can read the color value of single pixels in a specific sprite.

I used it myself in this tinker:

:embed https://microstudio.io/TinkerSmith/petridish/#transpiler


There, instead of using a large list to keep track of the data I used the sprite itself as an 2 dimensional array :)

But be careful, since it is an undocumented option one might never know if it will stay for the future.

Live Long and Tinker

@Tinker, Thanks for the details. In my case I would somehow need to transfer the status of the screen to a sprite. I will be rendering thick lines to the screen at various positions and angle, so it is not easy to translate this into an array. I will add reading/writing to the screen as a suggestion.

Hmmm, good idea!

Sounds like you should do what I did, just use a screen sized sprite as the screen itself.

I have not tried all the sprite object options yet.
Just have a snoop, there are plenty more.

  loaded = [native function]
  setFrame = [native function]
  getFrame = [native function]
  cutFrames = [native function]
  saveData = [native function]
  setCurrentFrame = [native function]
  clone = [native function]
  resize = [native function]
  load = [native function]
  copyFrom = [native function]
  clear = [native function]
  addFrame = [native function]

You could use the frames as double buffers... hehe ... or a second sprite if the clone works
Also quite interesting are the frame functions

  canvas = [object]
  context = [object]
  clone = [native function]
  getContext = [native function]
  getCanvas = [native function]
  setPixel = [native function]
  erasePixel = [native function]
  getRGB = [native function]
  clear = [native function]
  resize = [native function]
  load = [native function]
  copyFrom = [native function]

I can't find most of the above in the official documentation.Are these hidden options?

Not really hidden, but maybe subject to change. One or the other has been discussed or mentioned on the Discord channel. Just run the global command on the command line after you started a game and you can see all declared objects, functions and variables. From there you can dig deeper.

For example, global shows beside others the 'sprites' object (if a sprite is defined).
Now you enter sprites and get a list of all the sprites that are declared, each showing up as an object again.

Then you dig deeper down the rabbits hole, enter sprites.xxx , where xxx is the sprite in question. You see that in the source code for above example, in my case it was sprites.grid

rinse, wash, repeat

Now you see functions,YEAH = TINKERTIME Not documented, so it's trial&error using them. Loving that part, haha. Since screen and sprite routines are mainly canvas related some of them should make sense if you are familiar with that one.

Maybe don't use those for a commercial game, but have fun exploring.

Live Long and Tinker

Ah great information. I actually read this part in the document --> "other fields and native methods may currently seem available when you inspect a sprite object in the console" and thought that I could just type the name of the sprite in the console to get it's information. For instance:

CONSOLE > hero

Now I understand that the sprites belong to a list internally.

Oh, and now that I have learned about the above I can also see that I can do very cool things such as extend properties of a sprite:

// Add a lives variable


You got it :)

To save typing just assign the object to a variable and off you go.

myHero = sprites.hero

For the newbies like me that are not that familiar with all this object stuff ;)

getCol = function(xt,yt) return sprites.grid.frames[0].getRGB(xt,yt) end

how would you change the above function to being able to pass a sprite name to the function like below.

getCol = function(spriteName,xt,yt) return sprites.spriteName.frames[0].getRGB(xt,yt) end

and call it with getCol("grid",0,0) all my attempts have failed.

name = "mySprite"  

... sprites[name].frames[0].getRGB ...

Try this :)

On phone, so can't check, but in your case it should look like this:

getCol = function(spriteName,xt,yt) return sprites[spriteName].frames[0].getRGB(xt,yt) end

Thank you, that did it!

Post a reply



Validate your e-mail address to participate in the community