Author Topic: Tip: Function at line has more than 60 upvalues  (Read 12719 times)

0 Members and 1 Guest are viewing this topic.

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #15 on: September 21, 2016, 11:00:54 AM »
He already did above, it takes 10 lines :)

He meant to name 'more' solutions if he wanted.

There's a lot of solutions though, I did post the most common one that is easy to understand for both beginners and experts in the Lua world.

Your solution would work too, but it takes a relative amount of screen space while coding and it is easy to be lost if you are searching for a precise variable, this is not convenient at all, even for a "crazy" programmer.  :P

Your solution is good, but like Zefk I like to give my variables good names. I would not use an array for good variable naming. I also like to see my variable list and not a few listed arrays for name changes. Your solution depends on many factors or whether the variables are related to each other. The table solution is flexible. Especially, when you are designing your code. I will not know how many variables I will end up with in a complex project.

Oh then it is simple, there is a solution that looks almost the same as I did above

Code: (lua) [Select]
local vars = { name1 = value, name2 = value, etc, etc}
 
local function Update(event)
  for i = 1, #vars do
    print(vars[i])
  end
end

It does what you want, it print each value, because theorically, "name1" is the same thing as vars[1]

Diarandor

  • Hero Member
  • *****
  • Posts: 1062
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #16 on: September 21, 2016, 01:47:03 PM »
Maybe I am wrong, but I think in this case it should be:
Code: (Lua) [Select]
print(vars["name"..i])
Another way to do the loop is using the Lua "pairs" function and print both things, key and value, on each line (I think the order for the loop is random, or at least chaotic, but I am not sure). This would allow to have keys that are not of the form "nameX".
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
Re: Tip: Function at line has more than 60 upvalues
« Reply #17 on: October 04, 2016, 05:01:01 AM »
I normally use arrays when things are related to the same thing.

Lets say I have a image with a "1" on it. I might want to draw many at once and manipulate each one to a different position.

I would use an array like this

Code: ( lua) [Select]
local num1_place = {}

if num1_place[1] == false then
  blah_img:draw(screen, 23)
end

if num1_place[2] == false then
  blah_img:draw(screen, 13)
end

etc

instead of listing bunch of variables

Code: ( lua) [Select]
local num1_place_1 = false
local num1_place_2 = false
local num1_place_3 = false
local num1_place_4 = false
local num1_place_5 = false
local num1_place_6 = false
local num1_place_7 = false
local num1_place_8 = false
local num1_place_9 = false
local num1_place_10 = false
local num1_place_11 = false
local num1_place_12 = false
local num1_place_13 = false
local num1_place_14 = false
local num1_place_15 = false

I guess I could of used an array, but wanted to be picky with my variable names.

llamazing

  • Full Member
  • ***
  • Posts: 221
    • View Profile
Re: Tip: Function at line has more than 60 upvalues
« Reply #18 on: October 04, 2016, 05:27:44 AM »
I normally use arrays when things are related to the same thing.

Lets say I have a image with a "1" on it. I might want to draw many at once and manipulate each one to a different position.

I would use an array like this...
Ouch! Why are you drawing each number individually when you can make your life easier and use a text_surface to do the same thing?

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
Re: Tip: Function at line has more than 60 upvalues
« Reply #19 on: October 04, 2016, 06:08:01 AM »
Most of my fonts are not in any font package format. They are all bitmap fonts divided on separate png files. Plus I like doing it this way.  Well anyway, this is a topic for a different post.