Page 1 of 1

Putting unit pieces in table with a loop

Posted: 08 Dec 2010, 10:08
by knorke
ok, unit animation scripts.
I want to put pieces in an array.
For now, I do like this:

Code: Select all

local pieces = {}
local wall1 = piece "wall1" table.insert (pieces, wall1)
local wall2 = piece "wall2" table.insert (pieces, wall2)
local wall3 = piece "wall3" table.insert (pieces, wall3)
local wall4 = piece "wall4" table.insert (pieces, wall4)
local wall5 = piece "wall5" table.insert (pieces, wall5)
local wall6 = piece "wall6" table.insert (pieces, wall6)
local wall7 = piece "wall7" table.insert (pieces, wall7)
local wall8 = piece "wall8" table.insert (pieces, wall8)
local wall9 = piece "wall9" table.insert (pieces, wall9)
...
Thats work but its obviously fail.

As the pieces are all named wall1 to wallover9000 i want to make the pieces table with a loop:

Code: Select all

function get_pieces()
	for i=1,28 do 
	local pname = "wall" .. i
	Spring.Echo ("adding piece: " .. pname)
	pieces[i] = {}
pieces[i] = piece pname     <- what goes here?
	end
end
I tried
pieces = piece [[pname]]
and some other things but none work. I can only guess what would be correct synthax if it is even possible?

/e
got it :roll:
track = piece (pname)
great, now i am late.

Re: Putting unit pieces in table with a loop

Posted: 08 Dec 2010, 19:25
by FLOZi

Code: Select all

local MAX_WALLS = 9001
local walls = {}
for i = 1, MAX_WALLS do
  walls[i] = piece ("wall" .. i)
end
then walls[1] will point to the piece 'wall1' of the model.

Re: Putting unit pieces in table with a loop

Posted: 08 Dec 2010, 19:38
by CarRepairer
jK has recommended I modify unitscript loader to detect the piece names for each unit script so you won't need the piece function anymore. It's somewhere on my mind's todo.

Re: Putting unit pieces in table with a loop

Posted: 08 Dec 2010, 19:44
by FLOZi
CarRepairer wrote:jK has recommended I modify unitscript loader to detect the piece names for each unit script so you won't need the piece function anymore. It's somewhere on my mind's todo.
Why?

Re: Putting unit pieces in table with a loop

Posted: 08 Dec 2010, 23:33
by knorke
thanks FLOZi but see edit, i had already figured that ( ) was needed :)
detect the piece names for each unit

Code: Select all

for k, v in pairs(Spring.GetUnitPieceMap(unitID)) do
Spring.Echo('piece ' .. k)
end
prints all piece names

Re: Putting unit pieces in table with a loop

Posted: 09 Dec 2010, 02:26
by FLOZi
Yes, that's how the gadget checks if a piece exists already, but why get rid of the piece function?

Will you just populate the environment with variables named after each piece? With the piece function you can have piece variables named differently to model pieces which might be desirable. Also isn't there the upvalue limit to worry about (or does that only apply to local vars)?

Re: Putting unit pieces in table with a loop

Posted: 09 Dec 2010, 08:50
by yuritch
The limit is for locals only afaik (at least Tobi said so when I was messing with the first battleship's lua script).

This is how I make all the pieces into vars now:

Code: Select all

	-- this populates global namespace with all the piece names
	for k, v in pairs(Spring.GetUnitPieceMap(unitID)) do
		_G[k] = v
		-- auto-hide flares
		if string.find(k, 'flare', 1, true) then
			Hide(v)
		end
	end
_G object can do wonders... :)

Re: Putting unit pieces in table with a loop

Posted: 09 Dec 2010, 10:00
by knorke
_G object can do wonders... :)
sounds interessting, what is it?
This is how I make all the pieces into vars now:
You still have to acess the pieces by names...?

Will you just populate the environment with variables named after each piece?
That is exactly what I not wanted, instead of
wall1, wall2, wall3, ..walln
i wanted wall[1], wall[2], wall[3], wall[n]

Well, maybe the array thing was confusing, the core question was How can the hardcoded string "thispiece" in
local bla= piece "thispiece"
be replaced with a variable.

:arrow: Which works by by putting ( ) around it blabla.
---
Also whats with the limits you are talking about?
So far I did thankfully not encouter them.
What does this "upvalue limit" mean and whats is its value in Spring? Google says its 60 for WoW and it limits how much memory or variables you can use or something.

Re: Putting unit pieces in table with a loop

Posted: 09 Dec 2010, 19:58
by yuritch
The limit is for local variables. You can only have so many objects declared as local. Limit is something like 50 or maybe 100, I'm not really sure.

_G object allows you to dynamically declare variables from code (and quite possibly many other things as well, I only used the var creation so far)

And as for walls, the code FLOZi posted should do exactly what you want. My code just auto-creates a variable for every piece, since I don't have thousands of similar things in my model :)

Re: Putting unit pieces in table with a loop

Posted: 09 Dec 2010, 20:33
by zwzsg
I encountered it once while editing/fixing advplayerlist.