Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

How to add to, or subtract from, a referenced Variable...

Dad3353

Praised Adventurer
Disclaimer: I'm very new to js (although have a firm grasp on several other languages...); I'm toying with 'lightweight' one-liner script calls, not wanting to get into plug-ins and prototyping and stuff (yet...). I'm hoping that this is an easy one for the more experienced out there with The Knowledge. Here we go...

I've found out how to reference a Variable whose ID is held in another...

$gameVariables.value($gameVariables.value(3))!==0

... where '3' is the Variable holding the ID I'm testing (If Variable n is not zero', 'n' being held in Variable 3...)
My problem is wanting to subract '1' from the Variable n. Here's my latest attempt...

$gameVariables.setValue(($gameVariables.Value(3)),$gameVariables.Value($gameVariables.Value(3))-1)

... which throws an error. This should 'read' as...

Set the value of the Variable referenced in Variable 3 to that same value minus 1 (and so subtracting 1...).
The first part works if I use a constant, thus...

$gameVariables.setValue(($gameVariables.Value(3)),6)

The second part works on its own...

$gameVariables.Value($gameVariables.Value(3)) returns the value referenced in Variable 3

It's when I put them together with or without the subtraction, that it throws the error...

Type Error
Defined is not a function


I'm presuming that there's a Dumbo-style stoopid error there somewhere, and I'm hoping that the more experienced will, with a sly chuckle, ba able to point me to the True Path of Enlightenment, saving me a fortune on aspirin.
Any takers, please..?
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Why not use the control variables event command, I'm pretty sure you can do all the same things you're trying here, though I'm not 100% sure what you're trying to do and there may be a reason you're doing it via scripting, either way, I'll show you both ways to achieve this and give you tips on the way through the process.

Firs thing I noticed in your scripts above was the $gameVariables.Value(); make sure uppercase and lowercase are correct in this situation and in your case, you may have jut accidently did uppercase fro this post only but....Value should be value like so:
JavaScript:
$gameVariables.value(variable);
Naming conventions in Javascript are camelCase, so the first word is always lowercase and the second word is uppercase unless it's a class which is usually named like this: Game_Variables.

Next is your check with the game variable is not equal to 0, simply putting !== after the gameVariable will not work, you will need to use an if statement.

JavaScript:
if($gameVariables.value($gameVariables.value(3)) > 0) {

}
// OR
if($gameVariables.value($gameVariables.value(3)) !== 0) {

}
This will ensure that the gameVariable value is greater than 0, or if you do it your way, it's not equal to 0, either way works.

Next, I noticed you're using an extra parenthesesis after $gameVariables(( <--- You only need one so something like this.

JavaScript:
$gameVariables.setValue($gameVariables.value(3), $gameVariables.value($gameVariables.value(3)) - 1);
With a final code looking like so...
JavaScript:
    if($gameVariables.value($gameVariables.value(3)) > 0) {
        $gameVariables.setValue($gameVariables.value(3), $gameVariables.value($gameVariables.value(3)) - 1);
    }
I've never tested any of this, but it makes sense and should technically work, maybe the few tips and errors I pointed out was the main problem, if not let me know and I'll actually give all of this a test for you later today..

If you opt out of the scripting part of it all, you can use the "Control Variables" event command. A did a quick screenshot of the general idea.

Screenshot_1.png
Screenshot_2.png
 

Dad3353

Praised Adventurer
@LTN Games ...

Excellent..! Many thanks for having taken the time and trouble to go over this in such detail. My (noobie...) errors have been corrected, and it now works..! The real problem was the capitalisation of 'value'; a real tiger trap which I fell into, despite my decades of experience in programming. D'oh..!
For the rest, your comments are entirely pertinent without having a complete vision of what's going on; as it happens they really only served to reinforce the message that one has to be extremely careful when coding (in any language..!). Useful stuff, in any case.
In case it could be of interest, I'll explain why I'm doing this...
I've a simple Farm plot, on which stuff grows. Each Plot Event uses 5 Variables and 5 Switches, and works to my satisfaction. The problems come when I wish to scale up the Farm production..! Every new Plot Event has to have its lot of Variables and Switches updated; this is already fastidious enough for 3 Plots; I'm not keen on doing the same for 20 or more. My notion was to have, for each Plot, a set of numbers corresponding to the Variable and Switch ID's. When duplicating my Plots, I'd only have to update those numbers to have the code in the Event adjust itself to the relevant Variables and Switches. A bit primitive, maybe, but easy enough; my problem was the syntax of getting the line of script to do a simple subtraction, which you clarified and solved.
There are certainly more elegant and efficient ways of doing what I want, and, in time, I'll doubtless get there. Meanwhile, I'm learning 'hands on' how to do stuff; the 'elegance', if it comes at all, will come later. It's not a Farm that I want (there are surely plug-ins for that...), it's the Knowledge of how to bend MV to my will that interests me. Today a Farm, tomorrow the World..! Mwahahaha..!

Thanks again.
 

LTN Games

Master Mind
Resource Team
Xy$
0.01
Glad I could help! This has also given me an idea for my JS tutorial blog series, it's really important to know these small details like naming conventions, hehe.
 
Top