Questions from the forum

Top  Previous  Next

Q: I have followed the 6 first workshops of the GameStudio Online Tutorial. Now I am trying to make my own button with 3 different images of the same size, but it doesn't work.

A: Here's a simple, fully functional example:

 

// main panel bitmap

BMAP* mypanel_pcx = "mypanel.pcx";

 

// bitmaps used for the buttons

BMAP* pictureon_pcx = "pictureon.pcx";

BMAP* pictureoff_pcx = "pictureoff.pcx";

BMAP* pictureover_pcx = "pictureover.pcx";

 

function my_function(); // function prototype

 

PANEL* my_pan =

{

       layer = 15;

       button(40, 10, pictureon_pcx, pictureoff_pcx, pictureover_pcx, my_function, NULL, NULL);

       flags = SHOW;

}

 

// just a simple function that runs when the player clicks the button

function my_function()

{

       beep();        

}

 

 

Q: How can I get the coordinates of the model which was hit by a c_trace ray?

A: Use the snippet below as a base for your code.

 

VECTOR trace_start, trace_end, hit_coords;

 

function fire_weapon()

{

       while (mouse_left)

       {

               vec_set (trace_end.x, vector(10000, 0, 0)); // the c_trace ray has a lenght of 10,000 quants

               vec_rotate (trace_end.x, camera.pan);

               vec_add (trace_end.x, camera.x);        

               vec_set (trace_start.x, vector(0, 6, 0)); // the c_trace ray starts a bit offset from the center of the camera - this makes the red ray visible for the player

               vec_rotate (trace_start.x, camera.pan);

               vec_add (trace_start.x, camera.x);        

               // trace from the center of the camera 10,000 quants in front of it                

               c_trace (trace_start.x, trace_end.x, IGNORE_ME);

               draw_line3d(trace_start.x, NULL, 10000);

               draw_line3d(trace_end.x, vector(0, 0, 255), 100);                               

               if(you) // got an entity?

               {

                       vec_set(hit_coords, target.x); // then get the coordinates of the hit point!

               }

               else

               {

                       vec_set(hit_coords, nullvector); // display zero if the ray didn't hit an entity        

               }

               wait (1);

       }

}

 

function init_startup()

{

       on_mouse_left = fire_weapon;        

}

 

PANEL* hit_pan = // displays the hit point coordinates

{

       layer = 15;

       digits(10, 30, "Hit coordinate x: %.f", *, 1, hit_coords.x);

       digits(10, 50, "Hit coordinate y: %.f", *, 1, hit_coords.y);

       flags = SHOW;

}

 

 

Q: How do I manage to have collision using only 2D bmaps? My collision script is so buggy...

A: Check Aum 81...85; you will discover a full workshop series that discusses 2D games, including advanced 2D collision mechanisms.

 

 

Q: I have a panel that pops up with a button on it. The button closes the panel correctly, but it doesn't disappear together with the panel. Can anyone help?

A: The button must be an element of the panel that is supposed to disappear, and not a distinct panel itself - here's a simple example.

 

BMAP* mypanel_pcx = "mypanel.pcx";

 

BMAP* pictureon_pcx = "pictureon.pcx";

BMAP* pictureoff_pcx = "pictureoff.pcx";

 

BMAP* pointer_tga = "pointer.tga";

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

function close_panel(); // function prototype

 

PANEL* my_pan =

{

       bmap = mypanel_pcx;

       layer = 15;

       button(40, 10, pictureon_pcx, pictureoff_pcx, pictureon_pcx, my_function, NULL, NULL);

       flags = SHOW;

}

 

// hides the panel as soon as the player click the button

function my_function()

{

       reset(my_pan, SHOW);

}

 

 

Q: How can I build a circular mask using two different panels? The first panel would be the one of the front and the other one would be the background.

A: Use panels that have different layer values; the one with the bigger layer value will be placed closer to the top - here's a quick example.

 

BMAP* front_pcx = "front.pcx";

BMAP* background_pcx = "background.pcx";

 

BMAP* pointer_tga = "pointer.tga";

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

function front_clicked(); // function prototype

 

PANEL* front_pan =

{

       bmap = front_pcx;

       layer = 20; // appears over background_pcx;

       on_click = front_clicked; // runs this function when it is clicked

       flags = SHOW;

}

 

PANEL* background_pan =

{

       bmap = background_pcx;

       layer = 10; // appears below front_pcx;

       flags = SHOW;

}

 

// a simple function that runs when the front panel is clicked

function front_clicked()

{

       beep();

}

 

 

Q: How can I restart a level and all of my codes? As an example, I remove an entity using ent_remove during the game and I'd like it to be created again when the level is restarted.

A: Each time a level is being loaded / restarted through level_load, all the actions and functions that are attached to entities will stop. This means that your ent_removed entities will be loaded again automatically - you don't have to do anything in order to make that happen. The functions that aren't used by entities will keep running, so if (let's say) you've got a function that keeps the game score, it will continue to run fine even after loading a new level.

 

 

Q: I want to build an object that rotates. On this object another object should be placed, acting as a flickering light.

A: There you go.

 

VECTOR light_pos;

 

function blinking_lights()

{

       while (1)

       {

               // get the coordinates of the 10th vertex on the sphere model and keep the light at that position

               vec_for_vertex(my.x, you, 10);

               vec_set(my.blue, vector(0, 0, 255)); // our light will have a blue color

               my.lightrange = 30; // and a range of 30 quants

               wait (-0.1); // keep the light on for 0.1 seconds

               my.lightrange = 0;

               wait (-0.1); // then turn it off for 0.1 seconds

       }

}

 

action my_rotator()

{

       // create a NULL entity - you can also use something like "light.mdl" if you need visible light generating models

       ent_create(NULL, nullvector, blinking_lights);

       while (1)

       {

               my.pan += 3 * time_step; // rotate the sphere around its pan, tilt, roll angles

               my.tilt += 2 * time_step;

               my.roll += 1 * time_step;

               wait (1);

       }        

}

 

 

Q: I have a 3d model and want it coming out of the ground little by little, like a line of grass growing up on the ground.

A: Place the grass model in Wed in its fully grown state, and then attach it the action below.

 

action growing_grass()

{

       var grass_height; // stores the vertical size of the fully grown grass model

       var temp_z; // stores the initial grass height value

       set(my, INVISIBLE);

       c_setminmax(me); // set my bounding box of the grass model to its real size

       grass_height = my.max_z - my.min_z; // now compute the real height of the grass model

       temp_z = my.z;

       my.z -= grass_height; // move the grass downwards until it's prepared to grow (provided that it was placed on the ground properly, of course)

       wait (-5); // wait for 5 seconds before having the grass grow

       reset(my, INVISIBLE);

       while (my.z < temp_z) // move the grass model upwards until it returns to its initial height

       {

               my.z += 0.5 * time_step; // 0.5 sets the growing speed

               wait (1);        

       }

}

 

 

Q: How do I make my models have shadows on terrain entities? I've tried everything...

A: First of all, place this line of code inside function main, before the line that loads the level - it's very important to do that!

 

shadow_stencil = 2; // choose different shadow_stencil values here, depending on your needs - check the manual for more info

 

Then, set the "shadow" flag for the entities that are supposed to produce shadows from within Wed. Check the "cast" flag as well if you'd like the entities to also project shadows on themselves.

 

aum106_faq1

 

 

Q: I'm struggling with a piece of code that lets an entity turn and run towards the player. Any ideas how to achieve that with a smooth movement so that the entity won't turn first, and then start running towards the player?

A: There you go.

 

VECTOR chaser_dest;

ANGLE chaser_angle;

 

action player_chaser()

{

       var stand_percentage, run_percentage;

       // wait until the player model is loaded

       while (!player) {wait (1);}

       while (1)

       {

               if (vec_dist(player.x, my.x) > 500) // the player is away from the chaser?

               {

                       ent_animate(my, "stand", stand_percentage, ANM_CYCLE); // then play the "stand" animation

                       stand_percentage += 2 * time_step; // 2 = "stand" animation speed

               }

               else // the player has come closer than 500 quants to the chaser here

               {

                       if (vec_dist(player.x, my.x) > 150) // the chaser didn't approach the player close enough yet?

                       {

                               vec_set(chaser_dest, player.x);

                               vec_sub(chaser_dest, my.x);

                               vec_to_angle(chaser_angle, chaser_dest);

                               my.pan += ang(chaser_angle.pan - my.pan) * 0.1 * time_step;

                               ent_animate(my, "run", run_percentage, ANM_CYCLE); // then play the "run" animation

                               c_move (my, vector(10 * time_step, 0, 0), nullvector, IGNORE_PASSABLE | GLIDE);                                

                               run_percentage += 6 * time_step; // 6 = "run" animation speed                                

                       }

                       else

                       {

                               // the chaser is close enough to the player here, so it stops for now

                               // I chose to play the same stand animation, but feel free to do your own thing

                               ent_animate(my, "stand", stand_percentage, ANM_CYCLE); // then play the "stand" animation

                               stand_percentage += 2 * time_step; // 2 = "stand" animation speed

                       }

               }

               wait (1);

       }

}