View topic - To Game-Devs: The "n" entry in array-like tables was removed



All times are UTC + 1 hour


Post new topic Reply to topic  [ 54 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: 21 Jun 2011, 19:55 
Spring Developer
User avatar

Joined: 22 Sep 2007, 08:51
what was done
All array-like Lua tables that are created/filled by the engine, got the "n" member removed, which contained the arrays size. This is as of now in current spring master, and will be in the next release.

necessary changes
Instead of reading the "n" member of tables to get their size, you have to use the # operator now.
This change is backwards compatible; if you apply it to your Lua, it will also work in current release.
-> do it asap! ;-)
example:
Code:
-      for i=1,ud.weapons.n do
+      for i=1,#ud.weapons do

reasoning
Your Lua code will be a bit cleaner, and more portable, and in theory it would be a bit faster (unnoticeable in practice).

how to test
windows installer: http://springrts.com/dl/buildbot/defaul ... 6b01f8.exe
linux: git clone git://github.com/hoijui/spring.git

related commits
https://github.com/spring/spring/commit ... 9779c9f7a9
https://github.com/spring/spring/commit ... a810fa9336
https://github.com/spring/spring/commit ... 9c5c883d60
https://github.com/spring/spring/commit ... 014b9209f7

affected Lua stuff
No guarantee for completeness:
Code:
LayoutButtons
CommandFallback
AllowCommand
GetUnitPieceList
GetUnitPieceInfo
UnitDef.buildOptions
UnitDef.weapons
UnitDef.sounds.* =>
UnitDef.sounds.select
UnitDef.sounds.ok
UnitDef.sounds.arrived
UnitDef.sounds.build
UnitDef.sounds.repair
UnitDef.sounds.working
UnitDef.sounds.underattack
UnitDef.sounds.cant
UnitDef.sounds.activate
UnitDef.sounds.deactivate
GetSelectedUnits
GetActiveCmdDescs
GetConsoleBuffer
GetKeyBindings
GetActionHotKeys
GetGroupUnits
GetGroupUnitsSorted
GetGroupUnitsCounts
GetUnitCmdDescs
UnpackU8
UnpackU16
UnpackU32
UnpackS8
UnpackS16
UnpackS32
UnpackF32
WeaponDef.hitSound
WeaponDef.fireSound
GetPlayerRoster
GetPressedKeys
GetGlobalTexNames
DirList
SubDirs
GetAllyTeamList
GetTeamList
GetPlayerList
GetTeamStatsHistory
GetAllUnits
GetTeamUnits
GetTeamUnitsSorted
GetTeamUnitsByDefs
GetUnitsInRectangle
GetUnitsInBox
GetUnitsInCylinder
GetUnitsInSphere
GetUnitsInPlanes
GetUnitIsTransporting
GetUnitCommands
GetFactoryCommands
GetCommandQueue


edit:
added:
GetAllyTeamList
GetTeamList
GetPlayerList
GetTeamStatsHistory
GetAllUnits
GetTeamUnits
GetTeamUnitsSorted
GetTeamUnitsByDefs
GetUnitsInRectangle
GetUnitsInBox
GetUnitsInCylinder
GetUnitsInSphere
GetUnitsInPlanes
GetUnitIsTransporting
GetUnitCommands
GetFactoryCommands
GetCommandQueue
removed:
GetSelectedUnitsSorted
GetSelectedUnitsCounts


Last edited by knorke on 07 Mar 2012, 02:56, edited 5 times in total.
unsticky, its been a year. http://springrts.com/wiki/EngineChangelogForContentDevs


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 20:03 
MC: Legacy & Spring 1944 Developer
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
Can a mod/admin turn this into an announcement or at least sticky it? 8)

Thanks for the heads up.


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 21:09 
Zero-K Developer
User avatar

Joined: 10 Sep 2008, 02:11
Location: Bayesian space monkeys
Yay, cleaner code! :mrgreen:


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 22:10 
Spring Developer
User avatar

Joined: 22 Sep 2007, 08:51
Kloot made an other related commit, removing more n's (and re-adding two falsely removed ones). first post is updated.


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 22:11 
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
can you please give the link to a (win32) .exe with those changes?


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 22:25 
Lua Coder

Joined: 12 Jul 2009, 17:57
Good, probably 100 hidden bugs fixed.

I haven't about never remembered this 'n' entry and few times had to learn the hard way.


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 22:51 
Content Developer
User avatar

Joined: 13 Jan 2005, 00:46
Location: You are going to die anyway, hurry up and do it.
How long until release? Why is this being done so suddenly? It may break a good deal of projects


Top
 Offline Profile  
 
PostPosted: 21 Jun 2011, 23:24 
User avatar

Joined: 22 Feb 2006, 01:02
Location: cheap kitchen
base\springcontent.sdz\LuaGadgets\Gadgets\unit_script.lua
this file uses weapons.n in two places
->lua unit scripts do not work on units with a weapon

http://springrts.com/mantis/view.php?id=2492


Top
 Offline Profile  
 
PostPosted: 22 Jun 2011, 08:08 

Joined: 09 Jun 2011, 11:17
Good change, I personally found it weird anyhow asking array.getn() and not getting the real amount of elements if there is a small gap somewhere.


Top
 Offline Profile  
 
PostPosted: 22 Jun 2011, 10:07 
Server Owner & Developer
User avatar

Joined: 19 May 2006, 18:13
Location: Brno, Czech rep., EU, Terra, Sol, Orion arm, Milky way, Virgo supercluster
Note you cannot use # array size in keyed tables.
For those, you must enumerate and count all elements in lula.


Top
 Offline Profile  
 
PostPosted: 22 Jun 2011, 16:35 
MC: Legacy & Spring 1944 Developer
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
Licho wrote:
Note you cannot use # array size in keyed tables.
For those, you must enumerate and count all elements in lula.


Also note that if those are returned by the engine, they should still contain the .n


Top
 Offline Profile  
 
PostPosted: 22 Jun 2011, 19:27 
Server Owner & Developer
User avatar

Joined: 19 May 2006, 18:13
Location: Brno, Czech rep., EU, Terra, Sol, Orion arm, Milky way, Virgo supercluster
I still see it as pretty big flaw of lua internals. .. i bet its not hard to maintiain simple counter for keyeyd entries :(
Lua itself shoudl support #


Top
 Offline Profile  
 
PostPosted: 23 Jun 2011, 03:46 
Spring Developer

Joined: 31 May 2009, 23:08
@knorke:

hoijui fixed it:

https://github.com/spring/spring/commit ... b12b55daa5

(maybe if others want to see how to fix it...)


Top
 Offline Profile  
 
PostPosted: 23 Jun 2011, 07:59 
Moderator
User avatar

Joined: 26 Oct 2007, 15:21
What if:
Code:
unitCounts.n = nil 

?


Top
 Online Profile  
 
PostPosted: 23 Jun 2011, 15:46 
MC: Legacy & Spring 1944 Developer
User avatar

Joined: 29 Apr 2005, 00:14
Location: #moddev - join it!
Any widgets / gadgets counting entries and assigning n themselves should be left alone, if that is what you're asking?


Top
 Offline Profile  
 
PostPosted: 24 Jun 2011, 02:24 
Moderator

Joined: 12 Oct 2007, 08:24
Code:
unitCounts.n = nil 
I came across this too. It seems to be a workaround to remove the n element from the table so pairs can be used without checking for n. It could be removed with 0.83 or you could rewrite the use of pairs to use #table because it seems stupid to use pairs on an iterable table.


Top
 Offline Profile  
 
PostPosted: 24 Jun 2011, 18:07 
Kernel Panic Co-Developer
User avatar

Joined: 16 Nov 2004, 13:08
Or use the ipairs that was made just for that.


Top
 Offline Profile  
 
PostPosted: 25 Jun 2011, 05:54 
Moderator

Joined: 12 Oct 2007, 08:24
Iirc jk's lua page says ipairs is slower than "for i=1, #table do". I haven't come across a table where the #table approach fails to be equivalent to ipairs.


Top
 Offline Profile  
 
PostPosted: 05 Jul 2011, 16:12 
Moderator
User avatar

Joined: 19 Sep 2006, 21:48
Quote:
#t is 1.0406504065041 times slower than t.n for an array of size 100
#t is 2.0826446280992 times slower than t.n for an array of size 1000
#t is 2.8423236514523 times slower than t.n for an array of size 10000
#t is 4.6935483870968 times slower than t.n for an array of size 100000
#t is 5.2921810699588 times slower than t.n for an array of size 1000000


Imo, we don't have to worry about performance, since the API usually returns small arrays.

Code:
function test(size)
  local iterations = 10000000
  local t = {}
  t.n = size
  for i=1, t.n do
    t[i] = 2
  end
  local t1 = os.clock()
  for i=1, iterations do
    local _ = #t
  end
  local t2 = os.clock()
  local lengthOperatorTime = t2 - t1


  local t1 = os.clock()
  for i=1, iterations do
    local _ = t.n
  end
  local t2 = os.clock()
  local dotNTime = t2 - t1
 
  print("#t is "..lengthOperatorTime/dotNTime.." times slower than t.n for an array of size "..size)
end

test(100)
test(1000)
test(10000)
test(100000)
test(1000000)


Top
 Offline Profile  
 
PostPosted: 06 Jul 2011, 05:56 
Evolution RTS Developer
User avatar

Joined: 17 Nov 2005, 02:43
Location: Raegquitting Spring since 04/24/12
hoijui wrote:
necessary changes
Instead of reading the "n" member of tables to get their size, you have to use the # operator now.
This change is backwards compatible; if you apply it to your Lua, it will also work in current release.
-> do it asap! ;-)


This is as clear as fresh horseshit.

so this: arg.n
becomes: arg
or: arg.#
or:#arg
or...

How about some proper explanations. Also, was this really freaking necessary? Perhaps just one freaking release without you guys breaking lua for no damn good reason.


Top
 Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 54 posts ]  Go to page 1, 2, 3  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group

Site layout created by Roflcopter et al.