Questions from the forum

Top  Previous  Next

Q: I want to add a 3D model to my level; it should load a random texture from an external file every time the level is run. Please help me with some code.

A: Here you go:

 

var random_number;

 

BMAP* skin1_pcx = "skin1.pcx";

BMAP* skin2_pcx = "skin2.pcx";

BMAP* skin3_pcx = "skin3.pcx";

 

function randomize_startup()

{

       // create a new set of random numbers each time the game is run

       random_seed(0);

}

 

action skin_changer()

{

       random_number = integer(random(3)) + 1;

       if (random_number == 1)

               ent_setskin(me, skin1_pcx, 1);        

       if (random_number == 2)

               ent_setskin(me, skin2_pcx, 1);        

       if (random_number == 3)

               ent_setskin(me, skin3_pcx, 1);        

}

 

 

Q: I need to open a web page from inside my game.

A: Here's a snippet that uses a "Buy Game" button which opens Conitec's website.

 

aum114_faq1

 

BMAP* pointer_tga = "pointer.tga";

 

BMAP* start1_png = "start1.png";

BMAP* start2_png = "start2.png";

BMAP* buy1_png = "buy1.png";

BMAP* buy2_png = "buy2.png";

BMAP* quit1_png = "quit1.png";

BMAP* quit2_png = "quit2.png";

 

function buy_game();

function quit_game();

 

PANEL* main_pan =

{

       layer = 10;

       bmap = "main.jpg";

       pos_x = 0;

       pos_y = 0;

       button (150, 100, start2_png, start1_png, start2_png, NULL, NULL, NULL);

       button (150, 160, buy2_png, buy1_png, buy2_png, buy_game, NULL, NULL);

       button (150, 220, quit2_png, quit1_png, quit2_png, quit_game, NULL, NULL);       

       flags = SHOW;

}

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

function buy_game()

{

       // open Conitec's website in the default browser

       exec("http://www.3dgamestudio.com", NULL);

}

 

function quit_game()

{

       sys_exit(NULL);

}

 

 

Q: Can someone give me an example how to make a panel rotate?

A: Here's an example:

 

var rotation_speed = 5; // set the desired rotation speed here

 

BMAP* quit_pcx = "quit.pcx";

 

PANEL* quit_pan =

{

       layer = 15;

       pos_x = 300;

       pos_y = 200;

       bmap = quit_pcx;

       flags = SHOW;        

}

 

function rotation_startup()

{

       // set the rotation center in the middle of the panel

       quit_pan.center_x = quit_pan.size_x * 0.5;

       quit_pan.center_y = quit_pan.size_y * 0.5;

       while (1)

       {

               quit_pan.angle += rotation_speed * time_step;

               // limit the panel rotation angle to 0...360 degres

               quit_pan.angle %= 360;

               wait (1);

       }

}

 

 

Q: Is there any way to use a true type font without installing it?

A: Sure thing! Here's an example:

 

#include <acknex.h>

#include <default.c>

#include <windows.h>

 

// define the new truetype font with a size of 48

// make sure that its true name (not necessarily the file name) is used between the quotation marks

FONT* always_font = "Always In My Heart#48";

 

TEXT* newfont_txt =       

{

       layer = 15;

       pos_x = 10;

       pos_y = 20;

       font = always_font;

       string ("This is an elegant handwritten font");

       flags = SHOW;

}

 

function main()

{

       // set the resolution to 800x600 pixels                

       video_mode = 7;

       // set a blue background

       vec_set(screen_color, vector(255, 0, 0));

       AddFontResource("always.ttf");

       wait (3);

       set(newfont_txt, VISIBLE);

}

 

aum114_faq2

 

 

Q: How can I display a string of text in the center of the screen? I know how to properly center a bmap panel on screen using screen_size.x, but I can't center a string of text. I am using true type system fonts, so the characters aren't equally spaced.

A: Here's an quick snippet that does the job:

 

FONT* arial_font = "Arial#20";

 

TEXT* centered_txt =

{

       font = arial_font;

       string("This string is always displayed in the middle of the screen!");

       // center the string around the pos_x and pos_y positions

       flags = SHOW | CENTER_X | CENTER_Y;

}

 

function center_startup()

{

       wait (3); // wait until the level is loaded

       while (1)

       {

               centered_txt.pos_x = screen_size.x / 2;

               centered_txt.pos_y = screen_size.y / 2;

               wait (1);

       }

}

 

function resolution_startup()

{

       while (1)

       {

               wait (-5);

               // switch between these two video resolutions

               video_switch(8, 0, 0);

               // every five seconds

               wait (-5);

               video_switch(6, 0, 0);

       }

}

 

 

Q: Does anybody know how can I change the sky cube after loading a new level?

A: There you go:

 

STRING* level2_wmb = "level2.wmb";

 

ENTITY* my_sky;

 

function load_new_level();

 

function skycube_startup()

{

       wait (3); // wait until the first level is loaded

       my_sky = ent_createlayer("skycube+6.tga", SKY | CUBE | SHOW, 1); // creat the sky cube that will be used for the first level

       on_l = load_new_level; // press the "L" key to load the new level

}

 

function load_new_level()

{

       level_load (level2_wmb);

       wait (3);

       ent_morph(my_sky, "secondsky+6.tga"); // morph the sky cube bitmap into a new one

}

 

 

Q: I am trying to find a function that will allow me to place a vertical scroll bar on the right of a selection of items, and if I click and drag the scroll bar up and down, it will allow me to see the list of items going up and down. Is there a function for this?

A: Here's an example that uses a "vslider" and a "window":

 

aum114_faq3

 

var scroll_pos;

 

BMAP* pointer_tga = "pointer.tga";

 

PANEL* scrolling_pan =

{

       bmap = "scroll.jpg";

       pos_x = 460;    

       pos_y = 0;   

       vslider (200, 10, 500, "slider.jpg", 0, 400, scroll_pos);

       window(5, 5, 200, 450, "myitems.jpg", 100, scroll_pos);        

       flags = SHOW;

}

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

 

Q: I want to implement a quick motion effect.

A: Here's an example with a guard that patrols a path and speeds up whenever it feels the need to do so (actually, it speeds up randomly). The code will affect all the entities in the level, of course.

 

var wait_time;

 

ENTITY* dummy1;

 

action path_patroller()

{

       var walk_percentage;

       var distance = 0;

       var previous_pan;

       var following_pan;

       var min_speed = 1;

       var max_speed = 3;

       var walking_speed;

       VECTOR last_pos[3];

       VECTOR direction[3];

       // create a dummy entity that will move on the path

       dummy1 = ent_create(NULL, nullvector, NULL);

       // make sure to name your path this way

       path_set(dummy1, "path_000");

       while(1)

       {

               previous_pan = my.pan;

               // place the dummy entity on the path

               path_spline(dummy1, my.x, distance);

               distance += walking_speed * time_step;

               // let the npc look ahead

               vec_diff(direction, my.x, last_pos);

               vec_to_angle(my.pan, direction);

               vec_set(last_pos, my.x);

               wait(1);

               following_pan = my.pan;        

               // don't allow the walking speed to exceed the specified min / max limits

               walking_speed = clamp(walking_speed, min_speed, max_speed);

               ent_animate(my, "walk", walk_percentage, ANM_CYCLE); // play the "walk" animation

               walk_percentage += 3 * time_step; // "3" controls the animation speed

       }

}

 

function quick_motion_startup()

{

       while (1)

       {

               // wait 5 to 15 seconds before changing the game speed

               wait_time = random(10) + 5;

               wait (-wait_time);

               // the game has been run at high speed? Then restore normal speed

               if (time_factor > 1)

                       time_factor = 1;

               // the game has been run at normal speed? Then make it run at a higher (300%) speed

               else

                       time_factor = 3;

       }

}

 

 

Q: Is there an easy way to dump the names of all the models in my game folder to a .txt file?

A: It doesn't get much easier than this:

 

var number_of_models, file_handle_write;

var i = 0;

 

TEXT* models_txt =

{

       pos_x = 20;

       pos_y = 25;

       strings = 100; // display / store up to 100 models, increase this value as needed

       flags = SHOW;

}

 

// gets the names of up to 100 models in the current folder

function models_startup()

{

       number_of_models = txt_for_dir(models_txt, "*.mdl"); // find all the files with the .mdl extension

       file_handle_write = file_open_write("models.txt"); // create the file if it doesn't exist

       while (number_of_models > 0) // this loop will run until the number of found models is reached

       {

               file_str_write(file_handle_write, (models_txt.pstring)[i]); // write each model file name to models.txt

            // move on to the following row in the models.txt file

               file_asc_write (file_handle_write, 13); // write the following strings on a new line

             file_asc_write (file_handle_write, 10); // using asc(13) = carriage return + asc(10) = line feed

            number_of_models -= 1; // a model name was written, so let's move on

             i += 1; // this value sets the index of the text-based array

       }

}

 

 

Q: My computer has 4 GB of RAM, which isn't that great, and WED seems to take up 1.8 GB of RAM with my current level. It's definitely caused by the skins of the models used for the level structure; each model has 4 x 1024 pixels textures. Any way to bring down the RAM usage without adjusting the skin size?

A: It looks like each and every one of your models uses about 12 MB of RAM. If you scale down the textures from 1024 to 512 pixels, the memory consumption will be brought down to only 3 MB per model, and WED shouldn't use more than 500-600 MB of RAM. Size down the textures while you are developing the game to free memory and speed up the process, and then replace them with the larger (1024 pixels) versions before hitting the "Publish" button.