STAR TREK 2d20
  1. #1
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075

    Merging confusion

    Looking at:
    https://fantasygroundsunity.atlassia...or+code+re-use
    \

    I cannot seem to figure out a basic thing. I want to redo a windowclass laying things out differently.
    My initial plan was to copy the one in MoreCore then to modify with delete or replace the ones I needed to adjust.

    This worked so far, eventually I'll decide if should depart completely or do join/replace/delete type edits.
    When I got to the XP, XP-Needed and Level controls (number+widget) I wanted to center the widget label. I looked and found the template in MoreCore's template_campaign.xml:
    Code:
    	<template name="number_labeled">
    		<basicnumber>
    			<frame mergerule="replace" name="fielddark" offset="4,2,4,2" />
    			<font>sheettext</font>
    			<lineoffset default="on">1</lineoffset>
    			<script>
    				labelwidget = nil;
    			
    				function onInit()
    					if labelres then
    						labelwidget = addTextWidget("sheetlabelinline", string.upper(Interface.getString(labelres[1])));
    					else
    						labelwidget = addTextWidget("sheetlabelinline", string.upper(label[1]));
    					end
    					if labelwidget then
    						local w,h = labelwidget.getSize();
    						labelwidget.setPosition("bottomleft", w/2, h/2-4);
    					end
    				end
    			</script>
    		</basicnumber>
    	</template>
    NOTE: I am working on an older FG/MoreCore since I do not regularly update on my dev box.

    I am sure I just don't understand the whole XML rules as "simple" as they seem to be.
    In all versions of the documentation - I often get confused with the terms used to describe the parent/child base/derived relationships and I often think that it is saying the thing you are USING (preexisting definition) needs to have "replace", for example, for it to work - I'm just still all confused. Normally, I have gotten the far by just ... trying stuff until works way I expect then I do it that way.

    Right now, my first attempt was to take one of the controls and simply add (nothing was there before) the <script> section, taking it from the template just so I can test the layout before engaging in "merges":
    Code:
    			<number_labeled name="exp">
    				<anchored to="weight" position="right" offset="10,0" width="60" />
    				<labelres>char_label_experience</labelres>
    				<script>
    					labelwidget = nil
    				
    					function onInit()
    						if labelres then
    							labelwidget = addTextWidget("sheetlabelinline", string.upper(Interface.getString(labelres[1])))
    						else
    							labelwidget = addTextWidget("sheetlabelinline", string.upper(label[1]))
    						end
    						if labelwidget then
    							local w,h = labelwidget.getSize()
    							labelwidget.setPosition("bottom", 0, h/2-2)
    						end
    					end
    				</script>
    			</number_labeled>
    That worked the way I wanted. So, I figure instead of copy-pasting that <script> to each of the 3 places I needed it, let me just modify the template by deriving a new one from it. BUT, before I did that, let me test copying and modifying the original just to make sure I understand stuff and to limit changes per iteration. I've been bitten by doing too much then having trouble finding a culprit when it stopped working.

    So I tried changing the next one:
    Code:
    			<number_labeled name="expneeded">
    				<anchored to="exp" position="right" offset="5,0" width="60" />
    				<labelres>char_label_expneeded</labelres>
    			</number_labeled>
    to:
    Code:
    			<number_labeled2 name="expneeded" merge="replace">
    				<anchored to="exp" position="right" offset="5,0" width="60" />
    				<labelres>char_label_expneeded</labelres>
    			</number_labeled2>
    It does NOT work (meaning the updated script is no used) - the control and widget appear as if it was the original <number_labeled> template.

    Any idea what I am doing wrong? I mean, I can just use the explicit (copy paste ) script but...

  2. #2
    When using a windowclass merge, the controls are matched using a two-part key of the template/control tag as well as the name. You cannot replace only by name.

    If you really need to change a template, you have to copy the template, and replace.
    If you just want to make minor changes, then use a windowclass merge, and specify the additional tags.
    If you really want to fully replace a control with a different template, you would have to use a merge="delete" to remove the original one; and an insertbefore="<name>" to add a new one in the right position.

    It's almost always easier to do the windowclass merge:
    Code:
    	<windowclass name="charsheet_classes" merge="join">
    		<sheetdata>
    			<basicnumber name="expneeded">
    				<script>
    					function onInit()
    						if super and super.onInit then
    							super.onInit();
    						end
    						[CUSTOM CODE]
    					end
    				</script>
    			</basicnumber>
    		</sheetdata>
    	</windowclass>
    JPG

  3. #3
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    Thanks for the explanation

    The last thing you typed is what I ended up doing as I mentioned in the prior post (in bold) - only I had to do it in 3 places and I wanted to see if I could avoid cut and paste in 3 places.
    I do not think there would be anything wrong with copying the template and replacing it since I prefer the aesthetic I chose (of course I'd have to see where it is used elsewhere to see if it holds as my choice for those places too if any exist) but I liked the idea of an "alternate" one better.

    I tried the delete and then add thing you described - BUT I did not do insertbefore - I'll try that fast later tonight because I am the type to want to see how stuff works - I already committed the "copy-paste" version but if the insert before alt template thing works - that's probably "prettier" way.

    Thanks for all that info,
    Dan

  4. #4
    Varsuuk's Avatar
    Join Date
    Dec 2015
    Location
    New York
    Posts
    2,075
    Bah - I switch work and personal Macs and tried it ;P

    Code:
    ...
    			<number_labeled name="expneeded" merge="delete" />
    			<number_centered_labeled name="expneeded" insertbefore="level">
    				<anchored to="exp" position="right" offset="5,0" width="60" />
    				<labelres>char_label_expneeded</labelres>
    			</number_centered_labeled>
    ...
    and the template which only changes the script, leaving all the anchoring, traits, etc as original template:
    Code:
    ...
    	<template name="number_centered_labeled">
    		<number_labeled>
    			<script>
    				labelwidget = nil;
    			
    				function onInit()
    					if labelres then
    						labelwidget = addTextWidget("sheetlabelinline", string.upper(Interface.getString(labelres[1])));
    					else
    						labelwidget = addTextWidget("sheetlabelinline", string.upper(label[1]));
    					end
    					if labelwidget then
    						local w,h = labelwidget.getSize();
    						labelwidget.setPosition("bottom", 0, h/2-2);
    					end
    				end
    			</script>
    		</number_labeled>
    	</template>
    ...
    Now works as before - but without the duplicated code! "insertbefore=" was the key part I had been missing.

    Thanks,
    Dan

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
  •  
5E Character Create Playlist

Log in

Log in