PDA

View Full Version : Accessing variables in lua script files



Varsuuk
March 15th, 2019, 01:31
[SOLVED]

I am about to head to eat dinner and help boy with HW. I just got home and had intended on trying to figure out what last night I didn't get right:

I thought this was simple, but there is probably nuance I must learn?

I have a file listed in base.xml using the <script name="SomeName" file="paphtoscript.lua">

in pathtoscript.lua I didn't yet do an onInit (though tried that right after) because was just mucking to learn and I defined some arrays and a simple numeric value.

I then inside the script of a control (embedded number based control) I did something like:

<script>
function onInit()
setValue(SomeName.myVar)
end

initially tried with aArray[15].someIndexName and it said aArray was nil
So went to a simpler version above. This did not report an error in console but also didn't fill in the control.

doing:
setValue(3)

did set it to 3

putting
myVar = 3
in the .lua file and then my first try above of setValue(SomeName.myVar) still nothing...



*taking out hand for facepalm*

Going to grab food and will see if my obvious error was caught in the break before I go help with his HW so I can assuage my damn curious mind from thinking about it. :)

Moon Wizard
March 15th, 2019, 01:55
Make sure that the variable defined in your global script is not marked with the "local" keyword. If so, it won't be able to be accessed outside the functions in that script file only.

JPG

Varsuuk
March 15th, 2019, 02:08
aVAR = {}

Below is the contents of the Lua file sans the REAL variables I created - nested array/dictionaries - when that didn't work I said, yeah I should have toddled before flying and did the stuff below



nFoo = 1;
aVAR = {}


function onInit()
nFoo = 5;
aVAR[1] = {6};
end





<number_bonusfield_base name="strength_bonustohit"
source="abilities.strength.bonustohit_melee">
<anchored>
<top parent="strength" anchor="top" />
<right parent="frame_abilitybonus" anchor="center" offset="-10" />
</anchored>
<script>
function onInit()
bar = 3;
setValue(CharAbility.aVAR[1]);
(setValue(bar); worked...)
end
</script>
</number_bonusfield_base>

tried with other vars and accessing of it:

setValue(CharAbility.nFoo);
CharAbility.aStrength[15].tohit_mod --> (the "15" was originally read from a control and when didn't work I hardcoded to verify)



I'll check back in 45-60 mins have to run

Moon Wizard
March 15th, 2019, 02:40
As long as your global script is actually being read into memory and has those variables; then the references should work.

Make sure the global script is initializing by using Debug.console statement in onInit function of global script, which also outputs the variables to double-check.

Regards,
JPG

Varsuuk
March 15th, 2019, 03:23
Good call - I kept looking at the base.xml, copied the assigned name and searched on it to ensure I didn't typo (today at work I kept reading a yaml config file which had "sytem.warmup.orderPoolSize=..." and missing the obvious. I was tired it was one line of many... sigh...

I bet you are right, on phone atm - soon as get to desk I will print on onInit() and see that is it somehow. Since you verified the simple example was right - only thing left is I am not referencing the thing I thought I was.

Varsuuk
March 15th, 2019, 04:06
[SOLVED]


OK - I told my boy he can watch you tube in bed for 20 so I could look into this while he stayed up past reasonable child bedtime lol (Father of the Year...? OK, no)

Looking at console:


Runtime Notice: Reloading ruleset
Ruleset Warning: Font (windowtitle): Windows replaced specified font face (Alegreya Sans SC Bold) with (Alegreya Sans SC)
Script Error: [string "scripts/data_char_ability.lua"]:8: table index is nil
...

Didn't see the above due to noise of 8 other font warnings (due to partially copied over stuff that didn't get to)

SO, I didn't get to the printing thing because of what you said I read more closely. Well, was EASIER cos now I am connected to my desktop monitor instead of a laptop screen. Frigging eyes taint was they used to be (and they USED to be pretty crappy I might add)

Points to the first line of the dictionary... the []= part.
So, commented out the strength mods array.

The next dictionary flagged. Same spot. Start of the dictionary, first entry.

THATS when I realized I had read in the learning Lua book that:

myDictionary = {
["key1"] = "data1",
["key2"] = "other",
... etc }



That for "valid lua identifier strings" you can replace with dot notation.
Like:

local otherStr = myDictionary["Key1"];

can be written as:

local otherStr = myDictionary.key1;


So... guess what my offending code looked like (and entire file of similar stuff):



--[[Table 1: Strength]]
aStrength = {
--[[ 3-4 ]]
[3] = {[tohit_mod] = -2, [dmg_mod] = -1, [opendoors_low] = 1, [opendoors_high] = 1, [carry_mod] = -10},
[4] = {[tohit_mod] = -2, [dmg_mod] = -1, [opendoors_low] = 1, [opendoors_high] = 1, [carry_mod] = -10},
--[[ 5-6 ]]
[5] = {[tohit_mod] = -1, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 1, [carry_mod] = -5},
[6] = {[tohit_mod] = -1, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 1, [carry_mod] = -5},
--[[ 7-9 ]]
[7] = {[tohit_mod] = 0, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 0},
[8] = {[tohit_mod] = 0, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 0},
[9] = {[tohit_mod] = 0, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 0},
--[[ 10-12 ]]
[10] = {[tohit_mod] = 0, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 5},
[11] = {[tohit_mod] = 0, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 5},
[12] = {[tohit_mod] = 0, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 5},
--[[ 13-15 ]]
[13] = {[tohit_mod] = 1, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 10},
[14] = {[tohit_mod] = 1, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 10},
[15] = {[tohit_mod] = 1, [dmg_mod] = 0, [opendoors_low] = 1, [opendoors_high] = 2, [carry_mod] = 10},
--[[ 16 ]]
[16] = {[tohit_mod] = 1, [dmg_mod] = 1, [opendoors_low] = 1, [opendoors_high] = 3, [carry_mod] = 15},
--[[ 17 ]]
[17] = {[tohit_mod] = 2, [dmg_mod] = 2, [opendoors_low] = 1, [opendoors_high] = 4, [carry_mod] = 30},
--[[ 18 ]]
[18] = {[tohit_mod] = 2, [dmg_mod] = 3, [opendoors_low] = 1, [opendoors_high] = 5, [carry_mod] = 50},
};

--[[Table 2: Dexterity]]
aDexterity = {
--[[ 3-8 ]]
[3] = {[missle_to_hit_mod] = -1, [ac_mod] = -1},
[4] = {[missle_to_hit_mod] = -1, [ac_mod] = -1},
[5] = {[missle_to_hit_mod] = -1, [ac_mod] = -1},


Note, I wrote the keys as if they WERE identifiers. DAMN THE SYNTACTIC SUGAR leading me unto sin...
Yeah, need to put quotes there even if plan on using "dot notation" in access.


-------

So now the following line:
setValue(CharAbility.aStrength[window.strength.getValue()].tohit_mod);

worked like a charm!

Thanks so much for making me see my mistake.

PS, lots of code to parentheticalize (yeah, I made that up), so I used a RegEx in Eclipse Search & Replace, in case anyone else does this:
Find:
\[(\D+)\]
Replace:
\[\"\1\"\]

(eyeball it though, I am NOT that good with RegEx and it may not work in other situations)