Page 1 of 1

Having problems with Adding Lives

PostPosted: Tue Feb 21, 2012 6:43 pm
by RippeR7420
So this is what i'm doing...

Score->DrawActor->Script->
}
Code: Select all
if(Score.textNumber>=1000)
{
Lives.textNumber=Lives.textNumber+1;


I've tried putting this code in the Lives Actor under Draw Actor, but It still doesn't work..
The number just continuously rises.. on the other hand, when I put this code in the Create
Actor event, nothing happens at all.

I have a Variable for both, My Lives, and Score.

Anybody know what I'm doing wrong?

Re: Having problems with Adding Lives

PostPosted: Tue Feb 21, 2012 8:12 pm
by skydereign
Ok, you need to come to learn when events trigger. A lot of using gE is knowing when certain events will trigger and which ones of those are appropriate for the tasks you want. In this case create actor only happens once, so would putting it there make sense? That bit of code only gets looked at once when the actor is created, so that means score has to already be 1000 when the actor is created.

Now draw actor happens every frame. Since you are checking for when score is greater than 1000, that could happen at any time, and therefore you would have to check every frame. But since draw events execute every frame this poses the following problem. If score is greater than 1000 then Lives will be increased every frame. That currently is what you are having problems with. There are a couple of ways of fixing this. If score is allowed to be reset (as in lowering it by 1000 is okay) then doing that will work. But since you are using a text actor to display it probably that isn't the case. You could do something like this though (create a variable called next_life).
Code: Select all
if(score>next_life)
{
    next_life+=1000;
    lives++;
}

Two things about that, next_life will need to be set to 1000 at the start of your game, the other thing is I recommend you stay away from using textNumbers like that. They aren't a good replacement for real variables, as there have been many reports of textNumbers causing unpredictable behavior. So, instead you should only set textNumber to something, instead of using it in comparisons. You say you have said variables so use them. Usually people just add a draw event to their text actors that displays their variable.
life_display -> Draw Actor -> Script Editor
Code: Select all
textNumber=lives;

Re: Having problems with Adding Lives

PostPosted: Tue Feb 21, 2012 11:37 pm
by RippeR7420
ahh, okay... How would you recommend doing a "Lives" system?

Re: Having problems with Adding Lives

PostPosted: Tue Feb 21, 2012 11:42 pm
by skydereign
What do you mean? Just have a variable that holds the number of lives and use it accordingly. If you need to increase or decrease lives than you can just change the variable. If you need to check if lives is greater than zero (the player has another life) just use an if statement checking the variable. Lastly if you want to display it you can set a text actor's textNumber variable equal to it. You handle it like any other variable you deal with, except since this variable is one you want to be viewable by the player you would use textNumber to display it.

Re: Having problems with Adding Lives

PostPosted: Tue Feb 21, 2012 11:46 pm
by RippeR7420
I guess I'm just not understanding how to add +1 to the variable...

Re: Having problems with Adding Lives

PostPosted: Tue Feb 21, 2012 11:56 pm
by skydereign
How have you been doing movement all this time? I feel like you're probably over thinking this.
Code: Select all
x+=1; // this increases x by 1
x = x + 1; // so does this
x++; // and so does this

That's how you increase the value stored in variables (decreasing would just be using - instead of +).

Re: Having problems with Adding Lives

PostPosted: Wed Feb 22, 2012 12:32 am
by RippeR7420
haha maybe. What I am doing is this;

Lives->Draw Actor-> Script->

Code: Select all
if(Score.textNumber>=1000)
{
Lives.textNumber+=1;
}


So, I have a variable set up for my lives called "PlayerLives". I use it for SaveVars and LoadVars only... How do I incorporate into this draw actor?..

Re: Having problems with Adding Lives

PostPosted: Wed Feb 22, 2012 12:34 am
by RippeR7420
How do I set a Variable to hold the number of Lives?

Re: Having problems with Adding Lives

PostPosted: Wed Feb 22, 2012 12:45 am
by skydereign
You know how to do it. You've been doing it for your textNumber variables as well as your variables you have for saveVars and loadVars.
Code: Select all
Lives = 3; // sets Lives to 3
Lives++; // increases Lives by 1
Lives+=2; // increases Lives by 2
Lives--; // decreases Lives by 1

if(Lives>0)
{
    // checks if the player still has some Lives left
}

Re: Having problems with Adding Lives

PostPosted: Wed Feb 22, 2012 1:19 am
by RippeR7420
wow I'm feeling really stupid because I can't figure this out. :oops:
So I do something like this?;

Lives->Create->Script->
Code: Select all
PlayerLives=Lives.textNumber;
PlayerLives = 3;


and then this;

Lives->Draw->Script->
Code: Select all
if(Score.textNumber>=1000)
{
PlayerLIves++;
}


something like this?...

Re: Having problems with Adding Lives

PostPosted: Wed Feb 22, 2012 7:13 am
by skydereign
In all honesty you need to relearn variables. You are trying to understand something but only grasping a very small portion of it. This probably comes from copying bits of code and not truly understanding how it works. So I ask, do you know what a variable is? If you want we can continue this in pm and I can walk you through the basics.