FG Spreadshirt Swag
  1. #1

    How can i shorten this code?

    My neverending quest of designing my custom ruleset continues...

    This time i have been successful in finish the character sheet and its features. However, i think it's not really optimized. There's a ton of repetitive code for each of the many attributes of the sheet, the only difference being the name of the attribute itself in the code. I would like to make a custom script that would take all this repetitive functions in one place, so i could pass just one single parameter on each attribute, instead of repeating the whole code.

    This is an example:

    <specialization name="str_spec">
    		function onInit()
    			if getValue() == "" then
    				setValue("No Specialization")
    		function onValueChanged()
    <stringcontrol name="strength">
    		function onDoubleClick()
    			local value = window.str_points.getValue()
    			local dicepool = ChatManager.d10Check(value)
    			local diff = 6 + ModifierStack.getSum()
    			if Input.isShiftPressed() then
    				ActionsManager.performRoll(nil, nil, "Strength", diff, bSecretRoll, dicepool)
    		function onDrop(x,y,dragdata)
    			if Input.isControlPressed() then
    				window.str_spec.setValue("Speciality: " .. dragdata.getStringData())
    <standard_points name="str_points">
    If, let's say, i start a new file and register it as "CustomScript:

    How could i take these repetitive scripts in a way that i could just call a function like CustomScript.shorten("attribute_name") instead of repeat the whole code? I tried myself but i having trouble in figure out how can i make so that this new file knows where the node is when a function calls it from another file...
    (as you can see, i have limited LUA and FG structure knowledge. Please, be patient with me... )
    "A saint does what is right. A leader does what is necessary."

  2. #2
    You could do what you are saying; or you could make a template that reads information from a XML tag in the control definition.

    For an example of the latter, try looking at the template number_charabilityscore in the 5E ruleset that uses the "target" tag.

    See these references for that example:
    campaign/template_char.xml (for template definition),
    campaign/template_campaign.xml (for number_abilityscore child template definition),
    campaign/scripts/number_abilityscore.lua (for child template script),
    campaign/record_char_main.xml (for actually control definition using template with target tags)

    Last edited by Moon Wizard; September 13th, 2021 at 03:52.

  3. #3
    I have to trust Moon in relation to templates because I've not yet used them. However, to answer the question "how can i make so that this new file knows where the node is when a function calls it from another file" you can pass a reference to self or window depending on where it ends up being called from. Then in the registered Lua file you can find out thru that passed variable who called you.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Starfinder Playlist

Log in

Log in