lua KeyValue Problem

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
ExNusquam
Posts: 531
Joined: Mon Mar 03, 2014 11:26 pm
Location: Washington, D.C.

lua KeyValue Problem

Post by ExNusquam »

OK, this is definitely me being stupid, but I can't figure out how to make this work for the life of me. I'm trying to set this up so that the first helicopter trip drops units off, and the second trip picks them back up, using keyvalue to keep track of the state.

Code: Select all

val = ScenEdit_GetKeyValue('LZ')
 if val == 0
 ScenEdit_AddFacility('Test', 'Landing Zone', 248, 0 , 'DEC', '11.1973239792203', '49.0965833574316')
 ScenEdit_SetKeyValue('LZ', '1')
 if val == 1
 ScenEdit_DeleteUnit({side="Test", name="Landing Zone"})
 ScenEdit_SetKeyValue('LZ', '0')
 end
 
The event is firing...and the individual actions are working through the console...so I know that the problem is how I've implemented the keyvalue. Any help?
Yokes
Posts: 298
Joined: Tue Mar 13, 2007 9:27 pm

RE: lua KeyValue Problem

Post by Yokes »

Should the second "if" be an "elseif"?

I'm not very familiar with lua yet, but in many other languages an "if" statement can only be followed by an "else", "elseif", or "end" statement.

Yokes
RoryAndersonCDT
Posts: 1828
Joined: Mon Jun 15, 2009 11:45 pm

RE: lua KeyValue Problem

Post by RoryAndersonCDT »

ORIGINAL: Yokes

Should the second "if" be an "elseif"?

I'm not very familiar with lua yet, but in many other languages an "if" statement can only be followed by an "else", "elseif", or "end" statement.

Yokes
Quite right! I'd recommend making sure that val is set to a value, if its the first time GetKeyValue is called on 'LZ' it might be blank. Also note that GetKeyValue will always return a string:

This might work:

val = ScenEdit_GetKeyValue('LZ')

if val == nil or val == ''
val = '0'
end

if val == '0'
ScenEdit_AddFacility('Test', 'Landing Zone', 248, 0 , 'DEC', '11.1973239792203', '49.0965833574316')
ScenEdit_SetKeyValue('LZ', '1')
end

if val == '1'
ScenEdit_DeleteUnit({side="Test", name="Landing Zone"})
ScenEdit_SetKeyValue('LZ', '0')
end

Command Dev Team
Technical Lead
ExNusquam
Posts: 531
Joined: Mon Mar 03, 2014 11:26 pm
Location: Washington, D.C.

RE: lua KeyValue Problem

Post by ExNusquam »

No joy on that one baloogan...the event is firing, but nothing happens. I have ensured that the keyvalue is set using the console before it first fires. The individual events fire, but I can't seem to get it to work with the if/ifelse option.
mx1
Posts: 76
Joined: Thu Jan 16, 2014 6:01 pm

RE: lua KeyValue Problem

Post by mx1 »

Isn't the proper syntax in Lua like this?

if COND then
BLOCK
elseif COND then
BLOCK
...
else
BLOCK
end

It seems that then keyword is missing from both examples.
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: lua KeyValue Problem

Post by tjhkkr »

ORIGINAL: mx1

Isn't the proper syntax in Lua like this?

if COND then
BLOCK
elseif COND then
BLOCK
...
else
BLOCK
end

It seems that then keyword is missing from both examples.
You beat me to it. [:'(][;)][:D]
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
magi
Posts: 1533
Joined: Sat Feb 01, 2014 1:06 am

RE: lua KeyValue Problem

Post by magi »

you guys are so smart.....
Post Reply

Return to “Mods and Scenarios”