Page 1 of 1

Create units from external file list

Posted: 24 Nov 2016, 13:56
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

Re: Create units from external file list

Posted: 24 Nov 2016, 18:24
by FLOZi

Re: Create units from external file list

Posted: 24 Nov 2016, 20:59
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.

Re: Create units from external file list

Posted: 24 Nov 2016, 21:22
by FLOZi
Not if it is a lua file inside the archive you include, zwzsg.

Re: Create units from external file list

Posted: 25 Nov 2016, 09:30
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?

Re: Create units from external file list

Posted: 25 Nov 2016, 23:29
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.

Re: Create units from external file list

Posted: 26 Nov 2016, 12:46
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!!!

Re: Create units from external file list

Posted: 26 Nov 2016, 12:54
by FLOZi