How to randomize events?

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

Moderators: Slitherine Core, The Lordz

Post Reply
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2193
Joined: Tue Nov 23, 2010 3:35 pm

How to randomize events?

Post by Robotron »

I'd like to include events that show up only with a certain probability. How can this be done?

Let's take the "christmas truce"-event for example. What lines of code have be added to make it appear only with a 50% chance on turn 14?

Code: Select all

-- Christmas truce
function ChristmasTruce()
  if GetEvent("ChristmasTruce") == 0 then
    if game.turn == 14 then
      SetEvent("ChristmasTruce", game.turn)
    end
  end
end
Any ideas?
DanielHerr
Senior Corporal - Destroyer
Senior Corporal - Destroyer
Posts: 117
Joined: Sun Jan 11, 2015 7:51 pm
Contact:

Re: How to randomize events?

Post by DanielHerr »

You will need to use the math.random function. I haven't tried it inside the game, but this should work:

Code: Select all

function ChristmasTruce()
  if GetEvent("ChristmasTruce") == 0 then
    if game.turn == 14 then
      if math.random(0,1) == 1 then
        SetEvent("ChristmasTruce", game.turn)
      end
    end
  end
end
Here is more info on lua math: http://lua-users.org/wiki/MathLibraryTutorial
danielherr.github.io
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2193
Joined: Tue Nov 23, 2010 3:35 pm

Re: How to randomize events?

Post by Robotron »

Yeah, that worked like a charm, thanks a bunch. :D

My next problem: I want to set up an event that drops german morale by ten points if Paris isn't conquered by turn 13 (called the "Moltke"-event, because Chief of Staff General Moltke got sacked after being unable to conquer Paris in time).

This event should be triggered if the Paris-Hex (x82,y30) does NOT belong to germany at the start of turn #13 (the first turn of winter in 1914).

However, this code which I thought should be doing the trick only leads to a crash to desktop...I wonder why?

Code: Select all

function Moltke(hex)
  if GetEvent("Moltke") == 0 then
    if hex.x == 82 and hex.y == 30 and hex.faction.alliance.id ~= 2 then 	 	   
         if game.turn == 13 then
		           SetEvent("Moltke", game.turn)
		            local germany = game:GetFactionById(2)	   
		            ChangeFactionMorale(germany, -10)
                  end	          
            end
       end
end

DanielHerr
Senior Corporal - Destroyer
Senior Corporal - Destroyer
Posts: 117
Joined: Sun Jan 11, 2015 7:51 pm
Contact:

Re: How to randomize events?

Post by DanielHerr »

I haven't added an event, so I'm not sure, but I would guess that function is not being passed a hex. I think there is a function that gets the hex by the coordinates that you can use instead. You also might want to look in the log file inside Documents>Games>CtGW
danielherr.github.io
Robotron
Brigadier-General - Elite Grenadier
Brigadier-General - Elite Grenadier
Posts: 2193
Joined: Tue Nov 23, 2010 3:35 pm

Re: How to randomize events?

Post by Robotron »

Thanks again, DanielHerr, using "local hex = GetHex(82, 30)" it worked indeed. Brilliant! 8)

Code: Select all

--Moltke
function Moltke(hex)
  if GetEvent("Moltke") == 0 then
 
 if game.turn >= 13 then
 	local hex = GetHex(82, 30)
        	if hex.alliance.id == 1 then
  			SetEvent("Moltke", game.turn)
			local germany = game:GetFactionById(2)	   
			ChangeFactionMorale(germany, -10)   
         
    	  	 	end
		end
	end
end
Image
Post Reply

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