Robo's Lua Coding masterclass

A forum to discuss custom scenarios, campaigns and modding in general.

Moderators: Slitherine Core, The Lordz

Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Robo's Lua Coding masterclass

Post by Umeu »

:mrgreen:

I need some help with some code. I can only get it to do part of what I want, and I can't figure out what I'm doing wrong. Or how to do if differently.

I wrote 2 different versions, 1 based on the code you wrote for BEF, the other based on Gallieni. They neither do exactly what I want, and I've been trying to merge them, but it doesn't work.

It should work as follows: When the event triggers, an American unit should spawn in Verdun, if Verdun is occupied (which it most likely will be), 1 American unit should spawn in 1 hex range of Verdun as long as there is an unoccupied Entente hex available. If there's no unoccupied hex or no unoccupied Entente hex available, the unit should spawn in the port of Brest, if Brest's port is also unavailable, it should spawn in Nantes.

The code based on the BEF does only the latter part, if Verdun is full, a unit will spawn in Brest, if Brest is full, it'll spawn in Nantes.

Code: Select all

--------------------------------------------
if eventname == "BuildUSArmy2" then

	local brest = GetHex(70, 31)
	local brestport = GetHex(69, 30)
	local nantes = GetHex(74, 34)
	local nantesport = GetHex(73, 34)
	local cherbourg = GetHex(76, 28)
	local cherbourgport = GetHex(75, 28)
	local portsmouth = GetHex(76, 25)	
	local portsmouthport = GetHex(77, 25)
	local paris = GetHex(82, 30)
	local verdun = GetHex(87, 29)
    --local Gvalue = 1
	--local success = 0

	if MatchType() <= 2 then
		--local unitcounter = 1
		if verdun.unit == nil
			and verdun.alliance.id == 1 then
			if GetEvent("VerdunCaptured") <= 0 then
				GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
			end
					

		elseif brestport.unit == nil 
			and brest.alliance.id == 1 then
			GetPlayer(SpawnUnit(1, usa, 69, 30, 1, 100))
			
			elseif nantesport.unit == nil
			and nantes.alliance.id == 1 then
				GetPlayer(SpawnUnit(1, usa, 73, 34, 1, 100))		
			
		end
	end

	if MatchType() == 3   then
		usa.luaData.BuildUSArmy2 = 1
	end
end
The Code I based on Gallieni does only the first part, if Verdun is full, a unit will spawn in 1 hex radius if the hex is available. However, if the hex is unavailable, nothing happens, and the unit doesn't spawn.

Code: Select all

if eventname == "BuildUSArmy2" then

	local brest = GetHex(70, 31)
	local brestport = GetHex(69, 30)
	local nantes = GetHex(74, 34)
	local nantesport = GetHex(73, 34)
	local cherbourg = GetHex(76, 28)
	local cherbourgport = GetHex(75, 28)
	local portsmouth = GetHex(76, 25)	
	local portsmouthport = GetHex(77, 25)
	local paris = GetHex(82, 30)
	local verdun = GetHex(87, 29)

	if MatchType() <= 2 then
		if verdun.unit == nil
			and verdun.alliance.id == 1 then
			if GetEvent("VerdunCaptured") <= 0 then
				GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
			end
					
		elseif verdun.unit ~= nil 
			and verdun.alliance.id == 1 then	
	
			local hexes = game.map:GetHexesInRange(verdun, 1)
			for _, hex in pairs(hexes) do
				if hex.unit == nil then
					if hex.alliance.id == 1 then
						GetPlayer(SpawnUnit(1, usa, hex.x, hex.y, 1, 100))				
							break
					end
				end				
			end
		end
	end

	if MatchType() == 3   then
		usa.luaData.BuildUSArmy2 = 1
	end
end

I tried to merge them in various ways, but it usually means the same result as the code based on Gallieni, though in some variations I got the same result as the one based on BEF (even when there were empty Entente hexes 1 range in Verdun).

This is an example of the combination:

Code: Select all

if eventname == "BuildUSArmy2" then

	local brest = GetHex(70, 31)
	local brestport = GetHex(69, 30)
	local nantes = GetHex(74, 34)
	local nantesport = GetHex(73, 34)
	local cherbourg = GetHex(76, 28)
	local cherbourgport = GetHex(75, 28)
	local portsmouth = GetHex(76, 25)	
	local portsmouthport = GetHex(77, 25)
	local paris = GetHex(82, 30)
	local verdun = GetHex(87, 29)

	if MatchType() <= 2 then
		if verdun.unit == nil
			and verdun.alliance.id == 1 then
			if GetEvent("VerdunCaptured") <= 0 then
				GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
			end
					
		elseif verdun.unit ~= nil 
			and verdun.alliance.id == 1 then	
	
			local hexes = game.map:GetHexesInRange(verdun, 1)
			for _, hex in pairs(hexes) do
				if hex.unit == nil then
					if hex.alliance.id == 1 then
						GetPlayer(SpawnUnit(1, usa, hex.x, hex.y, 1, 100))				
							break
					end
				end				
			end
			
		elseif brestport.unit == nil 
			and brest.alliance.id == 1 then
			GetPlayer(SpawnUnit(1, usa, 69, 30, 1, 100))
			
			elseif nantesport.unit == nil
			and nantes.alliance.id == 1 then
				GetPlayer(SpawnUnit(1, usa, 73, 34, 1, 100))	
							
		end
	end

	if MatchType() == 3   then
		usa.luaData.BuildUSArmy2 = 1
	end
end
I tried for a few days, so if you have any insight, would be really appreciated.
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

Changed nothing and works fine for me:
Clipboard02.jpg
Clipboard02.jpg (53.12 KiB) Viewed 1889 times
Clipboard03.jpg
Clipboard03.jpg (33.05 KiB) Viewed 1889 times
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Sun Mar 05, 2023 3:33 pm Changed nothing and works fine for me:

Clipboard02.jpg


Clipboard03.jpg
ye the verdun spawn works but what I haven't gotten to work is for the unit to spawn in Brest or Nantes if all the hexes near verdun are unavailable (either occupied or enemy)
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

just add a variable to check if the spawning was successful 8)

Code: Select all

	local success = 0	
	
	if MatchType() <= 2 then
		if verdun.unit == nil
			and verdun.alliance.id == 1 then
			if GetEvent("VerdunCaptured") <= 0 then
				GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
					success = 1
			end
					
		elseif verdun.unit ~= nil 
			and verdun.alliance.id == 1 then	
	
			local hexes = game.map:GetHexesInRange(verdun, 1)
			for _, hex in pairs(hexes) do
				if hex.unit == nil then
					if hex.alliance.id == 1 then
						GetPlayer(SpawnUnit(1, usa, hex.x, hex.y, 1, 100))
						success = 1						
						break
					end
				end				
			end
		end	
		if success == 0 then	
			if brestport.unit == nil 
				and brest.alliance.id == 1 then
				GetPlayer(SpawnUnit(1, usa, 69, 30, 1, 100))
				
			elseif nantesport.unit == nil
				and nantes.alliance.id == 1 then
					GetPlayer(SpawnUnit(1, usa, 73, 34, 1, 100))	
								
			end
		end
	end

Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Sun Mar 05, 2023 3:55 pm just add a variable to check if the spawning was successful 8)

Code: Select all

	local success = 0	
	
	if MatchType() <= 2 then
		if verdun.unit == nil
			and verdun.alliance.id == 1 then
			if GetEvent("VerdunCaptured") <= 0 then
				GetPlayer(SpawnUnit(1, usa, 87, 29, 1, 100))
					success = 1
			end
					
		elseif verdun.unit ~= nil 
			and verdun.alliance.id == 1 then	
	
			local hexes = game.map:GetHexesInRange(verdun, 1)
			for _, hex in pairs(hexes) do
				if hex.unit == nil then
					if hex.alliance.id == 1 then
						GetPlayer(SpawnUnit(1, usa, hex.x, hex.y, 1, 100))
						success = 1						
						break
					end
				end				
			end
		end	
		if success == 0 then	
			if brestport.unit == nil 
				and brest.alliance.id == 1 then
				GetPlayer(SpawnUnit(1, usa, 69, 30, 1, 100))
				
			elseif nantesport.unit == nil
				and nantes.alliance.id == 1 then
					GetPlayer(SpawnUnit(1, usa, 73, 34, 1, 100))	
								
			end
		end
	end

I knew it was something like that haha but just didn't know how to do it. Was trying with unit counter and stuff but didn't work :O

thanks!
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Sun Mar 05, 2023 3:55 pm just add a variable to check if the spawning was successful 8)
btw, is there anything specifically I need to be aware of or watch out for when writing the code for this part

Code: Select all

	if MatchType() == 3   then
		usa.luaData.BuildUSArmy2 = 1
	end
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

behold the wisdom of the ancient sage!

Post by Robotron »

Yes. You never ever should just introduce a new luaData variable (here: usa.luaData.BuildUSArmy2) from within a function. It's good practice to define ALL luaData variables you are planning to use at the start, in a script that's getting read right after the game is launched. I've chosen game_morale.lua to store all the luaData stuff and you should too (or find a better solution :P)
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: behold the wisdom of the ancient sage!

Post by Umeu »

Robotron wrote: Sun Mar 05, 2023 7:04 pm Yes. You never ever should just introduce a new luaData variable (here: usa.luaData.BuildUSArmy2) from within a function. It's good practice to define ALL luaData variables you are planning to use at the start, in a script that's getting read right after the game is launched. I've chosen game_morale.lua to store all the luaData stuff and you should too (or find a better solution :P)
what exactly should I put there? Just the event name in high mid low priority for Entente and CP? Or something else?
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

Define any luaData variables you are planning to use, so they don't cause a crash with a "nil value" error when they are later checked by the script.
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Thu Mar 09, 2023 6:06 pm Define any luaData variables you are planning to use, so they don't cause a crash with a "nil value" error when they are later checked by the script.
defining means giving it a number between 0-100 (or random.math)?
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

Yes, any number value that fits the purpose you have in mind. Could be 0 from the start and later be changed to 1 indicating that a certain condition is fulfilled. Or start with a value of 1-100 to represent a starting chance for a certain D100 roll. Or could start with a high number value of let's just say 1000 to represent some value that is getting "used up" during the game etc.
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Fri Mar 10, 2023 10:29 am Yes, any number value that fits the purpose you have in mind. Could be 0 from the start and later be changed to 1 indicating that a certain condition is fulfilled. Or start with a value of 1-100 to represent a starting chance for a certain D100 roll. Or could start with a high number value of let's just say 1000 to represent some value that is getting "used up" during the game etc.
Ok, will do. I'm done with what I was working on, so if you're interested in including any of it in the potzlitz main mod, I'd be happy with that. The rest, I'll just keep for personal use or upload here for others who like to use it.
Minor Powers Artillery overhaul + small tech adjustment + new Generals expansion

New Generals expansion
I'd like to make generals a bit more polarized, so that they're used either for very specific unit types or roles (offensive/defensively). Of course, historical performance will play a role in their stats, but in general, new commanders for major powers will be 1 or 0 range, but have high stats in 1 attribute or group of attributes geared towards their role (defensive/offensive). The bigger range commanders will be a bit more allrounded, though still have their own distinct flavor and use. It's nice to have most of your troops commanded by Haig or Joffre and all, but when the going gets tough, you send in Currie or Mangin to break down the enemy lines. Or you'll use Borojovic to hold the key part of the Isonzo, while you send Hotzendorf on an all-inclusive cruise tour, preferably to Mars.

Also, to make generals more distinct, I've added new commander types. Currently I've added Artillery commanders, Tank commanders, submarine commanders and 1 type of commander that affects both infantry and artillery (but not other kinds of ground troops). I think that should cover all kinds required, but I might make a cavalry only commander :mrgreen:

x Von Arnauld restricted to submarines
x added Forstmann and Valentiner as captains restricted to submarines for Germany and Austria respectively (unlock with GotStrafeEngland and GermanSubsToAdriatic respectively)
x Bruchmuller added as an one range German commander restricted to artillery (unlocks with Bruchmuller event)
x Nivelle changed to affect artillery as well
x Walz added as a 1 range Turkish flying ace (unlocks with Bavarian Squadron 304 event)
x Patton added as a 0 range American commander restricted to tanks (Unlocks with Building the Army: Tank Corps event)
x Luke Jr. added as a 0 range American flying ace (Unlocks with Building the Army: Air Service event)
> Monash added as a 0 range British commander restricted to tanks to illustrate his combined arms proficiency (unlocks with the first British tank attack with disastrous outcome (3+ casualties)
> Byng added as a 1 range army British commander > still need to write event to unlock


Adjust tech research times:
x Submachine gun research time tripled.
x anti tank techs after 1st lvl increased research time 2x (I might revert this).
x Anti-Aircraft tech upgrades extended to Home Guard units.
x Railroad gun research time increased
x Germany gets tech boost on railroad gun at start of game > other nations that can produce it, get 3 random 1D6 rolls).
x France's Railroad gun tech readjusted to match the above.
x France's chlorine gas tech bonus at start of game adjusted to be equal to Germany's
x changed around anti-sub attack tech values so subs don't become obsolete too quickly. 10-5-10 rather than 10-10-5 (might revert this)
x added Renault event for the French to jump ahead in the tank war, similar to the Fokker event for airplanes.


Artillery Overhaul:
Artillery is king. But only a few major powers had the capacity to field, maintain and supply massive amounts of heavy artillery. To more accurately reflect this in the game, I've added the following restrictions to:

Minor Powers:
x max 1 battery
x max 5 ammo
x Minor powers Chlorine gas upgrade delayed by a about a year (12 turns)
x All techs past specialized shells are vastly delayed to the point of not unlocking during the game, with the exception of Portugal.
x Portugal gets an artillery lab at start of the game, so their artillery is up to date whenever they join the war.



Ottomans:
In some ways, the Ottoman Empire was a major power, but in other ways, they were even worse off than some minor powers. The Ottomans were unable to produce modern artillery, nor could they logistically support large amounts of modern artillery. They relied heavily on Germany and Austria to supply them with ammo. To reflect this, the Ottomans have no way to research any of the artillery techs by themselves. Instead, their tech updates are tied to events related to the Turco-German alliance.

x max 1 battery
x max 5 ammo
x no artillery research. Can't build artillery labs.

Events that unlock techs for Turkey:
x German military mission to Turkey also unlocks pneumatic howitzer tech
x Constantinople express also unlocks specialized shells tech (as well as pneumatic howitzer if not unlocked earlier), and the ammo trickle it gives is more generous to compensate for the limit on building ammo factories. (might need some tweaking to find the right value)
x Bavarian Squadron 304 event sends a German fighter + bomber squadron to Turkey and also unlocks Aerial Artillery Coordination tech.
x New event about a Turkish elite unit (XV corps) sent to Austria to fight the Russians. The event will represent them returning home, and will unlock the next counterbattery tech + spawn 1 artillery unit + 1 infantry unit, with lvl 3 experience (though a single repair will knock them down to lvl 2)
x Yildirim army group event will also unlock the next counterbattery tech.
x Kaiser's visit to Istanbul will unlock the gas tech equivalent to Germany's. It's the only way for Turkey to get gas upgrades. So you have to choose, have the Kaiser visit early and be stuck on lower gas tech for the rest of the game, but be able to use it for longer. Or have the Kaiser visit late, and risk that the tech no longer matters.
x Ottomans are not able to unlock creeping barrage.

Misc
x counter battery techs value increased by 1.
x USA get's artillery research (? > might change it to having option to research but no labs.)
x Georg Bruchmuller event now unlocks the man, the myth himself as an 1 range artillery battery commander.

USA
>>> USA can build unlimited ammo factories and can produce unlimited artillery like any major power, but doesn't have an artillery research lab, or any research lab, available from the start of the game. This means that whenever the US joins the war, their army and equipment are outdated. Catching up will require a lot of investment into labs, or instead they can:

x Build the Army through events, 1 for each type/branch of army.
x Each event unlocks the corresponding techs to match the british/french army, depending on who has the tech unlocked already. If none of the Entente Westfront major powers has the tech, USA won't get it either (usually not relevant, but can be relevant when playing Lamps out or USA join early option).
x To reflect that experience can't be bought, Creeping Barrage nor Infiltration tactics will unlock regardless of whether France or Britain have unlocked those techs already. Units also still have to be upgraded, to reflect the time, cost and difficulty of rapidly modernizing and expanding an army while also shipping it across an ocean.
x The events will cost the USA manpower, in exchange for units, technology and sometimes commanders.


Still planned:
> Front restrictions for Entente generals (Joffre, Foch etc & French, Haig etc to West Front, Townshend to Mesopotamian front, Allenby to Egypt, Hamilton to Turkey, and should get dismissed if Gallipoli fails
> Joffre-Nivelle-Petain command change + new Nivelle offensive event based on Somme event (I know something like it already exists, but Joffre doesn't get dismissed all that often, and I've never seen Petain unlock with siege of Verdun because Nivelle needs to come before him (imo it should be other way around)
> event involving Byng to unlock him (probably basend on battle of Cambrais 1917)

Things I couldn't figure out:
> constantinople express should also increase the max limit of artillery batteries to 2 (to represent German artillery shipments as well as ammo shipments)
> renault light tank event should also increase tank limit for France by 1, so they can have 1 more tank than other nations to show they had the biggest and most advanced tank corps by the end of the war.
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

@robotron

I'll post it here as not to derail the other thread too much with coding

but what am I doing wrong here? file = game.lua

this way, the code works.

Code: Select all

function UnitCanEntrench(unit,hex,nextTurn)
 
  if hex == nil then
    hex = unit.hex
  end
  
  if unit.faction.alliance.id == 1 and unit.faction.id == 7 then
	unit.faction.luaData.entrenchMax = 3
	elseif unit.faction.alliance.id == 1 and unit.faction.id == 0 then
	unit.faction.luaData.entrenchMax = 0
	else
	unit.faction.luaData.entrenchMax = unit.faction.luaData.entrenchMax
  end 
 
  if unit.alive 
	and unit.hex ~= nil 
	and unit.type ~= Unit.NAVAL 
	and unit.type ~= Unit.AIR
	and ((nextTurn == true and unit.hex.id == hex.id) 
			or unit.luaData.noEntrenchTurn ~= game.turn -1) then
				if hex ~= nil 
					and hex.entrench < unit.faction.luaData.entrenchMax then
				  if unit.supply == 2 
					or (unit.supply == 1 and hex.entrench < 2) then
					return true
				  end
				end
  end
  return false
end
but when I want to rewrite this part of the code to include coordinates or original faction hex rather than the unit ID, the game crashes, saying: attempt to index local 'hex' (a nil value) or attempt to index local 'originalFaction' (a nil value)

Code: Select all

  if unit.faction.alliance.id == 1 and unit.hex.x <= 109 and hex.originalFaction.id == 7 then
	unit.faction.luaData.entrenchMax = 3
	elseif unit.faction.alliance.id == 1 and hex.originalFaction.id == 7  then
	unit.faction.luaData.entrenchMax = 0
	else
	unit.faction.luaData.entrenchMax = unit.faction.luaData.entrenchMax
  end 
also I'd like for the enemy to not see this event, just the ??? hidden pop up. How do I do this?
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

I'm confused about what you are trying to do. Do you just want to modify function UnitCanEntrench or do you want to create a new event using parts of function UnitCanEntrench?
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Sun Nov 05, 2023 2:22 pm I'm confused about what you are trying to do. Do you just want to modify function UnitCanEntrench or do you want to create a new event using parts of function UnitCanEntrench?
the last one, I want to create a new event, which, under certain conditions (related to hex and nationality of the hex) gives another level of entrenchment. The idea is that the event is recreating a Hindenburg Line, and so I want the extra entrenchment only to happen on the Western front.
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

I'd try it this way:

Code: Select all

	for unit in germany.units do
		if 	unit.hex ~= nil
			and unit.terrain.type == Terrain.LAND 
			and (unit.hex.originalFaction.id == 0 or unit.hex.originalFaction.id == 6)  then
				unit.hex.entrench = germany.luaData.entrenchMax
		end
	end
The event will only be shown to the CP player if you trigger it via

Code: Select all

SetEvent("HindenburgLine", game.turn, nil, germany.alliance)
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Sun Nov 05, 2023 3:56 pm I'd try it this way:

Code: Select all

	for unit in germany.units do
		if 	unit.hex ~= nil
			and unit.terrain.type == Terrain.LAND 
			and (unit.hex.originalFaction.id == 0 or unit.hex.originalFaction.id == 6)  then
				unit.hex.entrench = germany.luaData.entrenchMax
		end
	end
The event will only be shown to the CP player if you trigger it via

Code: Select all

SetEvent("HindenburgLine", game.turn, nil, germany.alliance)
hmm, but this is only a one time effect, right? Or do I understand the code wrong? I want the effect to last, and I want it to give extra entrenchMax, so under normal circumstances, with all techs researched, the max multiplier would be 5. But with hindenburg line, I want it to be 6.

But I see that it's better to write a separate function, rather than trying to add it to the unitcanentrench one. But I'm not too familiar yet with writing functions.

Is there a reason to specify anything in between the ()? Sometimes there are some restrictions, and sometimes it's just a blank. But I don't understand yet why.
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

If you just want to have a function that can be triggered, it's sufficient to have the function's name followed by ()

Code: Select all

function Test()

end
If a function has other stuff included between the brackets, for example hex, unit etc. this means the function is used to check specific values that are passed to that function when it's called.

hmm, but this is only a one time effect, right?
Well, the hex-entrenchment of course stays until the hex is vacated for a few turns.
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Umeu
Sergeant First Class - Elite Panzer IIIL
Sergeant First Class - Elite Panzer IIIL
Posts: 437
Joined: Sat Nov 17, 2012 7:00 pm

Re: Robo's Lua Coding masterclass

Post by Umeu »

Robotron wrote: Sun Nov 05, 2023 4:28 pm If you just want to have a function that can be triggered, it's sufficient to have the function's name followed by ()

Code: Select all

function Test()

end
If a function has other stuff included between the brackets, for example hex, unit etc. this means the function is used to check specific values that are passed to that function when it's called.

hmm, but this is only a one time effect, right?
Well, the hex-entrenchment of course stays until the hex is vacated for a few turns.
Ok, thanks. I think I have an idea of how to make it work now.

What about making events ??? hidden btw? Is it random or can I add events to some list?
Check out Project: IMBA, the balance mod for the multiplayer section of Commander: the Great War. Your input is appreciated! viewtopic.php?f=218&t=39677
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2173
Joined: Tue Nov 23, 2010 3:35 pm

Re: Robo's Lua Coding masterclass

Post by Robotron »

Only choice-events can be displayed as ???-events.

If you want to prevent your Hindenburg-Line event from showing upon triggering, trigger it with -1 instead of game.turn

Code: Select all

if GetEvent("HindenburgLine") == 0 then
	if game.turn == 45 then --- just an example for whatever condition you fancy
		SetEvent("HindenburgLine", -1)
	end
end
The disadvantage of doing it this way is that you won't be able to refer to the proper game.turn on which the HindenburgLine was triggered in the future, because this is now set to -1

So for example this would no longer be possible

Code: Select all

if game.turn ==  GetEvent("HindenburgLine") +12 then
	(...)
end
Image
Slitherine's Commander the Great War - Director's Cut: POTZBLITZ mod!
FIND IT HERE: http://www.slitherine.com/forum/viewtopic.php?f=218&t=77884&p=662610#p662610
Post Reply

Return to “Commander the Great War : Mods & Scenario Design”