PDA

View Full Version : Typecasting DB Nodes



dulux-oz
April 2nd, 2014, 14:36
Hi Guys,

Does FG has a caste() or typecaste() function? I want to copy a DB String Node to a DB Number Node.

Thanks

Trenloe
April 3rd, 2014, 03:40
I don't think so. You'll have to get the data from the source with getValue() then set the type when you create the new database node with either https://www.fantasygrounds.com/refdoc/databasenode.xcp#createChild (based off an existing databasenode and creating a child to that database node) or if you're using the DB package: https://www.fantasygrounds.com/refdoc/DB.xcp#createNode or https://www.fantasygrounds.com/refdoc/DB.xcp#setValue or https://www.fantasygrounds.com/refdoc/DB.xcp#createChild

dulux-oz
April 3rd, 2014, 04:18
OK, thanks Trenloe - just a thought, I'll see if I can come up with another solution. :)

Cheers

Trenloe
April 3rd, 2014, 13:20
Unless there is something written as a helper function in CoreRPG. If not, write a helper function yourself. It would be a few lines of code at the most. Pass the source database node and target node name to the function, get the value of the source node, check if the string can be converted to a valid number and then use DB.setValue(targetnode, type, value). 3-5 lines of code max, put the helper function in a new manager .lua file, load the file with a name (e.g. CopyNodeHelper) from base.xml (or extension.xml) and then reference the name.function when you use the the new helper function whenever you need - e.g. CopyNodeHelper.copyStringNodeToNumberNode(source node, targetnode)

dulux-oz
April 3rd, 2014, 13:43
Unless there is something written as a helper function in CoreRPG. If not, write a helper function yourself. It would be a few lines of code at the most. Pass the source database node and target node name to the function, get the value of the source node, check if the string can be converted to a valid number and then use DB.setValue(targetnode, type, value). 3-5 lines of code max, put the helper function in a new manager .lua file, load the file with a name (e.g. CopyNodeHelper) from base.xml (or extension.xml) and then reference the name.function when you use the the new helper function whenever you need - e.g. CopyNodeHelper.copyStringNodeToNumberNode(source node, targetnode)

Yeah, that's basically what I've done - thanks mate. :)

Moon Wizard
April 4th, 2014, 01:54
There are actually several cast functions built into Lua to convert between number and string.

You can use [tonumber(string) or 0] to get a number from a string.

And you can use to ["" .. number] or [tostring(number)] or [string.format("%d", number)] to convert a number to a string.

Cheers,
JPG

dulux-oz
April 4th, 2014, 03:55
Yeah, thanks Moon - that's basically what I used :)

Cheers