Script v1.7.1.0
- AI
- AIDriveStrategy
- AIDriveStrategyBaler
- AIDriveStrategyCollision
- AIDriveStrategyCombine
- AIDriveStrategyConveyor
- AIDriveStrategyStraight
- AITurnStrategy
- AITurnStrategyBulb1
- AITurnStrategyBulb1Revers...
- AITurnStrategyBulb2
- AITurnStrategyBulb2Revers...
- AITurnStrategyBulb3
- AITurnStrategyBulb3Revers...
- AITurnStrategyDefault
- AITurnStrategyDefaultReve...
- AITurnStrategyHalfCircle
- AITurnStrategyHalfCircleR...
- HelperManager
- NPCManager
- Animals
- Contracts
- Debug
- Economy
- Effects
- Events
- Farms
- GUI
- Handtools
- I3d
- Materials
- Misc
- Objects
- Placeables
- Player
- Shop
- Sounds
- Specializations
- Triggers
- Utils
- Vehicles
- Weather
Engine v1.7.1.0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- XML
- general
Foundation Reference
NPCManager
DescriptionThis class handles all npcsParent
AbstractManagerFunctions
- addNPC
- getNPCByIndex
- getNPCByName
- getRandomIndex
- getRandomNPC
- initDataStructures
- loadFromXMLFile
- loadMapData
- loadNPCs
- new
- saveToXMLFile
addNPC
DescriptionAdds a new npcDefinition
addNPC(string name, string title, string category, string imageFilename, string baseDir)Arguments
string | name | npc index name |
string | title | npc real name |
string | category | npc category (e.g. young, middle, old, female) |
string | imageFilename | npc image filename |
string | baseDir | the base directory |
boolean | true | if added successful else false |
138 | function NPCManager:addNPC(name, title, category, imageFilename, baseDir, isBaseType) |
139 | if not ClassUtil.getIsValidIndexName(name) then |
140 | print("Warning: '"..tostring(name).."' is not a valid name for a npc. Ignoring npc!") |
141 | return nil |
142 | end |
143 | |
144 | name = name:upper() |
145 | |
146 | if isBaseType and self.nameToIndex[name] ~= nil then |
147 | print("Warning: NPC '"..tostring(name).."' already exists. Ignoring npc!") |
148 | return nil |
149 | end |
150 | |
151 | local npc = self.npcs[name] |
152 | if npc == nil then |
153 | if title == nil or title == "" then |
154 | print("Warning: '"..tostring(title).."' is not a valid title for a npc. Ignoring npc!") |
155 | return nil |
156 | end |
157 | if category == nil or category == "" then |
158 | print("Warning: '"..tostring(category).."' is not a valid category for a npc. Ignoring npc!") |
159 | return nil |
160 | end |
161 | if imageFilename == nil or imageFilename == "" then |
162 | print("Warning: Missing npc image file for npc '"..tostring(name).."'. Ignoring npc!") |
163 | return nil |
164 | end |
165 | |
166 | self.numNpcs = self.numNpcs + 1 |
167 | npc = {} |
168 | npc.name = name |
169 | npc.title = g_i18n:convertText(title) |
170 | npc.index = self.numNpcs |
171 | npc.imageFilename = Utils.getFilename(imageFilename, baseDir) |
172 | npc.finishedMissions = 0 |
173 | |
174 | self.npcs[name] = npc |
175 | self.nameToIndex[name] = self.numNpcs |
176 | self.indexToNpc[self.numNpcs] = npc |
177 | else |
178 | if title ~= nil and title ~= "" then |
179 | npc.title = g_i18n:convertText(title) |
180 | end |
181 | if imageFilename ~= nil and imageFilename ~= "" then |
182 | npc.imageFilename = Utils.getFilename(imageFilename, baseDir) |
183 | end |
184 | end |
185 | |
186 | return npc |
187 | end |
getNPCByIndex
DescriptionGets a npc by indexDefinition
getNPCByIndex(integer index)Arguments
integer | index | the npc index |
table | npc | the npc object |
207 | function NPCManager:getNPCByIndex(index) |
208 | if index ~= nil then |
209 | return self.indexToNpc[index] |
210 | end |
211 | return nil |
212 | end |
getNPCByName
DescriptionGets a npc by index nameDefinition
getNPCByName(string name)Arguments
string | name | the npc index name |
table | npc | the npc object |
218 | function NPCManager:getNPCByName(name) |
219 | if name ~= nil then |
220 | name = name:upper() |
221 | return self.npcs[name] |
222 | end |
223 | return nil |
224 | end |
getRandomIndex
DescriptionGets a random npc indexDefinition
getRandomIndex()Return Values
integer | npcIndex | a random npc index |
199 | function NPCManager:getRandomIndex() |
200 | return math.random(1, self.numNpcs) |
201 | end |
getRandomNPC
DescriptionGets a random npcDefinition
getRandomNPC()Return Values
table | npc | a random npc object |
192 | function NPCManager:getRandomNPC() |
193 | return self.indexToNpc[self:getRandomIndex()] |
194 | end |
initDataStructures
DescriptionInitialize data structuresDefinition
initDataStructures()Code
23 | function NPCManager:initDataStructures() |
24 | self.numNpcs = 0 |
25 | self.npcs = {} |
26 | self.nameToIndex = {} |
27 | self.indexToNpc = {} |
28 | end |
loadFromXMLFile
DescriptionLoad data from xml savegame fileDefinition
loadFromXMLFile(string filename)Arguments
string | filename | xml filename |
97 | function NPCManager:loadFromXMLFile(xmlFilename) |
98 | if xmlFilename == nil then |
99 | return false |
100 | end |
101 | |
102 | local xmlFile = loadXMLFile("npcXML", xmlFilename) |
103 | |
104 | if xmlFile == 0 then |
105 | return false |
106 | end |
107 | |
108 | local i = 0 |
109 | while true do |
110 | local key = string.format("npcs.npc(%d)", i) |
111 | if not hasXMLProperty(xmlFile, key) then |
112 | break |
113 | end |
114 | |
115 | local name = getXMLString(xmlFile, key.."#name") |
116 | local npc = self:getNPCByName(name) |
117 | if npc ~= nil then |
118 | npc.finishedMissions = Utils.getNoNil(getXMLInt(xmlFile, key.."#finishedMissions"), 0) |
119 | else |
120 | print("Warning: Npc '"..tostring(name).."' not found!") |
121 | end |
122 | i = i + 1 |
123 | end |
124 | |
125 | delete(xmlFile) |
126 | |
127 | return true |
128 | end |
loadMapData
DescriptionLoad data on map loadDefinition
loadMapData()Return Values
boolean | true | if loading was successful else false |
39 | function NPCManager:loadMapData(xmlFile, missionInfo, baseDirectory) |
40 | NPCManager:superClass().loadMapData(self) |
41 | |
42 | self:loadDefaultTypes(missionInfo, baseDirectory) |
43 | return XMLUtil.loadDataFromMapXML(xmlFile, "npcs", baseDirectory, self, self.loadNPCs, missionInfo, baseDirectory) |
44 | end |
loadNPCs
DescriptionLoad data on map loadDefinition
loadNPCs()Return Values
boolean | true | if loading was successful else false |
49 | function NPCManager:loadNPCs(xmlFile, missionInfo, baseDirectory, isBaseType) |
50 | |
51 | local i = 0 |
52 | while true do |
53 | local key = string.format("map.npcs.npc(%d)", i) |
54 | if not hasXMLProperty(xmlFile, key) then |
55 | break |
56 | end |
57 | |
58 | local name = getXMLString(xmlFile, key.."#name") |
59 | local title = getXMLString(xmlFile, key.."#title") |
60 | local category = getXMLString(xmlFile, key.."#category") |
61 | local imageFilename = getXMLString(xmlFile, key.."#imageFilename") |
62 | |
63 | self:addNPC(name, title, category, imageFilename, baseDirectory, isBaseType) |
64 | |
65 | i = i + 1 |
66 | end |
67 | |
68 | return true |
69 | end |
new
DescriptionCreating managerDefinition
new()Return Values
table | instance | instance of object |
16 | function NPCManager:new(customMt) |
17 | local self = AbstractManager:new(customMt or NPCManager_mt) |
18 | return self |
19 | end |
saveToXMLFile
DescriptionWrite data to savegame fileDefinition
saveToXMLFile(string xmlFilename)Arguments
string | xmlFilename | file path |
boolean | true | if loading was successful else false |
75 | function NPCManager:saveToXMLFile(xmlFilename) |
76 | -- save npcs to xml |
77 | local xmlFile = createXMLFile("npcsXML", xmlFilename, "npcs") |
78 | if xmlFile ~= nil then |
79 | for k, npc in ipairs(self.indexToNpc) do |
80 | local npcKey = string.format("npcs.npc(%d)", k-1) |
81 | setXMLString(xmlFile, npcKey.."#name", npc.name) |
82 | setXMLInt(xmlFile, npcKey.."#finishedMissions", npc.finishedMissions) |
83 | end |
84 | |
85 | saveXMLFile(xmlFile) |
86 | delete(xmlFile) |
87 | |
88 | return true |
89 | end |
90 | |
91 | return false |
92 | end |