Page 1 of 1

lua KeyValue Problem

Posted: Sun Apr 12, 2015 7:30 pm
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?

RE: lua KeyValue Problem

Posted: Mon Apr 13, 2015 12:29 pm
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

RE: lua KeyValue Problem

Posted: Mon Apr 13, 2015 3:47 pm
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


RE: lua KeyValue Problem

Posted: Wed Apr 15, 2015 10:34 pm
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.

RE: lua KeyValue Problem

Posted: Wed Apr 22, 2015 4:10 pm
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.

RE: lua KeyValue Problem

Posted: Sat Apr 25, 2015 12:23 am
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]

RE: lua KeyValue Problem

Posted: Sat Apr 25, 2015 2:45 am
by magi
you guys are so smart.....