Forum rules - please read before posting.

Calculating two AC vars in scripts?

edited August 2016 in Technical Q&A
hey there :)

Today i wanted to try to have two variables calculated and the result being set in another variable..

So i have the code for the two variables i want to calculate:









if
(DropStoneType.value == 15){

   AC.GlobalVariables.SetFloatValue (13, 285.71f, true);

and
if(DropStoneQuantity.value == 6){

   AC.GlobalVariables.SetIntegerValue (21, 7, true);
 



Now i want them to be multiplied with each other and send the result to the variable with ID 9. However, how would i do that calculation of the two vars in a Unity C# script?

Comments

  • you would probably do something like:

    float v1 = AC.GlobalVariables.GetFloatValue(13)

    float v2 = AC.GlobalVariables.GetFloatValue(21)

    float v2 = v1 + v2;
    AC.GlobalVariables.SetFloatValue(EnterYourVariableNumber, v3)

    something along those lines
  • Just like what klarax did above, but if you want to multiply:

    float v3 = v1 * v2;

    AC.GlobalVariables.SetFloatValue(EnterYourVariableID, v3);
  • You can also use the Variable: Set Action, which allows you to enter in a formula using variable tokens:

    [var:13] * [var:21]
  • klarax and ezsilman, thanks for your help!! <3
    Sadly i haven't gotten it to work with that yet. The v3 variable just stays empty.
    Does that code go into Start()?
    It should since i need to initiate those variables first, right?
  • edited August 2016
    use print(STUFF HERE) to see what is in the variables before you do the math.

    and after etc. try to see whats going on - the wonderful world of debug awaits XD

    p.s. you might have to do some casting from strings to floats and vice verser
  • edited August 2016
    the_dhel the function where some code needs to go varies depending on what you're after. Start() is only run once, when either unity starts play or when an object with a MonoBehaviour script is enabled for the first time in the hierarchy. So if your are running the code in Start() before the actual gVars have been populated, then the vars will be initialized as Null or 0. Something similar will happen if all the conditionals never meet the criteria for the same reasons: being calculated before the action has taken place. (By the way, if you want to know more about Unity's order of execution you can read it here and here just for Start() ).

    Seeing how you are using DropStone type and quantity in the naming of your variables I'd assume this is something that will happen after an action or a minigame(maybe?) has given you a score or stuff to get that score. in that case it would be better to put the code in a regular function(s) and call that function(s) with an object:Send message. that way the calculations will run exactly when you need them to. Or as Chris said, if you don't want to code, you can do the calculations for each var in an actionlist (Variable:Set accepts formulas) and then finish by using another Variable:Set with the code: [var:13] * [var:21].
  • The basic rundown of this minigame is that you have to determine how much an item costs. You can set the stone quality, quantitiy and a few more. These are then set in individual variables and on the bottom of the GUI is a field that shows you the final price/result of those calculations (Which is another Variable).

    I wanted to do this in a script because it is a LOT to write in the action list and it gets very confusing.. In a script it is, at least for me, much easier to see how things are done
  • Lol, sorry, the names of the variables were a little misleading. And you're right, there's some stuff that's just less cumbersome to pull off directly in code. Anyways, the long way to do it right would be to put the code in a method/function and call that method when the calculation needs to be performed. But that would mean attaching the script to an object and attaching that object to an actionlist with an Object:Send action every time you want to call the method... Or, you could make a short custom action. That way you can call the code anytime you want easily without any hassle and right from within any AC actionlist. If you are interested, I've put up an article in AC's wikia showing the basic creation of custom actions, which you can check here. Else you can try th other option (using an Object:Send action to call a script from an object.)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.