2

Eclipse is telling me that ')' is expected near '=', but surely that can't be right? This is my code:

Animator = Class{}

function Animator:init(statictilesize = true)

    self.isTileSizeStatic = statictilesize

end

I'm so confused. I've only been using Lua for a month though, I'm more of a C++ / C# / Python guy. Maybe I'm missing something.

John Lardinois
  • 109
  • 2
  • 11

2 Answers2

3

Okay, apparently I AM a total Lua Noob / spoiled C++ Python guy.

Lua doesn't allow that. Instead, inside of the init or constructor, put:

argument = argument or defaultValue

As in,

function Animator:init(statictilesize)
    statictilesize = statictilesize or true
    self.isTileSizeStatic = statictilesize
    -- Yikes
end

Edit: I found a more stable solution, given that I require more arguments after the first.

function Animator:init(booleanstatictilesize, totalanimationstates, totalanimationframes)

    if booleanstatictilesize ~= false then
      self.isTileSizeStatic = true
    else
      self.isTileSizeStatic = false
    end

end

Sort of hacked together type casting / checking. I could be wrong, I'm a noob at all this. I never had a formal programming education. I might sound like a total idiot.

John Lardinois
  • 109
  • 2
  • 11
1

Typically what seems to be done is to define your function like normal, and if the variables you want to be optional aren't set, you set them later, and redefine your function signature to look for a table:

Animator = Class{}

function Animator:init(args)
    self.isTileSizeStatic = args.statictilesize ~= false
end

Later you call this function with this form of syntax:

Animator.init{statictilesize = false}

Both nil and false are the "falsey" conditions in Lua. All other conditions in Lua are truthy, including 0 and ''. So in order to get the functionality that when statictilesize is unset, it defaults to a true condition, you must check its inequality to false, as everything else will be true (including nil since nil is not false).

Please note that this would implicitly convert your argument into a bool

It's quite a bit different from Python.

See here for more details:

https://www.lua.org/pil/5.3.html

Additionally, if you want false to be part of the acceptable set of arguments passed to the function (or you simply don't want the argument implicitly converted to boolean) you can use the following syntax:

function Animator:init(args)
    if args.statictilesize ~= nil then self.isTileSizeStatic = args.statictilesize else self.isTileSizeStatic = true end
end
  • 1
    Thanks, that's very helpful. Sorry that I didn't look at the Lua Docs, the problem is that I don't know the terminology to search for. I never had a formal programming education so I don't really know what all these things are called, which is why I always get really frustrated trying to Google / search for solutions. I know the idea of what I want to accomplish, but not what these things are called. I'd like to get a formal education but I can't afford it. I'm disabled, penniless, and desperately attempting to learn this stuff so I can get back into the workforce and earn a better life. – John Lardinois Aug 10 '18 at 16:37
  • Totally fine; that's why there's communities like this out there to help you learn. If you can afford it though, and you are interested in Lua, I strongly reccomend getting the official reading material from Amazon or the like. It's a good read and provides insight into why things are the way they are in Lua. –  Aug 10 '18 at 16:41
  • 2
    `args.statictilesize or true` will not work for booleans. What if `statictilesize == false` ? – Egor Skriptunoff Aug 10 '18 at 19:25
  • @EgorSkriptunoff Correct, my mistake, updated the answer. –  Aug 11 '18 at 16:44
  • 1
    General Tip: Look to StackOverflow's tag wikis for links to primary references and learning aids, like [Lua](https://stackoverflow.com/tags/lua/info). You might be surprised to find that many primary references such reference manuals and specifications are quite accessible for many learners. – Tom Blodget Aug 11 '18 at 21:26