-
March 24th, 2019, 05:04 #1
Accessing XML parameters from script
While trying to learn how to access the params as indicated in the page: https://www.fantasygrounds.com/modguide/scripting.xcp - I ran into misunderstandings and confusion. In the end as I was writing the question, it kept sparking ideas to try and eventually all my questions were answered and I deleted all but the basic part asking if this type of method/code/algo already exists in CoreRPG etc already so that I do not (poorly since naive still) reinvent a wheel
Sample XML:
Code:<number_savingthrow name="savingthrow_fire" source="defenses.saves.default.fire"> <anchored> <top parent="label_savingthrow_wandsstaves" /> </anchored> <applies_to> <type>dragon_fire</type> <type>fire</type> <test>abc</test> </applies_to> </number_savingthrow>
Code:... function hasValue(node, value) local isApplicable = false; for _, v in ipairs(node) do if v == value then return true end end return false; end ... Debug.console("Was FIRE found?", hasValue(self.applies_to[1].type, "fire")); ...
-
March 24th, 2019, 05:14 #2
The next step as always is covering the "error" or in other cases "not error" situation where there are no entries for "applies_to"
Code:<applies_to> </applies_to>
Code:Runtime Notice: Reloading ruleset Script Error: [string "campaign/scripts/char_saves_sw.lua"]:91: attempt to index field '?' (a boolean value)
-
March 24th, 2019, 05:39 #3
You can access the xml values via the window but you are far better off accessing it from the db.xml
-
March 24th, 2019, 06:02 #4
Lost me...
The XML in first post refers to xml written for a control window. (PS: I decided NOT to go that way, doing something else not using applies_to but figured still worth learning best ways)
The script in that post is referred to in the template for the control in the above sentence. It is in an external lua file.
I think you mean that it is better to avoid using the "via window" meaning this whole self.tag[number].othertag[number] system?
Cool, but what is the alternative best way? That's what this question was about. How do I given that rough idea in first xml, where I have a repeating tag with different values do I check (sure, using DB.xxx is fine by me if that is what you mean - but example?) for the one I want existing in the repeating tag group?
If there an existing free ruleset that does this (or in C&C, Savage Worlds) you can just tell me what file/method to check for an example if prefer.
-
March 24th, 2019, 06:17 #5
- Join Date
- May 2007
- Posts
- 84
Separately, the reason I believe you got the error you did is because "<applies_to></applies_to>" causes the "applies_to" to have a value of "true".
Look at the example from the page you listed. Empty tags provide a table with a value of "true" for the first entry.
Then your code tried to go to the "type" field of "applies_to[1]" which is a Boolean when "applies_to" tag is empty. Thus the error of trying to access a field on a Boolean field.
(I am a little surprised the error did not say "attempt to index field 'type' (a boolean value)", but it didn't.)Last edited by gamerhawaii; March 24th, 2019 at 06:22.
-
March 24th, 2019, 06:29 #6
Right of course the old “existing tag with no data is interpreted as true thing” I knew that but didn’t think how it applied here. Oops.
So, if I were to use this (I’m not it was an initial idea I discarded) via that function, I’d just need to validate it existed if it was legal for it to not exist. In this case it would have been “bad xml” if it was left blank.
In any event, don’t need the call or the method (yet). But wondered if the method was the right way to do what I wanted: check in a repeating group of a specific value exists - or if there is a better way.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks