Merchant creation script not working?
Posted: Thu Nov 02, 2017 8:30 pm
I'm trying to stitch together some other folks' code into a merchant generation script, but I get an error trying to add the unit because it indexes new_merch which is a nil value... can anyone help me with this?
Code: Select all
math.randomseed( os.time() )
PORTTABLE = {'Singapore Port', 'Singapore Port', 'Singapore Port', 'Shanghai Port', 'Shanghai Port', 'Shenzhen Port', 'Hong Kong Port', 'Busan Port', 'Dalian Port', 'Kobe Port' }
PORT = PORTTABLE[math.random( 1, #PORTTABLE)]
DBIDTABLE = { 259, 275, 339, 145, 144, 2776, 2775, 2774, 2773, 2023, 2031, 2029, 2030, 2028, 2027, 339, 2023, 2027 }
DBID = DBIDTABLE[math.random( 1, #DBIDTABLE)]
MISSIONTABLE = {'Singapore to Shanghai', 'Singapore to Kaohsiung', 'Singapore to Busan', 'Shanghai to Singapore'}
MISSION = MISSIONTABLE[math.random( 1, #MISSIONTABLE)]
lastCount = ScenEdit_GetKeyValue('MerchCounter');
lastCountN = tonumber(lastCount);
if lastCountN == nil then
lastCountN = 0;
end
lastCountN = lastCountN +1;
ScenEdit_SetKeyValue('MerchCounter', tostring(lastCountN));
b=ScenEdit_GetKeyValue('MerchCounter');
if b ~= 0 then
local new_merch = ScenEdit_AddUnit({side='Civilian', type='Ship',name='Merchant #'..b, dbid=DBID, base=PORT})
end
ScenEdit_AssignUnitToMission( new_merch.name, 'MISSION')
print (new_merch.name..' with dbid '..DBID..' was created in '..PORT..' on mission '..MISSION..'')