View Full Version : Why isn't this working :P?
Visvalor
December 4th, 2009, 23:11
Trying to call a string value up from a labeled string and then assign a variable number to the racial name xD
<script>
function update()
race_variable = racevar.getValue()
setValue(race_variable)
end
function onInit()
source = window.race.getDatabaseNode()
source.onUpdate = update
if source = Dwarf then
racevar = 1
else
racevar = 2
update()
end
</script>
Doesn't work :p why not?
joshuha
December 5th, 2009, 00:30
Think of the source as a pointer to the DB node not a value in itself.
So instead of if source = Dwarf
if source.getValue() == "Dwarf" then
Don't forget in LUA comparison for equal is ==, = is used for assignment.
You'll soon get used to almost any DB node function you want to do requires the function call after the node (in fact most everything you will want to do will require that except for flow control/variable assignment/comparisons).
Visvalor
December 5th, 2009, 09:20
<script>
function update()
race_variable = racevar.getValue()
setValue(race_variable)
end
function onInit()
source = window.race.getDatabaseNode()
source.onUpdate = update
if source.getValue() == "Dwarf" then
racevar = 1
else
racevar = 2
update()
end
</script>
Error? End expected near <eof>
I don't get it?
Ikael
December 5th, 2009, 12:28
You must 'end' every function and condition sentences you make (with the end keyword). In this case your function onInit() is not ended, you should add one additional 'end' keyword to the end of the function
Visvalor
December 5th, 2009, 17:34
Oh.... ha DUH thanks ;D
Visvalor
December 5th, 2009, 17:36
<script>
function update()
race_variable = racevar.getDatabaseNode()
setValue(race_variable)
end
function onInit()
source = window.race.getDatabaseNode()
source.onUpdate = update
if source.getValue() == "Dwarf" then
racevar = 1
else
racevar = 2
end
update()
end
</script>
Attempt to index global racevar (A number value)
Wtf >_<! Now why doesn't it work!?
I am trying to convert a string from a drop down menu to a numerical value so I can attach stat bonuses onto a character sheet :p and skills to a skill sheet.
joshuha
December 5th, 2009, 17:39
Where is racevar coming from? I dont see it defined in that script at all? If its a pointer to another DB node you need to do something similiar to the source assignment you did and you .getValue or .SetValue
Visvalor
December 5th, 2009, 17:41
racevar = 1
else
racevar = 2
that doesn't work o.o?
Visvalor
December 5th, 2009, 17:45
So should it be
racevar.SetValue (1)
? and do I need ; at the end like other languages?
Visvalor
December 5th, 2009, 17:49
<script>
function onInit()
source = window.race.getDatabaseNode()
source.onUpdate = update
if source.getValue() == "Dwarf" then
racevar.SetValue (1)
else
racevar.SetValue (2)
end
update()
end
function update()
race_variable = racevar.getDatabaseNode()
setValue(race_variable)
end
</script>
Attempt to index racevar a nil value :P
I am confused. XD
joshuha
December 5th, 2009, 18:29
I am talking about what is racevar? Is it another control on the character sheet? If so it needs to be assigned like your race control where you did source= window.race.getDatabaseNode().
Visvalor
December 5th, 2009, 19:00
Race is gotten from the drop down scroll, racevar needs to be defined as a number from picking the race, I want it to select if dwarf then 1 if elf then 2 if human then 3 etc etc.
How do I define the racevar variable inside the script if a condition is met? (The final elseif will be race=0 for no race selected or user based input that isn't part of the drop down menu)
Foen
December 5th, 2009, 22:18
If racevar is a straight script variable (rather than a control or database object) then its value is set using the assignment (=) operator and checked using the equality (==) operator.
If it is an object (either a control or a database node) then you need to use getValue() and setValue().
Taking a look at your snippet of script, I'd guess this is a script variable and not an object. In that case you should use assignment to set the value, and use equality to test the value.
Without having eny other script, I'd guess your example should be somthing like:
function onInit()
source = window.race.getDatabaseNode();
source.onUpdate = update;
if source.getValue() == "Dwarf" then
racevar = 1;
else
racevar = 2;
end
update();
end
function update()
race_variable = racevar;
setValue(race_variable);
end
It is also worth using explicit line terminators (;) in script blocks, as new-line has no meaning.
Hope that helps
Foen
Foen
December 5th, 2009, 22:22
I forgot to say that the use of variable names that haven't been previously declared is allowed in Lua, but it makes the variable global to the script. In the case above, the variables source, racevar, and race_variable are implicitly global.
It isn't a bad idea to decalre most variables locally (using something like: local source=nil;) to avoid unintended side-effects.
Just my 2c
Stuart
Visvalor
December 6th, 2009, 01:09
Well your version of the script works but it calls up racevar to be 2 all the time, doesn't go to 1 even if Dwarf is selected. Should I remove the ;? :\
joshuha
December 6th, 2009, 05:13
Well your version of the script works but it calls up racevar to be 2 all the time, doesn't go to 1 even if Dwarf is selected. Should I remove the ;? :\
Try this:
function onInit()
source = window.race.getDatabaseNode();
source.onUpdate = update;
update();
end
function update()
if source.getValue() == "Dwarf" then
racevar = 1;
else
racevar = 2;
end
race_variable = racevar;
setValue(race_variable);
end
By having the logic check only in the onInit it will only be checked once when the control is created on the screen. By putting it in the update as the source DB node changes it will recheck your IF statements.
Foen
December 6th, 2009, 05:35
Like Joshuha says :)
Visvalor
December 6th, 2009, 08:23
Works Perfect!
I Love You :p! <3
Visvalor
December 6th, 2009, 18:21
<script>
function onInit()
source = window.race.getDatabaseNode();
source.onUpdate = update;
update();
end
function update()
if source.getValue() == "Dwarf" then
racevar = 1;
else
racevar = 2;
end
race_variable = racevar;
setValue(race_variable);
end
</script>
! OK It works inside of a numbercontrol but I want to know how I set it up at the beginning of a window as a standalone script to define a bunch of variables before any thing else is? I put it up top and set up a script to call race_variable further down and it doesn't work :P
Ideas?
( I want all variables to be global and able to be called upon without setting up visible controls to them :X )
Powered by vBulletin® Version 4.2.1 Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.