Log In  

Hello fellow Piconians !

More trouble in paradise. Here now try out this code:

n=0
if n<10 then
  ::a::
  ?"<"
end

?"*"
flip()
n=n+1
goto a

I want it to always show "<" despite the value of a Yet, this doesn't run. Now granted I understand that the label a is inside a conditional loop yet at least in the other programming languages I've worked in that shouldn't make a difference. It does here and comes back and says:

SYNTAX ERROR
NO VISIBLE LABEL 'A' FOR <GOTO>
AT LINE 11 (TAB 0)

@zep is this a problem of Pico-8 or inherent in LUA itself ?

P#117486 2022-09-15 22:59 ( Edited 2022-09-16 00:59)

It looks like Lua does not support this structure. From https://www.lua.org/manual/5.2/manual.html#3.3.4 :

A label is visible in the entire block where it is defined, except inside nested blocks where a label with the same name is defined and inside nested functions. A goto may jump to any visible label as long as it does not enter into the scope of a local variable.

P#117489 2022-09-16 00:24 ( Edited 2022-09-16 00:26)

Pretty sure this is just how it works in Lua @dw817. I don't think it's specific to goto even, just a scoping issue.

The if block creates a new scope and local variables defined inside won't be visible to the outside. Labels, I believe, behave like local variables which is why you're not able to use a as a goto target.

P#117490 2022-09-16 00:25

Hi @luchak & @jasondelaat:

So the label if inside a loop is independent of the code outside it ?

Wait, so you're saying if I define LOCAL inside an IF statement that its value is lost outside the IF statement ?

Let's see this:

function try()
local a=3
local b=4
  if a==3 then
    local b=5
    ?b
  end
  ?b
end

cls()
try()
5
4

OMy ! What unusual behavior. Languages I've used before would overwrite that upper local with the new one.

Hrrm ... There is still a great deal I do not understand about Pico-8 and LUA for that matter.

Thanks, guys !

Closing out this ticket.

P#117491 2022-09-16 00:59 ( Edited 2022-09-16 01:01)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-16 23:14:54 | 0.034s | Q:18