I am not sure if I am asking too much, but is there a chance that you create a template for us / set of files needed for this miracle
?
So you want it the easy way, a copy/paste solution?
Well you're lucky because I am currently working on a similar script and it isnt many changes away from what you like to achieve.. it still took over 1 hour, there you go:
The script itself is abit advanced, but because it manages everything I think this is still the easieset possible solution. I personally hate this endless file seperation and like the idea of a simple 1 file solution.
Step 1: You need to paste this Code in any registered .xml file:
<!-- === Used to prevent multiple usage of the lua code === -->
<MiscObject Name="Upgrade_Dummy">
<Text_ID/>
<Space_Model_Name>W_LASER_INVISIBLE.ALO</Space_Model_Name>
<Land_Model_Name>W_LASER_INVISIBLE.ALO</Land_Model_Name>
<Scale_Factor>1.0</Scale_Factor>
<Behavior>DUMMY_ORBITAL_STRUCTURE, SPACE_OBSTACLE, IDLE</Behavior>
<SpaceBehavior>ABILITY_COUNTDOWN, UNIT_AI, HUNT</SpaceBehavior>
<Layer_Z_Adjust>0.0</Layer_Z_Adjust>
<Sort_Order_Adjust>-1</Sort_Order_Adjust>
<Remove_Upon_Death>true</Remove_Upon_Death>
<Space_Layer> StaticObject </Space_Layer>
<Target_Stealth_Units>Yes</Target_Stealth_Units>
<Victory_Relevant>no</Victory_Relevant>
<Lua_Script>ObjectScript_Hero_Upgrade</Lua_Script>
</MiscObject>
Step 2:
Please paste
<Lua_Script>ObjectScript_Hero_Upgrade</Lua_Script>
into any .xml instance in UpgradeObjects.xml (you can create an new upgrade). Dont forget to additionally paste the name of this upgrade into the
<Tactical_Buildable_Objects_Multiplayer> tag of your MULTIPLAYER version of the Starbase, or it wont be buildable.
Actually, you could paste <Lua_Script>
ObjectScript_Hero_Upgrade</Lua_Script> directly into the code of the LV2 Starbases, the upgrade would take effect as soon that starbase upgrades to Level 2. But it would overwrite any lua script that starbase already uses, so you might want to check that unit for oldar lua entries first.
Step 3:
Please copy all content below into a empty .lua file and place it into the
/Data/Scripts/GameObjects Directory:
--==================================================================================================
--**************************** Coded by Imperial: Hero Upgrade Script ******************************
-- ***************** This script is allowed to be used as template for other mods *****************
--==================================================================================================
--[[
<!-- You need to have this dummy unit 1x in any registered .xml: -->
<!-- === Used to prevent multiple usage of the lua code === -->
<MiscObject Name="Upgrade_Dummy">
<Text_ID/>
<Space_Model_Name>W_LASER_INVISIBLE.ALO</Space_Model_Name>
<Land_Model_Name>W_LASER_INVISIBLE.ALO</Land_Model_Name>
<Scale_Factor>1.0</Scale_Factor>
<Behavior>DUMMY_ORBITAL_STRUCTURE, SPACE_OBSTACLE, IDLE</Behavior>
<SpaceBehavior>ABILITY_COUNTDOWN, UNIT_AI, HUNT</SpaceBehavior>
<Layer_Z_Adjust>0.0</Layer_Z_Adjust>
<Sort_Order_Adjust>-1</Sort_Order_Adjust>
<Remove_Upon_Death>true</Remove_Upon_Death>
<Space_Layer> StaticObject </Space_Layer>
<Target_Stealth_Units>Yes</Target_Stealth_Units>
<Victory_Relevant>no</Victory_Relevant>
<Lua_Script>ObjectScript_Hero_Upgrade</Lua_Script>
</MiscObject>
<!-- Then please paste this Lua tag into any Upgrade .xml instance of UpgradeObjects.xml, or StarBase.xml -->
<Lua_Script>ObjectScript_Hero_Upgrade</Lua_Script>
<!-- Finally please insert VALID unit names into Valid_Units (2 replaces 1, 4 replaces 3, 6 replaces 5 and so on.. -->
<!-- Do it with caution, any misstyping in Valid_Units will crash the script!, and dont use Squadrons or HeroCompanys, UniqueUnits and SpaceShips/Ifantry is allowed -->
]]
-- We'll need to load this:
require("PGStateMachine")
require("PGStoryMode")
--=================== State Definitions ===================
function Definitions()
Define_State("State_Init", State_Init)
Define_State("State_Inactive", State_Inactive)
Valid_Units = {"Tauri_Hero_Mitchell_Fighter_Mitchell", "Tauri_Hero_Daedalus", "Tauri_Hero_Mitchell_Fighter"}
-- This is the Dummy Opgrade Unit we're going to use
Upgrade_Dummy_Name = "Upgrade_Dummy"
-- We'll need this table to know which units were hidden:
Hidden_Units = {}
-- Refreshing every 1 secs
ServiceRate = 0.1
Neutral = Find_Player("Neutral")
end
--=================== State_Init ===================
function State_Init(message)
if message == OnEnter then
-- We want only Regular Units to spawn 1 Upgrade_Dummy,
-- if this script is executed by a Upgrade_Dummy it will run through this state without doing anything
if Object.Get_Type() ~= Find_Object_Type(Upgrade_Dummy_Name) then
-- Getting all Units of this type
local Upgrade_Dummies = Find_All_Objects_Of_Type(Upgrade_Dummy_Name)
-- If there already is one Instance of this kind, this Script will Exit
if not TestValid(Upgrade_Dummies[1])
-- Making sure each fraction gets its own invisible Upgrade Dummy
or Upgrade_Dummies[1].Get_Owner() ~= Object.Get_Owner()
or Upgrade_Dummies[2].Get_Owner() ~= Object.Get_Owner()
or Upgrade_Dummies[3].Get_Owner() ~= Object.Get_Owner()
or Upgrade_Dummies[4].Get_Owner() ~= Object.Get_Owner() then
-- Otherwise we create one instance of it, so it runns... once ^^
Upgrade_Dummie = SpawnList({Upgrade_Dummy_Name}, Object, Object.Get_Owner(), true, true)
-- After spawning this dummy we can exit this script, its not longer needed.
ScriptExit()
else
ScriptExit()
end
end
-- Have to wait for stability reasons
Sleep (4.0)
-- If you wish to see the Dummy (for test purposes) uncomment this:
-- Object.Highlight(true)
Set_Next_State("State_Inactive")
end
end
--=================== State_Inactive ===================
function State_Inactive(message)
if message == OnUpdate then Sleep (10.0) -- Sleep 10 to refreach every 10 sec
-- === This part finds all valid Target Units and throws them into the table "Target_Unit" ===
-- (Re)innitiating Variable
Target_Unit = {}
-- Getting all Valid_Units instances on the map
for each, String_Name in pairs(Valid_Units) do
local Found_Units = Find_All_Objects_Of_Type(String_Name)
for each, Unit in pairs(Found_Units) do
-- If it still exists and has the player fraction
if TestValid(Unit) and Unit.Get_Owner() == Object.Get_Owner() then
-- Then we will insert it into the table of "Target_Unit"
table.insert(Target_Unit, Unit)
end
end
end
-- ==========================================
-- And finally we check this on every refresh for each unit in the Target_Unit list:
for each, Unit in pairs(Target_Unit) do
-- If still existing
if TestValid(Unit) then
-- === We Replace Valid_Units[1] with Valid_Units[2] ===
if Unit.Get_Type() == Find_Object_Type(Valid_Units[1]) then
-- Spawning Upgraded version to old position: param 1 = Unit, param 2 = Loaction, param 3 = Owner (Object Owner in this case)
Upgraded_Unit = Spawn_Unit(Find_Object_Type(Valid_Units[2]), Unit, Unit.Get_Owner())
Sleep (0.5)
-- Despawning old version after a short break
Unit.Despawn()
-- === Replacing Valid_Units[3] with Valid_Units[4].. and so on ===
-- You can copy this part as often you like and change the values of Valid_Units[X] to upgrade more units
elseif Unit.Get_Type() == Find_Object_Type(Valid_Units[3]) then
-- Upgraded_Unit = Spawn_Unit(Find_Object_Type(Valid_Units[4]), Unit, Unit.Get_Owner())
Sleep (0.5)
-- Despawning old version after a short break
Unit.Despawn()
-- You can use wheter Despawn or paste the few lines above to hide the object instead
-- Commented out because causes Scriptcrash if is assigned to unused table slots:
--[[
elseif Unit.Get_Type() == Find_Object_Type(Valid_Units[5]) then
Upgraded_Unit = Spawn_Unit(Find_Object_Type(Valid_Units[6]), Unit, Unit.Get_Owner())
Sleep (0.5)
Unit.Despawn()
elseif Unit.Get_Type() == Find_Object_Type(Valid_Units[7]) then
Upgraded_Unit = Spawn_Unit(Find_Object_Type(Valid_Units[8]), Unit, Unit.Get_Owner())
Sleep (0.5)
Unit.Despawn()
]]
end
end
end
-- For testpurposes, to know when a cycle ends:
-- Object.Get_Owner().Give_Money(1000)
end
end
--=================================== Threads ===================================
-- When triggered our Object will make Harakiri
function Make_Harakiri()
for each, Unit in pairs(Hidden_Units) do
Unit.Despawn()
end
-- Clearing Variable
Hidden_Units = {}
end
--=================== End of File ===================
Now, what this script is going to do is;
It checks what unit executes it. If you inserted its lua tag to more then 1 xml units on the map this doesent matter, only the first one of them will spawn a Upgrade_Dummy (the object we inserted to the .xml before). Exception: It allows only 1 instance of Upgrade_Dummy per fraction, so other players can use it as well.
If this script is executed by the (invisible) Dummy (for test purpose you can turn it visible uncommenting the Highlight line),
then it will run through the State_Init state without doing anything and will continue with State_Inactive, which refreshes OnUpdate and sleeps 10 secs, so it checks the map every 10 secs for the units listed in Valid_Units.
Step 4, the last thing you need to do:
Please insert units into the
Valid_Units table. But
do it with caution, as any mistyping or non existent .xml instance in this table will instantly
crash the script. (you can use the (commented) .give_money line at the bottom to test when the script crashes).
The script works like this:
Valid_Units[1] (the first listed object) will be replaced by Valid_Units[2],
then Valid_Units[3] getts replaced by Valid_Units[4]
Valid_Units[5] getts replaced by Valid_Units[6], and so on... you just need to make sure to have the right units in that table, thats it.
BUUT, please dont use Squadron or HeroCompanys, because these are groups of Objects (containers) and the script would not detect them, instead use the actual SpaceUnits, or UniqueUnits, or Infantry .. what ever of these Squadrons/Teams so the script can detect them on the map and replace them.
In my script example I used SG1 to be upgraded by the Daedalus as soon the Upgrade was built.
If the upgrade was built before this unit enters the map the script will keep running and check until these units spawned, then it will auto upgrade them.
Sorry if I over explained, I thik its better to explain too much then leaving open questions. Its actually simply following the 4 steps, thats it.
I can imagine this script, now that it works can be used for The Pegasus Chronicles as well ^^