RDNWiki: DOWScar/Primer/Basics ...

RDN Wiki Home | Page Index | Recent Changes | Recently Commented | Users | Registration | Login:  Password:  

SCAR Primer: Basics


Contents

Code Block Delimiter


A code block means a section of code where variables are only used in that scope. A code block is determined by any section which starts with if, else, elseif, do, while and so on, and ends with end:


function Test()

	-- Local variable
	local x = 10

	-- New code block
	if x == 10 then

		-- New variable, n
		local n = 30
	
	end

	-- Now n is not defined any more, but x is.

end -- End of function Test, x is no longer defined

Comments


Comments can be one, or multiple lines. They are not compiled at run time, and are simply there to make the code more understandable:


-- This is a one line comment

--[[
	This is a
	multiline comment
	block.
]]

--{{ This is also a multiline comment block, but on one line (so, of course, now a singleline comment!) ]]

Here is a good example of using these comments:


-------------------------------------------------
-- PLAYER FUNCTIONS
-------------------------------------------------
--[[
	The different player functions for the scenario:
	- Sorts out the players income
	- Reinforces the player with thier commanders again
]]

String Delimiter


This is how to encompass strings – such as:


We should attack


Would be encompassed in double quotes ( "" ) like this:


local attackstring = "We should attack"

Table/List


You can make lists, or tables, in SCAR to store multiple bits of information. Notes on tables:



function OnGameSetup()

	-- Define a difficulty global
	-- Note: it doesn't need to predefine what will be in it.
	t_difficulty = {}

	-- We add different table information for each difficulty.
	t_diff_easy = {hqarmour = 0.95, banneraccuracy = 0.3, postaccuracy = 0.3, turretaccuracy = 0.3, grots = false, timeattack = 10 }
	t_diff_normal = {hqarmour = 1, banneraccuracy = 0.7, postaccuracy = 0.7, turretaccuracy = 0.7, grots = false, timeattack = 7  }
	t_diff_hard = {hqarmour = 1.1, banneraccuracy = 1, postaccuracy = 1, turretaccuracy = 1, grots = true, timeattack = 2  }
end

function SetDifficultyLevel( difficultyLevel )

	-- sets up bonus HP depending on difficulty level (0.5 easy, 1.5 hardest)
	Difficulty_SetForAll( difficultyLevel )

	-- easy
	if difficultyLevel == DIFFICULTY_EASY then

		-- Easy difficulty tables can be copied to the difficulty table
		t_difficulty = t_diff_easy

	-- medium
	elseif difficultyLevel == DIFFICULTY_NORMAL then

		-- medium/normal difficulty tables can be copied to the difficulty table
		t_difficulty = t_diff_normal

	-- hard
	elseif difficultyLevel == DIFFICULTY_HARD then

		-- hard difficulty tables can be copied to the difficulty table
		t_difficulty = t_diff_hard
	end
end

Variables


There are only two kinds of variables in SCAR. These are local and global.


You can store any type of information in each of them – in normal programming terms, it would be like:


String («Hello»), Integer (23), Boolean (true/false), Table (t_something = {}), Real (10.4253) (known as Float or Double in other languages).


For Dawn of War, there is also:


Position (x, y, z co-ordinates), EntityID (ID of a entity group), SquadID (ID of a squad group).


They must be one word, without punctuation, but can include underscores, and are usually lowercase likethis, but can be DoneLikeThis too:


-- Correct variable names:
g_variable
hello
good_testing_variable
TestVariable
x
-- Incorrect ones:
test variable name
#beer
\/hello/\

See the next primer, Naming Conventions for the best way to name variables to be understandable.


Here is how locals and globals are defined:


function Test()

	-- Global variables can be changed or got from any new function
	g_globalvariable = 20

	-- Local variable's get created just for the duration of the function it is created in
	local localvariable = 10

end -- Once this end is reached, the variable "localvariable" is lost from memory. "globalvariable" is not though.

-- this is called after Test
function Test2()

	-- Cannot access the variable "localvariable". This will create a new one, which will default to 0 for this test
	if localvariable > 5 then
		print("Local is greater then five: "..localvariable)
	end

	-- Same test for the global variable will work however. We can also change the global variable.
	if globalvariable > 5 then
		print("Local is greater then five: "..globalvariable)
	end
end

Whitespace


Whitespace is the name for new lines and extra spaces to make code more legible. It is entirely a matter of preference, and in group projects it is recommended to be wary and use this.


You can use tab to indent blocks of code, which helps readability. From this example:


if x > 5 then print("X is now greater then 5") else print("X isn't greater then 5") end

This can seem very trivial, as it isn't very complicated, but it can be much easier to read if it is like this:


if x > 5 then
	print("X is now greater then 5")
else
	print("X isn't greater then 5")
end

Usually a new line is used for each new variable, function call, or if/end statement, such as above.

Links


LUA Wiki
SCAR
Wiki Home

End of Page


Referring pages: DOWScar
DOWScar/Primer
DOWScar/Primer/BestPractices

There are no files on this page. [Display files/form]
There is no comment on this page. [Display comments/form]