Log In  

Hey, can one of you solve this ? It doesn't make sense to me.

This code:

function doit(x,y,d,r,c)local b,e,h,v,a=x,x-r,y if d==1then b=y e=y-r h=x elseif d==2then e=x+r elseif d==3then b=y e=y+r h=x end for i=b,e,sgn(e-b)*2do a=b-i
if(d==2or d==3)a=i-b
end
end

Runs correctly. THIS code, however, does not. All I did was move the if(d==2 or d==3) to the line above.

function doit(x,y,d,r,c)local b,e,h,v,a=x,x-r,y if d==1then b=y e=y-r h=x elseif d==2then e=x+r elseif d==3then b=y e=y+r h=x end for i=b,e,sgn(e-b)*2do a=b-i if(d==2or d==3)a=i-b
end
end

I get a SYNTAX error.

Any ideas on what the problem is ?

P#108680 2022-03-15 18:56 ( Edited 2022-03-15 20:04)

My guess is that it's either too much for one line, or it's not liking a lack of a space somewhere. I've had that happen in tweetcarts-- the code works, but Pico-8 has some kind of unpredictable quirks with formatting.

P#108682 2022-03-15 19:23
1

Pico-8's single line version of the lua if statement is temperamental. From what I can tell, it only works if it's the only control flow statement on that line. The same goes for while. This means the function statement can't precede it without a newline in between.

P#108683 2022-03-15 19:25

Ok. I'll fidget by putting a space in there and see if that helps. Was hoping it was something easy to find.

Thanks, @JadeLombax and @kimiyoribaka !

Closing this ticket ...

P#108684 2022-03-15 20:03
1

Note that this code is useful for pico-8 pre-parser issues:

printh([=[
--any code you want can go here, and the pure Lua version gets copied to the clipboard
]=],"@clip")

Your code above becomes this, which shows the issue. The shorthand if must be the first "if" on the line to be expanded properly, or else there is no "then" added after it.

function doit(x,y,d,r,c)local b,e,h,v,a=x,x-r,y if d==1 then b=y e=y-r h=x elseif d==2 then e=x+r elseif d==3 then b=y e=y+r h=x end for i=b,e,sgn(e-b)*2 do a=b-i
if(d==2 or d==3 ) then a=i-b  end 
end
end

function doit(x,y,d,r,c)local b,e,h,v,a=x,x-r,y if d==1 then b=y e=y-r h=x elseif d==2 then e=x+r elseif d==3 then b=y e=y+r h=x end for i=b,e,sgn(e-b)*2 do a=b-i if(d==2 or d==3 )a=i-b
end
end

Spaces won't solve the if quirks, unfortunately, since it needs a newline. But if this is a tweetcart, there's actually tons of space to save here. Try avoiding any then/elseifs with single line ifs, or removing that if statement entirely with an and-or assignment to a. a=d>1and i-b or b-i

P#108724 2022-03-16 11:20

Going to pause for a second here, @shy. What does this statement you wrote, a=d>1and i-b or b-1 do ? I am not understanding it at all. Is there an online tutorial explaining this ?

P#108740 2022-03-16 17:36 ( Edited 2022-03-16 17:36)
2

It is know as the «and-or trick» or shortcut.
The expression test and first or second returns first if test is true (not nil or false), otherwise returns second.
A catch: first should not be nil or false.

It is mentioned here: https://www.lua.org/pil/3.3.html

P#108750 2022-03-16 18:23

I ... sort of understand it, @merwok. Here, let me post one of the functions from recent code I wrote.

https://www.lexaloffle.com/bbs/?tid=46976

function drawbeam(x,y,d,r,c,s)local p,b,e,h,v,a=pset,x,x-r,y if d==1then b=y e=y-r h=x elseif d==2then e=x+r elseif d==3then b=y e=y+r h=x end for i=b,e,sgn(e-b)*s do a=b-i if d==2or d==3then a=i-b end for j=h-a/2,h+a/2,s do if d==0or d==2then p(i,j,c)else p(j,i,c)end end end end

What code would this be using the optimization method you described ?

P#108752 2022-03-16 18:52 ( Edited 2022-03-17 01:59)

@dw817 Here's a more visual explanation of that and-or trick. Also called a "ternary operator" in other languages.

https://mboffin.itch.io/ternary-operators

P#108761 2022-03-16 20:57

Yep people here covered it well. Basically you can treat a and b or c as similar to a ? b : c in other languages, as long as b is never false or nil. See also a = a or default_value if you want to make sure a isn't nil.

Looking at your code again though, looks like you're doing a directional thing? You can probably heavily optimize that by some bitwise code instead (or using sin/cos, though that's slower):

function doit(x,y,d,r,c)
 local b,e,h,ds,v=x,x,y,(d&2)-1 --converts d=0 1 2 3 -> -1 -1 1 1
 if(d&1>0)b=y e=y h=x --d&1 converts d=0 1 2 3 -> 0 1 0 1
 e+=ds*r
 for a=0,r,2 do --this is the same as all that i stuff?
  ...
 end
end
P#108764 2022-03-16 21:46

Off subject a bit, forgive me for tooting your own horn, @MBoffin. This is AWESOME what you did here !

https://mboffin.itch.io/pico8-educational-toolset

My favorite 💜 https://mboffin.itch.io/pico8-maze-algorithm

Back to the chase, @shy. I am seeing what you are doing here, while I understand what the code does, I am not understanding WHY it does - how it integrates. I need to tackle THAT in my brain first before I can go much further. Let me apply what you've done.

P#108783 2022-03-17 03:02 ( Edited 2022-03-17 03:17)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 16:38:15 | 0.071s | Q:30