PDA

View Full Version : Adding sources?



Visvalor
February 21st, 2009, 06:28
Where it says source="thingy.stats" or whatever, how do I get it to define thingy1.stats and thingy2.stats in 2 different windows and then on a 3rd have it display the added/multiplied/subtracted/divided combination of 2 different user inputs?

My charsheets rely heavily on being able to do this

Any help is appreciated

~V

Visvalor
February 21st, 2009, 14:31
Looking at the FGWiki I can see a hint to the answer I'm looking for when it says under scripts

"Getting and handling values"

Ok so this is what it says

function update()
base = source.getValue()*0.5
setValue(base*base)
end
function onInit()
source = window.strength.getDatabaseNode()
source.onUpdate = update
update()
end

Ok so now how do I get the first value, add math to it and then display it on the charsheet? How do I assign a value to the edited value?

~V

Oberoten
February 21st, 2009, 23:21
Check your private messages. :)

- Obe

Culdraug
February 21st, 2009, 23:55
Check your private messages. :)

- Obe

Ack! I was hoping to see this answer also. :)

Oberoten
February 22nd, 2009, 08:30
Let me then post the PM here :

Sure. :) I'll pluck a piece of code from my own charactersheet. (Charsheet_main.lua)






<numberfield name="age" >
<bounds rect="410,46,35,20" />
<script>
function onValueChanged()
local flag;
local mult1;
local mult2;
flag = window.getDatabaseNode().getChild("bonusxp").getValue() print ("Flag = " .. flag);
if flag ~= 0 then
return true;
else
mult1 = window.getDatabaseNode().getChild("age").getValue()
mult2 = window.getDatabaseNode().getChild("xpperar").getValue()
window.getDatabaseNode().getChild("xpbase").setValue( mult1*mult2 );
end
end
</script>
</numberfield>


This is the code defining the variable age as a number. It will first put the age spot at X=410, Y=46 and with a width=35, height = 20.

Then begins the true magic that is the Lua-Script...


Code:



Function OnValueChanged()

When the value of the parent-node is changed, it will fire up the code contained in this snippet.


Code:




local flag;
local mult1;
local mult2






This just gives me a few variable names to store retrieved data in. To pull a value from elsewhere on the charactersheet... you use :


Code:


multi1 = window.getDatabaseNode().getChild("age").getValue()



This finds the value of the database node called "age" and stores it's value in Multi1

Lets now say that you do this for several values.

Multi1 to Multi2 and Multi3 etc.

You can then do almost any arithmetic you care to name on them by normal programing handling.

IE Multi1 + Multi2 / Multi3. So much for retrieving data. Now... I bet you want to store all this somewhere as well...


This is where

Code:




window.getDatabaseNode().getChild("age").setValue( Multi1 )





Would come in and sets the value of age to whatever value is stored in Multi1 instead.

Happy bug-squishing.

- Obe