Skip to main content

News

Topic: Returning Values (Read 3055 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
Returning Values
Trying to return the value of a global variable.
The variable is designated to count the number of steps total made in playthrough.
How do I return the value of that variable at a specific time, but without it steadily increasing as you walk?

In other words, the variable needs to return a static number (unchanging),
so I can compare it to itself after six steps have been taken when that value was returned.
For example:
Code: (javascript) [Select]
 if (Battle = true || NewStepCount is less than or equal to 6 steps than the OldStepCount) return true; 


This is what I have so far:
Code: (javascript) [Select]
 
function CheckAction(){

CheckActionButtons();
if (Battle = true || (Steps - OldStepCount) <= 6) {return true}
else {return false;}
}


function GetStepCount(){ return Steps; }
function CheckActionButtons(){

if (IsKeyPressed(KEY_SPACE)) {return true, OldStepCount = GetStepCount();}
if (IsKeyPressed(KEY_Z)) {return true, OldStepCount = GetStepCount();}
else {return false;}
}


The reason is I want to display my interface on when an action button is pressed, or if a battle is going on.
Which I've already place in:
Code: (javascript) [Select]
 if (CheckAction() == true) {Interface_Render();} 


Also, note: OldStepCount is a global variable, and so is Steps.
The weird thing though is OldStepCount is always at zero (because I haven't pressed any ActionButtons), but the interface is still displaying.
  • Last Edit: July 26, 2013, 05:59:33 pm by Vakinox

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Returning Values
Reply #1
Remember you use == to compare if something is the other or not. Using a single ='s will cause it to always equal the other.

On KeySpace and Key_Z you are returning true too early, the code beyond those statements is what's called 'dead code' and is neither compiled or interpreted since those constructs are shaken off of the abstract syntax tree. Perhaps you ,ewan to return true afterwords, or you can do a asm styled return like this:

Code: (javascript) [Select]

function DoSomething()
{
  var ret = false;

    if ( condition ) {
        ret = true;
    }
  // do_stuff

    return ret;
}
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Returning Values
Reply #2

Code: (javascript) [Select]

function DoSomething()
{
  var ret = false;

    if ( condition ) {
        ret = true;
    }
  // do_stuff

    return ret;
}

Code: (javascript) [Select]

function DoSomething()
{
  var ret = condition;
  // do_stuff

    return ret;
}

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Returning Values
Reply #3
Well the idea is to show that in the mist of much work, you can always come back to setting 'ret' etc. Yes in such a case it is possible to collapse many if's into that one condition, but you can end up with a really long if statement in the end on more complex conditions/statements. In terms of code complexity my answer is the best since there's only one return statement in the end, which is a common and good code practice.

He was doing:
Code: (javascript) [Select]

if ( condition1 ) { stuff() ; return something1; }
if ( condition2 ) { return something2; }
if ( condition3 ) {
     if ( sub_condition ) { return something3; }
}
return false;


When if you set a single item you can debug it easier / not follow so many returns. Otherwise in your 'simplification' you can just:
Code: (javascript) [Select]

function doSomething() {
    // do stuff
    return condition;
}


And skip 'ret' altogether. My original example must have been too simple.
  • Last Edit: July 26, 2013, 08:27:11 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here