STAR TREK 2d20
Page 2 of 2 First 12
  1. #11
    When defining your function, you did not return the value, just called the function.

    Code:
    		["strength"] = function (arg) AbilityScoreBonusesManager.updateStrengthRelatedBonuses(arg) end,
    to this works:

    Code:
    		["strength"] = function (arg) return AbilityScoreBonusesManager.updateStrengthRelatedBonuses(arg) end,
    Attached Files Attached Files

  2. #12
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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,
    	};
    it is back to:
    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)

  3. #13
    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.

  4. #14
    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
    then your call is like this instead:


    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.

  5. #15
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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

  6. #16
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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");
    ...
    Now, mind, you - I just never THOUGHT of "pasting together" the table name so that alone was a great service to me - to bump me out of my old C++ brain (and to lesser extent, Java now that I am forced to be full-time Java since August - one day I'll finish reading a good book on it lol... no need to worry that all FIX/OUCH Treasury/Bond traffic for NASDAQ goes through code I wrote on my own via googling about Java )

    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



    Quote Originally Posted by gamerhawaii View Post
    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
    then your call is like this instead:


    Code:
    	local func = aAbilityToUpdateSwitch2(nodeScore);

    or even just in-line it like:
    Code:
    	local func = AbilityScoreBonusesManager["update" .. nodeScore:gsub("^%l", string.upper) .. "RelatedBonuses"];

  7. #17
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
DICE PACKS BUNDLE

Log in

Log in