I wish my hours were random.

Talk about making games.

I wish my hours were random.

Postby linkshards » Sun May 30, 2010 3:07 am

Seriously. I'm getting a little frustrated with what I'm trying to do here. I have spent hours of code dissection and a bit of C research to get my code to work and I still cannot get it to work.

So here's what I have.
I am trying to get my game to randomly pick a x and y coordinate on the screen displayed (which it does)
Then randomly select 1 of 3 monsters I have created and spawn it.

All of my monsters are off screen due north and have a lock code on them that prevents them from MoveTo(ing) the main player. Until they are spawned then that's when they MoveTo.

Code: Select all
montemp = 0; //creating my switch
     if(montemp = 0){
            montemp = rand(3); //switch started
 
            timer1++;

    if(montemp == 1){ //spawn monster1 if montemp is equal to 1
        if (timer1 == 40)
        {
    CreateActor("monster1", "mon1walkfront1_seq", "(none)", "(none)", rand(320), rand(240), false);


    CreateActor("monster1", "mon1walkfront1_seq", "(none)", "(none)", rand(-320), rand(-240), false);
        timer1 = 0;
        }
 montemp = 0;

                    }
 
 
 

}

  if(montemp == 2){
        if (timer1 == 100){
       

    CreateActor("monster2", "mon2walkfront1", "(none)", "(none)", rand(320), rand(240), false);
 

    CreateActor("monster2", "mon2walkfront1", "(none)", "(none)", rand(-320), rand(-240), false);
        timer1 = 0;
        }
montemp = 0;
                    }





 if(montemp == 3){



        if (timer1 == 200){
 
CreateActor("monster3", "mon3walkfront1_seq", "(none)", "(none)", rand(320), rand(240), false);
CreateActor("monster3", "mon3walkfront1_seq", "(none)", "(none)", rand(-320), rand(-240), false);
        timer1 = 0;
        }
montemp = 0;


                    }
             }
 


It's quite a lot. The code works when I specifically set the montemp to 1 (and it spawns monster1) and erase all the other junk, but I cannot get it to work with everything else.

Help anyone? :( I've almost lost my motivation to keep working with this.
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score

Re: I wish my hours were random.

Postby DST » Sun May 30, 2010 3:40 am

First off, i have to say that learning to program takes time. It's something that you do because you enjoy it, and so it keeps you motivated. But it takes time. C is a language, with its own words and sentence structure. Would you expect to learn French in only a few days?

Let's look at what is happening in your code:

if (montemp==0){ //you had (typo) = there
montemp=rand(3); //montemp could still == 0
timer1++; //count up timer
if(timer==40 && montemp==1){ //this is standard for combining two queries.

now you've changed montemp, and on the next frame, montemp may/may not == 0. You then set montemp to 0 again, making the previous action of montemp=rand(3) redundant. You do this until timer==40.
That's 39 times you set montemp to rand(3) that had no effect on anything.

To create a random monster spawn, the timer should come first.

Code: Select all
timer1++;
if(timer==40){
montemp=rand(3);
switch(montemp){
case 1:
//make monster
break;
}
}


Then, when you write the (timer==200) case, then you'd reset timer1 to 0.


In your example, what happens if timer1==40 but montemp ==2? Your chances of montemp hitting 1 and timer hitting 40 at the same time are 1 in 3.
What happens if montemp=rand(3) produces a montemp of 0? Then none of the following actions will happen. And timer1 will never get reset to 0, it will just keep counting up forever, because it required both variables to be true.

So the effect you want is that montemp is secondary to the timer1. timer1 decides when something will happen, and montemp decides what happens AFTER timer1 gives the ok.

You just mixed two questions here, and they can result in what you want never happening.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: I wish my hours were random.

Postby linkshards » Mon May 31, 2010 6:31 am

It worked! :D Oh my gosh! Thank you so much!
You're right I should've been a more patient. I know quite a bit about programming (Java actually, and C is very similar to it.)
You recommended the switch statement in this program...
I've had help from HBlade when he told me about creating a switch.

I guess I got a little confused. When I learned Java in school, I learned that a switch was referred to a temporary variable (such as "temp") being used to control conditions.
So... I guess in terms of programming when someone refers to a switch, are they referring to the switch statement or a temp switch?
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score

Re: I wish my hours were random.

Postby DST » Mon May 31, 2010 7:15 am

Often the 'school' methods include things that aren't always relevant (like endless booleans) to our game making. I'm not saying that they schools are wrong, but they teach a different type of programming, the kind where you work for some giant corporation, on one tiny slice of the program.

In rogue game making, it's much different, as you are usually working on the entire program yourself, and it's also a much smaller program.

In games like these, usually we are switching something from a constant variable. The variable changes depending on what the game is doing, but a temporary variable is usually only used to create randomness.

You might use a temp if you wanted to randomly spawn one of three monster types.

You would use a constant variable if you were spawning an enemy based on the level the player is on, where the levels have different enemy types.

Glad to hear it worked for you!
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: I wish my hours were random.

Postby linkshards » Mon May 31, 2010 7:39 am

True, I guess it's different where I go in terms of programming.

Also, I'm having a bit of trouble with mouse detection and collision detection.
I can figure out the mouse detection stuff eventually, but the collision stuff is killing me.

Here's a video of what I have in mind:
The video's long, but don't watch the entire thing, what I'm looking at here is the collision when the enemy or player gets hurt.


The character/enemy is knocked back a bit, plays an animation, then returns to normal.
When I try to program the collision one of the actors gets pushed endlessly to one side of the window and leaves the screen, totally ignoring the "walls" of the window.
OR
The character is just 'nudged' or moved instead of actually pushed and then coming to a stop.
User avatar
linkshards
 
Posts: 45
Joined: Tue Apr 27, 2010 3:40 am
Location: Cibolo, Texas
Score: 1 Give a positive score

Re: I wish my hours were random.

Postby DST » Mon May 31, 2010 9:06 am

The simplest way to make a controlled collision movement is using the moveto() function, like this:


Code: Select all
int i=x-collide.x;    //store the offset of this actor vs. collide actor position
int j=y-collide.y;    //using x-collide.x gives us negative offset (collide.x-x would create positive offset)
MoveTo("Event Actor", x+i, y+j, 10.000000, "Game Center", "");  //move to that negative offset


If he is to move farther away than that, just multiply the i/j values by whatever you wish before adding them to the moveto();

As for the animation and invincibility, you can 'changeanimation' in the collision script, and then add an 'animationfinish>hurt animation>script editor' where you restore to normal animation.

If he is to be invincible during the duration of the 'hurt', simply wrap this script in a variable check, like
if(cancollide==1){
//movetoscript here
cancollide=0;
}

Then return it to 1 on the animation finish. You can use a timer, or a 'state' switch in draw actor to more accurately set the length of time he is hurt, if you prefer, instead of using animationfinish.

The cancollide disables collisions while he is in hurt mode, and by NOT using that same 'if' in wall collisions, he'll always collide with walls no matter what.

I often like to use a state switch for actors, like this:

Actor>draw actor>

switch(state){
Code: Select all
case 0: //normal operation
break;
case 1://hurt mode
variable++;
if(variable==10){   //stay hurt for 10 frames
variable=0;
state=0;               //then return to normal mode
ChangeAnimation(etc etc);
}
break;
}


Then you can simply switch the state in the collision, and also use that same state instead of cancollide, by only allowing the collisions script to happen if the state!=1. You can also disable other events in the state, so you won't have to do it in the collision. Keep in mind that it's a good idea to set that 'variable' used as a timer to 0 upon collision, to prevent it being a wrong value if something else happened to interrupt it.


Most important part of all: If you have multiple enemies that are not clones, you would need this collision script for collision with each one. so make it a function!

Go to global script, and wrap it in a function name:
Code: Select all
void getHurt(int cx, int cy){
//collision script here, using cx instead of collide.x, and cy instead of collide.y
}


Then just call the getHurt(collide.x, collide.y); in the collisions with each type of enemy, instead of typing it out each time. Making changes to the script will be much easier that way, as everything you do to the collide script will automatically happen to all the collisions.

Note that you have to pass the collide.x and y variables to the script but not the event actor's x and y, because a script automatically becomes part of the actor it's called in. So you'd also want to add the collide.damage, as another int in the script, and any other variable that the enemy passes on to the player.

Using these things as functions makes a game 100x easier to create.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest