Log in

View Full Version : Loading variables ><



Visvalor
May 12th, 2011, 04:52
Hey, I'm back to work on my system, and I'm loading a bunch of variables from the "inventory" page to the main page. Problem is when you create a new character, the variables aren't created yet. So they all bug in the scripting

How can I make it so these variables are all created upon character creation? Put them on the main page and hide them perhaps? Is there a less ghetto way?

phantomwhale
May 12th, 2011, 08:27
Super-keen to help, but feel I need some code snippets here ! That said, I've been coding all day, so maybe others are a little more alert that I right now.

drahkar
May 12th, 2011, 11:33
I would suggest that you have your initial lookup for the databasenode use createChild instead of getChild. They function exactly the same only with createChild, if it doesn't exist yet, then it creates an empty databasenode instead of throwing an error. That or if you have a specific default value you want in it if there is nothing in the node then make a function that your wrap around createChild that says if the return value of getting the information from the database node is nil, create the node with createChild and then set the default value. Otherwise just get it.

Visvalor
May 12th, 2011, 16:57
I put it as createChild, and then it gave me a bunch of arithematic errors. I'll try to figure out how to createChild 0 but won't that result in reseting my variables to 0 each time the charsheet loads?

Visvalor
May 12th, 2011, 17:03
<!--Agility-->
<abilityscore name="agility" source="stats.agility">
<anchored>
<to>vitality</to>
</anchored>
<noreset />
<readonly />
<script>
function update()
Tagi = source.getValue()
Ragi = source2.getValue()
Bagi = source3.getValue()
setValue(Tagi+Ragi+Bagi)
end
function onInit()
source = window.trueagility.getDatabaseNode()
source2 = window.raceagility.getDatabaseNode()
source3 = window.agility_bonus.getDatabaseNode()
source.onUpdate = update
source2.onUpdate = update
source3.onUpdate = update
update()
end
</script>
</abilityscore>
<abilityscore5 name="trueagility" source="trueagility">
<noreset />
<readonly />
</abilityscore5>
<abilityscore name="raceagility" source="stats.race.agility">
<noreset />
<readonly />
<script>
function update()
raceagibase = Globals.raceagi(source)
setValue(raceagibase)
end
function onInit()
setVisible(false);
source = window.getDatabaseNode().getChild("race")
source.onUpdate = update
update()
end
</script>
</abilityscore>
<abilitylabel>
<anchored>
<to>agility</to>
</anchored>
<static>AGI</static>
</abilitylabel>
<abilityscore2 name="agility_bonus" source="stats.agility_bonus">
<anchored>
<to>agility</to>
</anchored>
<noreset />
<script>
function update()
head_agi_bonus = source.getValue()
torso_agi_bonus = source1.getValue()
legs_agi_bonus = source2.getValue()
feet_agi_bonus = source3.getValue()
shoulders_agi_bonus = source4.getValue()
rightarm_agi_bonus = source5.getValue()
leftarm_agi_bonus = source6.getValue()
ring1_agi_bonus = source7.getValue()
ring2_agi_bonus = source8.getValue()
ring3_agi_bonus = source9.getValue()
ring4_agi_bonus = source10.getValue()
necklace_agi_bonus = source11.getValue()
charm_agi_bonus = source12.getValue()
weapon1_agi_bonus = source13.getValue()
weapon2_agi_bonus = source14.getValue()
weapon3_agi_bonus = source15.getValue()
weapon4_agi_bonus = source16.getValue()

setValue(head_agi_bonus + torso_agi_bonus + legs_agi_bonus + feet_agi_bonus + shoulders_agi_bonus + rightarm_agi_bonus + leftarm_agi_bonus + ring1_agi_bonus + ring2_agi_bonus + ring3_agi_bonus + ring4_agi_bonus + necklace_agi_bonus + charm_agi_bonus + weapon1_agi_bonus + weapon2_agi_bonus + weapon3_agi_bonus + weapon4_agi_bonus)
end
function onInit()
source = window.getDatabaseNode().createChild("stats.equipment_head_agi_bonus")
source1 = window.getDatabaseNode().createChild("stats.equipment_torso_agi_bonus")
source2 = window.getDatabaseNode().createChild("stats.equipment_legs_agi_bonus")
source3 = window.getDatabaseNode().createChild("stats.equipment_feet_agi_bonus")
source4 = window.getDatabaseNode().createChild("stats.equipment_shoulders_agi_bonus")
source5 = window.getDatabaseNode().createChild("stats.equipment_rightarm_agi_bonus")
source6 = window.getDatabaseNode().createChild("stats.equipment_leftarm_agi_bonus")
source7 = window.getDatabaseNode().createChild("stats.equipment_ring1_agi_bonus")
source8 = window.getDatabaseNode().createChild("stats.equipment_ring2_agi_bonus")
source9 = window.getDatabaseNode().createChild("stats.equipment_ring3_agi_bonus")
source10 = window.getDatabaseNode().createChild("stats.equipment_ring4_agi_bonus")
source11 = window.getDatabaseNode().createChild("stats.equipment_necklace_agi_bonus")
source12 = window.getDatabaseNode().createChild("stats.equipment_charm_agi_bonus")
source13 = window.getDatabaseNode().createChild("stats.equipment_weapon1_agi_bonus")
source14 = window.getDatabaseNode().createChild("stats.equipment_weapon2_agi_bonus")
source15 = window.getDatabaseNode().createChild("stats.equipment_weapon3_agi_bonus")
source16 = window.getDatabaseNode().createChild("stats.equipment_weapon4_agi_bonus")
source.onUpdate = update
source1.onUpdate = update
source2.onUpdate = update
source3.onUpdate = update
source4.onUpdate = update
source5.onUpdate = update
source6.onUpdate = update
source7.onUpdate = update
source8.onUpdate = update
source9.onUpdate = update
source10.onUpdate = update
source11.onUpdate = update
source12.onUpdate = update
source13.onUpdate = update
source14.onUpdate = update
source15.onUpdate = update
source16.onUpdate = update
update()
end
</script>
</abilityscore2>

<diescore2 name="Agility Check">
<anchored>
<to>agility</to>
</anchored>
<script>
function onClickDown(button, x, y)
setDice({"d20"});
end
</script>
</diescore2>
<modbox name="Agility CheckMod">
<bounds>1,1,1,1</bounds>
<script>
function update()
AgiCheck = source.getValue()
setValue(AgiCheck)
end
function onInit()
setVisible(false);
source = window.getDatabaseNode().getChild("stats.agility")
source.onUpdate = update
update()
end
</script>
</modbox>

<genericcontrol>
<bounds>366,143,18,17</bounds>
<icon>stat_up</icon>
<script>
function onClickDown(button, x, y)
trueagility = window.getDatabaseNode().getChild("trueagility");
statpoints = window.getDatabaseNode().getChild("statpoints");

if statpoints.getValue() > 0 then
trueagility.setValue(trueagility.getValue()+1);
statpoints.setValue(statpoints.getValue()-1);
end
end
</script>
</genericcontrol>

Here is an example of my code. I can't quite figure out the createChild 0 , but I'll try in a sec with replacing all getChild to createChild so that it can get the info.... I just don't want it reseting the char-sheets each time.

drahkar
May 12th, 2011, 17:47
What createChild does is creates a databasenode at the location if it doesn't exist, otherwise it gets the data fro the existing node per getChild. When it is initially created, the databasenode has nothing in it. Its an empty value. You then have to either setValue something to it, OR set your code so that it can recognize that its an empty node and process the data from it accordingly.

Visvalor
May 12th, 2011, 17:57
Could you give me an example as how to do this? How do I tell the code to process accordingly? I really don't want createChild to SetValue every time the charsheet is opened because a few pages down the enchantments are saved.

drahkar
May 12th, 2011, 18:41
createChild doesn't set anything. It merely creates a blank databasenode if it doesn't exist.

For example:



<abilityscore name="agility" source="stats.agility">
<anchored>
<to>vitality</to>
</anchored>
<noreset />
<readonly />
<script>
function onInit()
agi = window.getDatabaseNode().createChild("stats.agility");

agiValue = agi.getValue();

if agiValue ~= nil then
else
setValue(10);
end
end
</script>
</abilityscore>

That would check to see if stats.agility existed. If it didn't then it would create it with the createChild function. Then it would proceed to check to see if that databasenode had a value in it. At that point if it didn't have a value, it would then set a value to it as a default value (In this case a value of 10. Otherwise it would do nothing.

I would also consider adding this code into the abilityscore template instead of having it here. Instead having it be something line this:



<template name="abilityscore">
<numberfield>
<anchor information>
<other information and layout data>
<statname mergerule="replace">
<script>
function onInit()
statNode = window.getDatabaseNode().createChild("stats." .. statname[1]);

statValue = statNode.getValue();

if statValue ~= nil then
else
setValue(10);
end
end
</script>
</numberfield>
</template>

<abilityscore name="agility" source="stats.agility">
<anchored>
<to>vitality</to>
</anchored>
<noreset />
<readonly />
<statname>agility</statname>
</abilityscore>

As you can see that will let you streamline the code to apply to anything you put into the abilityscore template without having to recopy the same code over and over.

Does this help explain what I mean?

Visvalor
May 12th, 2011, 22:03
This definitely explains what you mean but the structure is completely foreign to me.


<statname mergerule="replace">
<script>
function onInit()
statNode = window.getDatabaseNode().createChild("stats." .. statname[1]);

statValue = statNode.getValue();

if statValue ~= nil then
else
setValue(10);
end
end
</script>

What function does <statname mergerule="replace"> have?

So;

statNode = window.getDatabaseNode().createChild("stats." .. statname[1]);

This would pull all the stats.agility.whatever and pool them together? I might have to restructure my entire ruleset to accomadate this... I also don't see an update() command. Does it update automatically in this structure?

drahkar
May 12th, 2011, 23:05
<statname mergerule="replace"> passes into the code as statname[1].

This will create a single node stats.agility
statNode = window.getDatabaseNode().createChild("stats." .. statname[1]);

This is a relatively basic process that, when the control is initialized, it runs this code. It doesn't have an onUpdate applied to it, but the information within this is easily adaptable for that purpose. I just wrote out the example in the simplest process so speeds.

Visvalor
May 13th, 2011, 02:22
May be relatively basic process to you, but it's greek to me :P

How does it pull up all the different stats from the bonuses?

Moon Wizard
May 13th, 2011, 18:42
One thing I noted from the code that could be an issue. When you use createChild, you need to make sure that you specify a data type for the node to be created. Otherwise, you end up creating an untyped (or list) node by default. Once you have the wrong node type created in the database, the specific number/string controls set to those database locations will not link up correctly, or may not even appear.

Regards,
JPG

Moon Wizard
May 13th, 2011, 18:49
Also, you should check out the linkednumber template from either the d20 or 4E rulesets. The d20 ruleset is simpler, so you might start there. It is available for download from the Downloads section.

Then, you can define fields that total up other fields like this:


<linkednumber name="agility" source="stats.agility">
<anchored>
<to>vitality</to>
</anchored>
<noreset />
<readonly />
<source>
<name>trueagility</name>
<op>+</op>
</source>
<source>
<name>stats.race.agility</name>
<op>+</op>
</source>
<source>
<name>stats.agility_bonus</name>
<op>+</op>
</source>
</linkednumber>


Even if you want to write your own, looking at the examples in the d20 ruleset, especially on the character sheet, will be helpful.

Regards,
JPG