These control structures are detailed at the LUA wiki:
http://lua-users.org/wiki/ControlStructureTutorial. The page should be a good starter for these controls, and might be best read first.
However, these here use Dawn of War specific examples on how to use the various controls.
The if statement is a conditional. This tests things to be true or false.
Basically, in english, an if statement could be litterally like this:
-- Is ben big? If yes, say hello
-- Translated above statement into an if statement.
if ben_size == big then
print("hello")
endAn else statement can be used to provide the case when the if statement fails. This is a good example of how to use if and else statements together.
function Rule_PrintIncome()
-- Test how rich player 1 is.
-- Player_GerResource is a function. This will return how much resources they have
-- g_Player1 is a global variable, and is player 1.
-- Get the players power
local power = Player_GetResource(g_Player1, RT_Power)
if power < 100 then
-- This is printed if player 1 has LESS then 100 power.
print("Player 1 isn't very...powerful! hahaha! he has only..."..power.." power!")
else if power < 500 then
-- This is printed if player 1 has LESS then 500 but EQUAL or MORE then 100 power.
print("Player 1 is moderately powerful.. He has..."..power.." power!")
else
-- This is printed if player 1 has EQUAL or MORE then 500 power.
print("Player 1 is very...powerful! Darn! He has "..power.." power!")
end
endResults:
for loops can be used to transverse a amount of numbers or values in a table. The examples in the
LUA Users Wiki – Control Structure Tutorials should provide the 3 examples, and here is a good dawn of war version:
-- ms01.scar
-- Add all entities from player 2, around the markers mkr_gethut0X, where X = 1, 2 or 3, into one EGroup.
for i = 1,3 do
Player_GetAllEntitiesNearMarker(g_Player2, "eg_orkspawn0"..i, "mkr_gethut0"..i)
EGroup_AddGroup(EGroup_CreateIfNotFound("eg_temphutz"), EGroup_FromName("eg_orkspawn0"..i))
end
-- Now you can do things to the entity group (destroy them, count them, set health, get health, etc).
For more information on for loops and thier uses see the LUA wiki:
http://lua-users.org/wiki/ForTutorial
while loops work by calling code until a conditional statement fails. The examples in the
LUA Users Wiki – Control Structure Tutorials should provide the main uses of this control structure.
For Dawn of War, there is rarely a time when while is nessissary, because generally you can (and should) use for more often. There is no good examples of when a while statement should be used in Dawn of War (at least, the author cannot think of one), rather then a for statement.
repeat loops are similar to while loops. They work on a conditional basis, however, they have the condition at the end of the piece of code, meaning the loop is always executed at least once.
The examples in the
LUA Users Wiki – Control Structure Tutorials should provide the main uses of this control structure.
Like while in Dawn of War it has limited use.
The break statement is used in all loops to stop the loop exactly when executed. It can be used to break a loop on a condition, for example, this for loop will create a single trak at one of the mekshops randomly. It isn't perfect – usually, mekshop 6 gets the most traks because the randomisation isn't good enough, however, this will not create more then once whenver it is called.
break can be used in while, repeat and for loops.
-- Iterate around the loop
for i = 1, 6 do
-- Are there any mekshops without associated traks?
if not EGroup_IsEmpty("sg_player3_mekshop_"..i) then
-- Must create one somewhere
totaldone = totaldone + 1
-- Randomly see if this is the one which is chosen
-- Or it is the last one avalible
-- World_GetRand(1, 6) down to (1, 1), should mean an equal % at each.
if World_GetRand(1, mekshops) == 1 or totaldone == mekshops then
-- Create the trak here
SGroup_CreateIfNotFound("sg_OrkTrak")
SGroup_Clear(SGroup_FromName("sg_OrkTrak"))
Util_CreateSquadsAtMarker(g_Player3, "sg_OrkTrak", "ork_squad_wartrak", "mk_player3_mekshop_"..i, 1)
-- Order them to attack always
Cmd_SetStance("sg_OrkTrak", STANCE_Attack)
-- Only create one at a time. Break.
break
end
end
end
Note: as noted in the
LUA Control Structures Tutorials, you need to have an end statement after any use of break, else it will not compile and cause errors.