Create units from external file list

Create units from external file list

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Create units from external file list

Post by daryl »

Hi all,
can you help me please about this question:
i would like a script that create units from an external file list:

For example, I have this code:

Code: Select all

function gadget:GetInfo()
   return {
      name = "unita_iniziali",
      desc = "Aggiunge unità all'inizio del gioco.",
      author = " thx to http://springrts.com community"
      date = "September 19st,  2010",
      license = "GPL,  v.2,  2008",
      layer = 1,
      enabled = true,
   }
end
 
local gaiaTeamId        = Spring.GetGaiaTeamID()

eu_pilonax_a=Spring.CreateUnit("EUF_pilonax",3027,0,9301,2,3)
eu_pilonax_b=Spring.CreateUnit("EUF_pilonax",3582,0,9301,1,3)
eu_pilonax_c=Spring.CreateUnit("EUF_pilonax",3343,0,9656,1,3)
eu_pilonax_d=Spring.CreateUnit("EUF_pilonax",3582,0,9640,1,3)

end
this code create directly the units but,
I would like read the creating unit list from a file, example:

Code: Select all

function gadget:GetInfo()
   return {
      name = "unita_iniziali",
      desc = "Aggiunge unità all'inizio del gioco.",
      author = " thx to http://springrts.com community"
      date = "September 19st,  2010",
      license = "GPL,  v.2,  2008",
      layer = 1,
      enabled = true,
   }
end
 
local gaiaTeamId        = Spring.GetGaiaTeamID()

code that create units by reading the units list from "initialunits.lua"  ( or "initial.txt" or other file)

end
is this possible? can you help me to do this?
Thx you
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Create units from external file list

Post by FLOZi »

User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Create units from external file list

Post by zwzsg »

Unit creation is a synced operation (= must be the same on all computers in online game), and reading external files is disabled in synced context.

So you need to read the external file in unsynced gadget or widget, pass the data from unsynced to synced by using the special channels made for that, and then create units in a synced gadget.

Yes, it's much more convoluted than you'd expect.

But still possible, I've done it a long time ago viewtopic.php?f=44&t=24034
What I called loading savegame, and loading missions, is actually just about creating units from an external file.

Of course, during 6 years, Spring API probably changed enough that my code might not work anymore.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Create units from external file list

Post by FLOZi »

Not if it is a lua file inside the archive you include, zwzsg.
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Re: Create units from external file list

Post by daryl »

ok..Unit creation is a synced operation and.. what appen if I change this LUA code in all the computers in online game before playing?

for example I can edit directly this code:

Code: Select all

function gadget:GetInfo()
   return {
      name = "unita_iniziali",
      desc = "Aggiunge unità all'inizio del gioco.",
      author = " thx to http://springrts.com community"
      date = "September 19st,  2010",
      license = "GPL,  v.2,  2008",
      layer = 1,
      enabled = true,
   }
end
 


eu_pilonax_a=Spring.CreateUnit("EUF_pilonax",3027,0,9301,2,3)
eu_pilonax_b=Spring.CreateUnit("EUF_pilonax",3582,0,9301,1,3)
eu_pilonax_c=Spring.CreateUnit("EUF_pilonax",3343,0,9656,1,3)
eu_pilonax_d=Spring.CreateUnit("EUF_pilonax",3582,0,9640,1,3)
"x" other unit to player 2 etc...

end
this file will be edited before online play and this will be the same code in all the computer in online game.
Is this a good solution?
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7049
Joined: 16 Nov 2004, 13:08

Re: Create units from external file list

Post by zwzsg »

FLOZi wrote:Not if it is a lua file inside the archive you include, zwzsg.
I interpreted "external" as "not in the game archive". Because as his first example indicate daryl already knows how to create units from a lua file inside the game archive.


Daryl: It might work, but no, it's not a good solution. The good solution is to use Spring.SendLuaRulesMsg & gadget:RecvLuaMsg to hop over the unsynced / synced great divide.
User avatar
daryl
Posts: 195
Joined: 08 Oct 2006, 10:33

Re: Create units from external file list

Post by daryl »

zwzsg wrote:I interpreted "external" as "not in the game archive". Because as his first example indicate daryl already knows how to create units from a lua file inside the game archive.
yes, as solution 1
zwzsg wrote:Daryl: It might work, but no, it's not a good solution. The good solution is to use Spring.SendLuaRulesMsg & gadget:RecvLuaMsg to hop over the unsynced / synced great divide.
ok.. i will do some experiments...
thx to all!!!
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6240
Joined: 29 Apr 2005, 01:14

Re: Create units from external file list

Post by FLOZi »

Post Reply

Return to “Lua Scripts”