-
March 16th, 2019, 20:16 #11
- Join Date
- May 2007
- Posts
- 84
When defining your function, you did not return the value, just called the function.
Code:["strength"] = function (arg) AbilityScoreBonusesManager.updateStrengthRelatedBonuses(arg) end,
Code:["strength"] = function (arg) return AbilityScoreBonusesManager.updateStrengthRelatedBonuses(arg) end,
-
March 16th, 2019, 20:39 #12
LOL, right - because I copied my real method which was performing UI updates
It's the stupid things I usually fall for.
Thanks much for seeing that.
So, now that we verified that it works by calling the method in the other script via the table but only if I do it indirectly - any thoughts as to why this and your onInit() work where if I had just done an unnamed method that calls the method I wanted it works. But if I change your Test3 to what I first tried:
Code:--local aAbilityToUpdateSwitch = nil; local aAbilityToUpdateSwitch = { ["strength"] = AbilityScoreBonusesManager.updateStrengthRelatedBonuses, ["dexterity"] = AbilityScoreBonusesManager.updateDexterityRelatedBonuses, ["constitution"] = AbilityScoreBonusesManager.updateConstitutionRelatedBonuses, ["intelligence"] = AbilityScoreBonusesManager.updateIntelligenceRelatedBonuses, ["wisdom"] = AbilityScoreBonusesManager.updateWisdomRelatedBonuses, ["charisma"] = AbilityScoreBonusesManager.updateCharismaRelatedBonuses, };
Code:Runtime Notice: Reloading ruleset Ruleset Warning: Font (windowtitle): Windows replaced specified font face (Alegreya Sans SC Bold) with (Alegreya Sans SC) Script Error: [string "test.lua"]:3: attempt to index global 'AbilityScoreBonusesManager' (a nil value)
-
March 16th, 2019, 20:51 #13
- Join Date
- May 2007
- Posts
- 84
This may be right or wrong, but this would be my understanding.
When you are trying to use AbilityScoreBonusesManager directly, it is being called before everything is initialized so it does not exist yet. So the system cannot get the pointer to the functions, so you get nil. If you wait until "onInit()", now AbilityScoreBonusesManager exists and the system can see its functions.
When you wrap it in a function, you are defining the function to use right then so that function exists and can be assigned. The body of the function (which contains the call to AbilityScoreBonusesManager) is not evaluated until when called at run-time. By then, AbilityScoreBonusesManager exists.
-
March 16th, 2019, 21:25 #14
- Join Date
- May 2007
- Posts
- 84
Also, just to throw ideas out there. Since your functions are all named based on the name of the ability, you could also get rid of the table and use this instead (which finds the correct function based on its name.) Not saying you should do it this way, just providing another alternate.
Code:function aAbilityToUpdateSwitch2(arg) return AbilityScoreBonusesManager["update" .. arg:gsub("^%l", string.upper) .. "RelatedBonuses"]; end
Code:local func = aAbilityToUpdateSwitch2(nodeScore);
or even just in-line it like:
Code:local func = AbilityScoreBonusesManager["update" .. nodeScore:gsub("^%l", string.upper) .. "RelatedBonuses"];
Last edited by gamerhawaii; March 16th, 2019 at 21:28.
-
March 16th, 2019, 21:31 #15
That is a succinct explanation and it makes sense to me.
I wondered as I said in prior response to your first onInit() solution if the second worked because only the "topmost" level is being evaluated immediately and as you said the method is created locally and is therefore visible at that time.
That the method being called is not yet defined is fine, it is not attempted until the method is called and by then it is defined and known.
Thanks a lot for your patience and efforts in helping me understand all of this.
The first part as I said before made good sense immediately. It may or may not be defined by the time this script is loaded. Putting it on onInit()of what I felt was the answer (the se
-
March 24th, 2019, 03:29 #16
Hey, again as before thanks I decided to try your regex/sub idea as I may want to use it because while I like the explicitness of the table method (I am a big one, for example, in code for passing args versus defining "default args" other than when adapting code) I do not like that it creates a method in the table which calls a method. So lookup to call a method which calls a method is one too many calls for indulging in my "purity" ;P
I received an error however pointing to "gsub" being nil.
I fixed it as indicated, but posting in CASE I misused the original example or my version was naive (as I have little lua practice still and other languages creep in to mess me up at times.)
Code:function updateAbilityBonuses(nodeScore) local func = aAbilityToUpdateSwitch[nodeScore.getParent().getName()]; Debug.console(nodeScore); abilName = string.gsub(nodeScore.getParent().getName(), "^%l", string.upper); Debug.console("update" .. abilName .. "RelatedBonuses"); -- This one gave the error: Debug.console("update" .. nodeScore:gsub("^%l", string.upper) .. "RelatedBonuses"); ...
So, rather than waste processing with gsub, etc - I can simply rename the method to be lowercase ;P it only slightly bothers me not to be camelCased. Probably I will change those methods to space_seperated_words to make my twitching stop.
But yeah, thanks - I will now remember and use the fact that I can basically create function names because they are merely syntactic sugar over the whole table["key"] thing anyway
-
March 24th, 2019, 03:31 #17
OK - I compromised since _-separated stuck out too much:
---> update_strengthRelatedBonuses
Now, thanks to you, a single line method:
Code:function updateAbilityBonuses(nodeScore) AbilityScoreBonusesManager["update_" .. nodeScore.getParent().getName() .. "RelatedBonuses"](nodeCharFromAbilityScore(nodeScore)); end
Last edited by Varsuuk; March 24th, 2019 at 03:45.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks