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:

    Code:
    <specialization name="str_spec">
    	<script>
    		function onInit()
    			if getValue() == "" then
    				setValue("No Specialization")
    			else
    				window.strength.setTooltipText(getValue())
    			end
    		end
    					
    		function onValueChanged()
    			window.strength.setTooltipText(getValue())
    		end
    	</script>
    </specialization>
    
    <stringcontrol name="strength">
    	<anchored>
    		<to>frame_physical</to>
    		<position>insidetopleft</position>
    		<offset>25,28</offset>
    	</anchored>
    				
    	<static>Strength</static>
    				
    	<script>
    		function onDoubleClick()
    			local value = window.str_points.getValue()
    			local dicepool = ChatManager.d10Check(value)
    			local diff = 6 + ModifierStack.getSum()
    							
    			if Input.isShiftPressed() then
    				window.pool.setValue(value)
    				window.pool_name.setValue(getValue())
    			else
    				ActionsManager.performRoll(nil, nil, "Strength", diff, bSecretRoll, dicepool)
    			end
    		end
    					
    		function onDrop(x,y,dragdata)
    			if Input.isControlPressed() then
    				window.str_spec.setValue("Speciality: " .. dragdata.getStringData())
    			end
    		end
    	</script>
    </stringcontrol>
    			
    <standard_points name="str_points">
    	<anchored>
    		<to>strength</to>
    		<position>insidetopleft</position>
    		<offset>95,-2</offset>
    		<size>
    			<width>50</width>
    			<height>20</height>
    		</size>
    	</anchored>
    	<default>1</default>
    </standard_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)

    Regards,
    JPG
    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.

    Jason

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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
  •  
STAR TREK 2d20

Log in

Log in