"Unexpected Symbol near if" error

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Jam_Toad
Prole
Posts: 2
Joined: Sun Sep 04, 2022 2:28 am

"Unexpected Symbol near if" error

Post by Jam_Toad »

Howdy!! I am kinda brand new here and I just had a really quick question.

How come this code errors? I have used this same code in other engines/frameworks and it works fine.

Code: Select all

local foo = if true then "bar" else "hello world"
Is this not a valid short circuit in Love2D?

Thank you and have a good one!
Last edited by Jam_Toad on Mon Sep 05, 2022 5:40 am, edited 1 time in total.
User avatar
BrotSagtMist
Party member
Posts: 655
Joined: Fri Aug 06, 2021 10:30 pm

Re: "Unexpected Symbol near if" error

Post by BrotSagtMist »

A shortcut for what?
And also, obviously your other engines use a different programming language, this code is utter gibberish.
But why it fails? You start an assignment and then open a block with a codeword leaving the assignment hanging in the air id say. Neither makes the if block any sense, "bar", like youre just throwing a string to the language without telling it what to do with it.
Youre probably looking for
local foo= true and "bar" or "hello"
That would be the common lua syntax.
obey
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: "Unexpected Symbol near if" error

Post by ReFreezed »

Hello and welcome to the forums.

This is what Lua sees, and what errors the code has:

Code: Select all

local foo =  -- Missing an expression after '='.
if true then
	"bar"  -- Invalid statement.
else
	"hello world"  -- Invalid statement.
end
In addition to BrotSagtMist's solution using and/or, see a relevant page on the lua-users wiki, and the "Ternary operators" section on this page.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Jam_Toad
Prole
Posts: 2
Joined: Sun Sep 04, 2022 2:28 am

Re: "Unexpected Symbol near if" error

Post by Jam_Toad »

BrotSagtMist wrote: Sun Sep 04, 2022 12:15 pm A shortcut for what?
And also, obviously your other engines use a different programming language, this code is utter gibberish.
But why it fails? You start an assignment and then open a block with a codeword leaving the assignment hanging in the air id say. Neither makes the if block any sense, "bar", like youre just throwing a string to the language without telling it what to do with it.
Youre probably looking for
local foo= true and "bar" or "hello"
That would be the common lua syntax.
My apologies, Roblox lets me do it. I realize they use a custom version of Lua called Luau, they must have added the feature.
NishiyamaPedro
Prole
Posts: 1
Joined: Sat Jun 10, 2017 12:24 am

Re: "Unexpected Symbol near if" error

Post by NishiyamaPedro »

You can do it like this.

Code: Select all

local foo = true and "bar" or "hello world"
There are other ways to get the same result, if you are interested you can read here
http://lua-users.org/wiki/TernaryOperator
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests