This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jmarko's profile picture

More Than 60 Upvalues?

Started by jmarko, 24 July 2013 - 02:24 PM
jmarko #1
Posted 24 July 2013 - 04:24 PM
Hi!I'm make a 2D top-down game with nitrogen's Gameutil (and NPaint Pro).I have a function named draw() and here is this code:

function drawmap()
gameutils.clearBuffer(colours.black)
if map == 0 then
gameutils.writeToBuffer(map0)
--Walls
gameutils.writeToBuffer(obj2)
gameutils.writeToBuffer(obj3)
gameutils.writeToBuffer(obj4)
gameutils.writeToBuffer(obj5)
gameutils.writeToBuffer(obj6)
--Houses
gameutils.writeToBuffer(obj7)
gameutils.writeToBuffer(obj8)
gameutils.writeToBuffer(obj9)
gameutils.writeToBuffer(obj10)
gameutils.writeToBuffer(obj11)
gameutils.writeToBuffer(obj12)
gameutils.writeToBuffer(obj13)
gameutils.writeToBuffer(obj14)
gameutils.writeToBuffer(obj15)
gameutils.writeToBuffer(obj16)
gameutils.writeToBuffer(obj17)
gameutils.writeToBuffer(obj18)
gameutils.writeToBuffer(obj19)
gameutils.writeToBuffer(obj20)
gameutils.writeToBuffer(obj21)
gameutils.writeToBuffer(obj22)
gameutils.writeToBuffer(obj23)
gameutils.writeToBuffer(obj24)
gameutils.writeToBuffer(obj25)
--NPCS
gameutils.writeToBuffer(obj26)
--Player
gameutils.writeToBuffer(obj1)
elseif map == 1 then
gameutils.writeToBuffer(map0)
--Player
gameutils.writeToBuffer(obj1)
--Other
gameutils.writeToBuffer(obj27)
gameutils.writeToBuffer(obj28)
elseif map == 2 then
gameutils.writeToBuffer(map0)
--Walls
gameutils.writeToBuffer(obj2)
gameutils.writeToBuffer(obj3)
gameutils.writeToBuffer(obj32)
gameutils.writeToBuffer(obj31)
gameutils.writeToBuffer(obj30)
gameutils.writeToBuffer(obj29)
--Houses
gameutils.writeToBuffer(obj33)
gameutils.writeToBuffer(obj34)
gameutils.writeToBuffer(obj35)
gameutils.writeToBuffer(obj36)
gameutils.writeToBuffer(obj37)
gameutils.writeToBuffer(obj38)
gameutils.writeToBuffer(obj39)
gameutils.writeToBuffer(obj40)
gameutils.writeToBuffer(obj41)
gameutils.writeToBuffer(obj42)
gameutils.writeToBuffer(obj43)
gameutils.writeToBuffer(obj44)
gameutils.writeToBuffer(obj45)
gameutils.writeToBuffer(obj46)
gameutils.writeToBuffer(obj47)
gameutils.writeToBuffer(obj48)
gameutils.writeToBuffer(obj49)
gameutils.writeToBuffer(obj50)
gameutils.writeToBuffer(obj51)
gameutils.writeToBuffer(obj52)
gameutils.writeToBuffer(obj53)
gameutils.writeToBuffer(obj54)
gameutils.writeToBuffer(obj55)
gameutils.writeToBuffer(obj56)
gameutils.writeToBuffer(obj57)
gameutils.writeToBuffer(obj58)
gameutils.writeToBuffer(obj59)
gameutils.writeToBuffer(obj60)
gameutils.writeToBuffer(obj61)
gameutils.writeToBuffer(obj62)
gameutils.writeToBuffer(obj63)
gameutils.writeToBuffer(obj64)
gameutils.writeToBuffer(obj65)
gameutils.writeToBuffer(obj66)
gameutils.writeToBuffer(obj67)
gameutils.writeToBuffer(obj68)
gameutils.writeToBuffer(obj69)
gameutils.writeToBuffer(obj70)
gameutils.writeToBuffer(obj71)
gameutils.writeToBuffer(obj72)
gameutils.writeToBuffer(obj73)
gameutils.writeToBuffer(obj74)
gameutils.writeToBuffer(obj75)
gameutils.writeToBuffer(obj76)
gameutils.writeToBuffer(obj77)
gameutils.writeToBuffer(obj78)
gameutils.writeToBuffer(obj79)
gameutils.writeToBuffer(obj80)
gameutils.writeToBuffer(obj81)
gameutils.writeToBuffer(obj82)
gameutils.writeToBuffer(obj83)
gameutils.writeToBuffer(obj84)
--Player
gameutils.writeToBuffer(obj1)
end
gameutils.drawBuffer()
end
When i runned my game this error code showed:

bios:338: [string "test1.lua"]:273: function
line 195 has more than 60 upvalues

How can i fix this?
Lyqyd #2
Posted 24 July 2013 - 04:46 PM
Use tables instead of putting the index of each object as part of the variable name.
jmarko #3
Posted 24 July 2013 - 05:08 PM
Use tables instead of putting the index of each object as part of the variable name.
Sorry I'm beginner but How?
I need an example
Bubba #4
Posted 24 July 2013 - 05:16 PM
Rather than having all of your objects stored in different variables, store them in a single table. It will shorten things quite a bit.


local someTable = {
  obj1 = createObject(); --#Somehow create your objects
  obj2 = createObject();
  map0 = createObject();
}

for index,object in pairs(someTable) do
  gameutils.writeToBuffer(object)
end

See more on tables here
jmarko #5
Posted 24 July 2013 - 05:24 PM
Rather than having all of your objects stored in different variables, store them in a single table. It will shorten things quite a bit.


local someTable = {
  obj1 = createObject(); --#Somehow create your objects
  obj2 = createObject();
  map0 = createObject();
}

for index,object in pairs(someTable) do
  gameutils.writeToBuffer(object)
end

See more on tables here
And what i need to do when i put in the table my obj1 obj2 etc. variables are created when i start the game and i don't want to repeat this step.
Bubba #6
Posted 24 July 2013 - 06:00 PM
And what i need to do when i put in the table my obj1 obj2 etc. variables are created when i start the game and i don't want to repeat this step.

Well you really should post the rest of your code. I have no idea how you are creating your variables when you start the game and as such it's difficult to give you any help.

Either post the code, or post an abbreviated but fully implemented form of your code and I would be happy to peruse it.
Lyqyd #7
Posted 24 July 2013 - 06:56 PM
Essentially, instead of:


local obj1 = "whatever"
local obj2 = "whatever else"
local map1 = "thing"

do this:


local obj = {}
obj[1] = "whatever"
obj[2] = "whatever else"
local map = {}
map[1] = "thing"

This gives you the added benefit of iterating the entries in the table in your rendering pass rather than needing a line of code for each and every thing you want to render.
jmarko #8
Posted 25 July 2013 - 01:11 AM
Essentially, instead of:


local obj1 = "whatever"
local obj2 = "whatever else"
local map1 = "thing"

do this:


local obj = {}
obj[1] = "whatever"
obj[2] = "whatever else"
local map = {}
map[1] = "thing"

This gives you the added benefit of iterating the entries in the table in your rendering pass rather than needing a line of code for each and every thing you want to render.
Thanks