Log In  

Many programming languages allow you to make direct changes to arrays at a memory level using a command called VARPTR.

https://www.youtube.com/watch?v=sdZahXk-e9Y

What I would like to do is:

a=""
for i=0,8191 do
  a=a.."*"
end

memcpy(24576,varptr(a),8192)
repeat
  flip()
until forever

Is this possible to do in PICO ?

P#31055 2016-10-16 14:55 ( Edited 2017-08-20 13:28)

I don't think Lua or pico 8 has pointers.

P#31068 2016-10-16 17:38 ( Edited 2016-10-16 21:38)

Anyone else managed to do it ? I'm not discounting you, Rytrasmi, just want to be certain there really is no way to read or write directly with PEEK or POKE to variable and/or string storage.

P#31073 2016-10-16 19:38 ( Edited 2016-10-16 23:38)

Anyone? Anyone? Bueller?

But seriously, peek and poke are sandboxed, meaning that peeking outside the 0x0000 - 0x7FFF range of pico RAM gives 0 and poking outside the range returns an error. Try it. These are not even real RAM addresses, in that the OS uses actual RAM at these addresses for other things.

The underlying C the drives Lua does use pointers. However, these are not exposed to Lua users.

P#31131 2016-10-17 21:31 ( Edited 2016-10-18 01:32)

Well, it would just be so darned convenient to be able to access all the variables in memory. In fact, I can't think of any programming languages except this one which forbid it, Rytrasmi.

P#31153 2016-10-18 11:11 ( Edited 2016-10-18 15:11)

The whole point of pointers is to make a passed variable mutable/editable. But tables in lua already do that, BAM!

If you need access to a variable that belongs to another function, then it probably shouldn't belong to that function.

P#31156 2016-10-18 11:41 ( Edited 2016-10-18 18:58)

Lots of high level languages don't have pointers or pointer arithmetic. Python and Java come to mind, the latter being the most widely used language in the world. JavaScript also lacks pointers any it's everywhere.

P#31158 2016-10-18 12:39 ( Edited 2016-10-18 16:41)

Mostly I wanted to use MEMCPY which would be faster than FOR/NEXT to copy arrays to memory.

Now if there's a way as quick as MEMCPY to copy an array to memory that is not MEMCPY, I'd be interested in it.

P#31181 2016-10-18 17:00 ( Edited 2016-10-18 21:00)

Foreach might be faster than a plain for loop. Depends how it was implemented. Also I think you can use memset, as long as you're filling a range of memory with the same value. The manual shows the syntax for each of these.

How fast do you need it to be?

P#31187 2016-10-18 17:31 ( Edited 2016-10-18 21:31)

Do you remember when PROT showed light speed travel ?

https://youtu.be/vByR8tGNFfM?t=133

I'd like it ... about that fast. :)

Failing that, as the scientist said. Perhaps a demonstration ?

A challenge - who can transfer a standard array - or list, say size 8192, filled with random numbers 0-255, fastest to memory location, oh, I don't know - 24576 ?

And the same ability to read back from 24576, 8192-bytes to the same standard array or list.

Someone mentioned earlier that it is the limitations of PICO-8 that make it so brilliant and beautiful.

With - the limitations of PICO-8, what can be accomplished in this ?

P#31190 2016-10-18 17:55 ( Edited 2016-10-18 22:03)
1

I just want to clarify terminology because this is the first page that comes up in Google for 'PICO-8 pointer' and the discussion above makes some assumptions that might confuse new programmers:

PICO-8 has pointers. In fact, almost every variable is a pointer to an object. Here's an example. If you write:

local a = {true}
local b = a
b[1] = false
? a[1]

then you'll find that a[1] is now false, because a and b are pointers to the same object. Java and Python work the same way.

What PICO-8 lacks compared to C are pointer arithmetic and a weak type system. Although this means that certain low-level tricks can't be used, it also means that PICO-8 programs don't crash due to bugs in those programs. So, it is probably a good tradeoff. There are a few cases where you can use direct memory operations such as peek(), poke(), memcpy(), and memset() to set system state such as the palette and pixel values. But you can't use them to change the values of variables or data structures. There are some cases where it would be really convenient to do so, but...memory safety against corruption and crashes is a pretty good benefit to get from not allowing that.

P#43481 2017-08-20 09:28 ( Edited 2017-08-20 13:28)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 12:50:57 | 0.011s | Q:24