Questions from the forum

Top  Previous  Next

Q: I have a text file with strings like this:

 

string11,string22

string33,string44

 

I would need a snippet which reads the text file, switches the columns keeping the data intact, and then writes them to a new text file like this:

 

string22,string11

string44,string33

 

A: There you go.

 

var read_result = 1;

 

STRING* buffer1_str = "#200"; // the strings in the text file can have up to 200 characters each

STRING* buffer2_str = "#200"; // increase these values if you have longer strings

 

function process_data_startup()

{

       var file_handle_read, file_handle_write;

       file_handle_read = file_open_read("data.txt");

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

       while (1) // reads the data until the end of the file is reached

       {

               // read the first string

               read_result = file_str_read(file_handle_read, buffer1_str);

               // read the second string

               read_result = file_str_read(file_handle_read, buffer2_str);

               // both strings were read here, so let's write them to the new file

               // didn't read an empty string (the end of the file wasn't reached yet)? Then write the strings to output.txt!                

               if (read_result != -1)

               {

                       file_str_write(file_handle_write, buffer2_str); // write the first string (was the second in its row)

                       file_str_write(file_handle_write, ","); // separate the consecutive strings with a comma                

                       file_str_write(file_handle_write, buffer1_str); // write the second string (was the first in its row)

                       // move on to the following row in the output.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

               }

               else // reached the end of the file?

               {

                       break; // then let's get out of this loop!

               }                        

               wait (1);

       }

       // the data reading and writing has finished here, so close the input and ouput files

       file_close(file_handle_read); // close data.txt

       file_close(file_handle_write); // close output.txt        

}

 

 

Q: Can someone give some help with the VIEW commands since the manual is quite lacking here.

A: Here's a fully functional example.

 

VIEW* top_view =

{                

       // sets the camera layer value, allowing it to be placed on top of other views that have a lower layer value

       layer = 10;

       // sets the position of the view on the x axis of the screen

       pos_x = 10;

       // sets the position of the view on the y axis of the screen        

       pos_y = 10;

       // sets the size of the view on the x axis of the screen        

       size_x = 128;

       // sets the size of the view on the y axis of the screen                

       size_y = 256;

       // sets the camera.arc value (zoom factor) of the view

       arc = 45;

       // set the ambient value of the view (bigger values will make the view brighter than the default camera view)

       ambient = 100;

       // displays the view

       flags = SHOW;

}

 

function attached_startup() // attaches the view to the player, placing it 200 quants above it

{

       // your player code must include this line of code: "player = my;" (without using the quotes)

       while (!player) {wait (1);} // wait until the player model is loaded in the level

       top_view.tilt = -90; // make the new view look downwards

       while (1)

       {

               vec_set(top_view.x, player.x);

               top_view.z += 200; // place the view 200 quants above the player

               top_view.pan = player.pan; // rotate the view together with the player

               wait (1);

       }

}

 

 

Q: Does anyone have a lite-C version of the Serious Grass code from Aum58?

A: The converted version is included in this month's resources.

 

 

Q: I want to have an in-game menu that is toggled on / off by pressing the "T" key. Can anyone help?

A: Here's a simple example that does what you want.

 

var toggled = 0;

 

PANEL* main_pan =

{

bmap = "main.tga";

pos_x = 10;    

pos_y = 20;   

}

 

function toggle_menu_startup()

{

       while (1)

       {

               if (key_t)

               {

                       while (key_t) {wait (1);}

                       toggled += 1;

                       if (toggled % 2) // time to show the menu?

                       {

                               set (main_pan, SHOW); // then do it!

                       }

                       else // got to hide the menu?

                       {

                               reset (main_pan, SHOW); // then do it!                        

                       }

               }

               wait (1);

       }

}

 

 

Q: I would like to use up to 8 gamepads in a single application. Is this possible without using a dll?

A: Gamestudio supports 2 joysticks / gamepads with up to 12 buttons each; you will need to use a dll if you want to have access to 8 gamepads.

 

 

Q: I am not sure what I did, but my player model floats up off the map; I can still move it as if it were on the map, though. Anyone has any idea what might have caused this?

A: The first possible cause is the origin of your model; check it in Med and adjust it properly, with the origin on the z axis being around the waist of the actual model. If this doesn't solve the problem for good, check out the section of your code that takes care of player's gravity; it's usually something like this:

 

.........................

temp.z -= 10000;

temp.z = 0;

temp.z = -c_trace (my.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | USE_BOX) - 12; // look for a c_trace instruction that changes the z component of a vector

.........................

 

In this case, we are subtracting an additional 12 quants from player's vertical component; play with the numerical value (positive values might do their job as well) until player's feet are placed on the ground.

 

 

Q: I have a tent model has a lamp next to it (part of the same entity).  I'm using lightrange and ambient, but I would like the light to come from the lamp, rather than the origin of the model (the tent).

A: Create an invisible entity and attach it the light source, like this.

 

function dynamic_light()

{

       my.ambient = 100;

       my.lightrange = 250;

       while (1)

       {

               my.red = 200 + random(40); // use a slight red color variation

               my.green = 80;

               my.blue = 30;

               wait (1);

       }        

}

 

action my_tent()

{

       VECTOR lamp_pos;

       while (!player) {wait (1);} // wait until the player model is loaded

       // get the coordinates of the 123rd tent vertex and store them inside the lamp_pos vector

       vec_for_vertex(lamp_pos, my, 123);

       ent_create(NULL, lamp_pos, dynamic_light);

}

 

 

Q: Is it possible to export from Gamestudio to Excel? I would like to write my inventory program in lite-C.

A: You can use lite-C to export to csv, a file format that is imported by Excel without any problem - here's a simple inventory snippet that exports to csv.

 

var file_handle;

 

BMAP* pointer_tga = "pointer.tga";

 

STRING* product_str = "Input the product name";

STRING* quantity_str = "Input the quantity";

 

TEXT* product_txt =

{

       pos_x = 30;

       pos_y = 50;

       layer = 20;

       string(product_str);

}

 

TEXT* quantity_txt =

{

       pos_x = 500;

       pos_y = 50;

       layer = 20;

       string(quantity_str);

}

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

function quit_input() // press the esc key to stop the program and save the data

{

       file_close(file_handle); // close inventory.csv

       sys_exit(NULL); // and then shut down the engine

}

 

function input_startup()

{

       on_esc = quit_input;

       wait (-5); // wait for 5 seconds

       file_handle = file_open_write("inventory.csv"); // create the file if it doesn't exist

       file_str_write(file_handle, "Products");

       file_str_write(file_handle, ","); // separate the consecutive strings with a comma

       file_str_write(file_handle, "Quantities");

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

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

       // run this loop until the player types "quit" for the product name

       while (1)

       {

               set(product_txt, SHOW);

               wait(-3);

               str_cpy(product_str, "#20"); // reset the string, allow up to 20 characters to be input for the product name

               inkey(product_str);

               file_str_write(file_handle, product_str); // write the product name to the file

               file_str_write(file_handle, ","); // separate the consecutive strings with a comma                

               reset(product_txt, SHOW);                

               set(quantity_txt, SHOW);

               wait(-3);

               str_cpy(quantity_str, "#4"); // reset the string, allow up to 9,999 units (4 digits) to be input for the quantity

               inkey(quantity_str);

               file_str_write(file_handle, quantity_str); // write the product name to the file

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

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

               reset(quantity_txt, SHOW);

       }

}

 

aum101_faq2

 

 

Q: I have a variable var money = 1234567; I need to show this number on a panel like this: 1,234,567. Is it possible to show the number with its digits separated by commas?

A: Sure.

 

STRING* money_str = "#10";

STRING* temp_str = "#10";

STRING* copy_str = "#10";

 

TEXT* money_txt =

{

       pos_x = 20;

       pos_y = 20;

       string(money_str); // displays the properly formatted string

       flags = SHOW;

}

 

function money_startup()

{

       var money = 1234567;

       var length;

       str_for_num(temp_str, money); // convert the number to a string

       str_cpy(copy_str, temp_str);

       length = str_len(temp_str); // count the number of digits

       if (length <= 3) // no need to add commas here?

               str_cpy(money_str, temp_str); // then copy the converted number to money_str directly!

       if (length == 4) // got a 4 digit number here?

       {

               str_trunc(temp_str, 3); // remove the last 3 digits

               str_cpy(money_str, temp_str); // copy the first digit to money_str;

               str_cat(money_str, ","); // add a comma

               str_cpy(temp_str, copy_str); // restore temp_str

               str_clip(temp_str, 1); // remove the first digit

               str_cat(money_str, temp_str); // add the last 3 digits

       }

       if (length == 5) // got a 5 digit number here?

       {

               str_trunc(temp_str, 3); // remove the last 3 digits

               str_cpy(money_str, temp_str); // copy the first digit to money_str;

               str_cat(money_str, ","); // add a comma

               str_cpy(temp_str, copy_str); // restore temp_str

               str_clip(temp_str, 2); // remove the first 2 digits

               str_cat(money_str, temp_str); // add the last 3 digits

       }

       if (length == 6) // got a 6 digit number here?

       {

               str_trunc(temp_str, 3); // remove the last 3 digits

               str_cpy(money_str, temp_str); // copy the first digit to money_str;

               str_cat(money_str, ","); // add a comma

               str_cpy(temp_str, copy_str); // restore temp_str

               str_clip(temp_str, 3); // remove the first 3 digits

               str_cat(money_str, temp_str); // add the last 3 digits

       }

       if (length == 7) // got a 7 digit number here?

       {

               str_trunc(temp_str, 6); // remove the last 6 digits

               str_cpy(money_str, temp_str); // copy the first digit to money_str;

               str_cat(money_str, ","); // add a comma

               str_cpy(temp_str, copy_str); // restore temp_str

               str_trunc(temp_str, 3); // remove the last 3 digits                

               str_clip(temp_str, 1); // remove the first digit

               str_cat(money_str, temp_str); // add the following 3 digits

               str_cat(money_str, ","); // add a comma                

               str_cpy(temp_str, copy_str); // restore temp_str                

               str_clip(temp_str, 4); // remove the first 4 digits

               str_cat(money_str, temp_str); // add the last 3 digits                

       }

}

 

 

Q: Can anyone show me a simple algorithm for camera shaking?

A: Here's a simple, and yet effective method that changes camera's roll angle for a few seconds.

 

SOUND* explosion_wav = "explosion.wav";

 

function roll_startup()

{

       var shaking_time = 3; // shake the camera for 3 seconds

       while (!key_e) {wait (1);} // press the "E" key to start the explosion (camera shaking)

       while (key_e) {wait (1);} // waut until the player releases the "E" key

       snd_play (explosion_wav, 100, 0);

         while (shaking_time > 0)

       {

               shaking_time -= time_step / 16; // subtract a value of 1 from shaking_time each second

            camera.roll = 3 - random(6); // shake the camera by changing its roll angle, play with the numerical values

            wait (1);

       }

         camera.roll = 0; // the explosion has ended here, so let's reset camera's roll angle

}