Converting between old and new string formats (SOLVED)

Started by mkennedy, Sun 15/04/2007 06:42:54

Previous topic - Next topic

mkennedy

Is there anyway to convert bewteen the old and new style string formats?
I'm trying to write a program that ask the player for a number and then performs calculations based apon those numbers. Many of the new functions only work with the new Strings (uppercase) and won't accept the old style strings (lowercase) , but when I try to use the older functions with old style strings I get an "...is not a public member..." error.

EDIT: Thanks for the info, the promlem is now solved.

strazer

#1
Please post the relevant parts of the code that give you trouble. The error message you get doesn't look like a string conversion problem.

Have you tried something like this:

Code: ags

  String strNumber1;
  String strNumber2:
  while ((strNumber1 != "0") && (strNumber1.AsInt == 0))
    strNumber1 = Game.InputBox("First number:"); // repeat until a number entered
  while ((strNumber2 != "0") && (strNumber2.AsInt == 0))
    strNumber2 = Game.InputBox("Second number:"); // repeat until a number entered

  int intNumber1 = strNumber1.AsInt;
  int intNumber2 = strNumber2.AsInt;

  Display("%d plus %d is %d", intNumber1, intNumber2, intNumber1 + intNumber2);
  Display("%d times %d is %d", intNumber1, intNumber2, intNumber1 * intNumber2);
  //...


Edit:

Btw, you can convert old-style strings to new-style Strings like this:
  String newstring = String.Format("%s", oldstring);

mkennedy

Ok, Thanks. The reason I asked was I'm trying to convert my text parser template into a module and I need to convert the old style strings to the new type.  so far I've imported a GUI named PARS that has a textbox named wordbox and my code for the module is as follows:

// Main script for module 'prsmod'
int random, inv[2];
String invent,idont,input,nchar,nhot,nobj;

#sectionstart on_key_press  // DO NOT EDIT OR REMOVE THIS LINE
function on_key_press(int keycode) {
  // called when a key is pressed. keycode holds the key's ASCII code
  if (IsGamePaused() == 1) keycode=0;  // game paused, so don't react to keypresses
  if (keycode==17)  QuitGame(1);   // Ctrl-Q
  if (keycode==363) SaveGameDialog();   // F5
  if (keycode==365) RestoreGameDialog();  // F7
  if (keycode==367) RestartGame();  // F9
  if (keycode==434) SaveScreenShot("scrnshot.bmp");  // F12
  if (keycode==9)   InventoryScreen();  // Tab, show inventory

  if (keycode==19)  Debug(0,0);  // Ctrl-S, give all inventory
  if (keycode==22)  Debug(1,0);  // Ctrl-V, version
  if (keycode==1)   Debug(2,0);  // Ctrl-A, show walkable areas
  if (keycode==24)  Debug(3,0);  // Ctrl-X, teleport to room
     if ((keycode >= 'A') && (keycode <= 'Z')){ 
    String buffer;
    buffer.Format("%c", keycode);
//      PARS.Enabled = true ; // line number 23

    wordbox.Text=buffer;
//    InterfaceOn(0);
                           }
}
#sectionend on_key_press  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart interface_click  // DO NOT EDIT OR REMOVE THIS LINE
function interface_click(int interface, int button) {
if (interface==0) {    //starts parsing, including any global commands
String curs[10]; //Hard code cursor modes
curs[0]="walk";
curs[1]="look";
curs[2]="use";
curs[3]="talk";
curs[4]="USEINV";
curs[5]="get";
curs[6]="pointer";
curs[7]="wait";
curs[8]="LEC";
curs[9]="verb";
int counter,invspk;
String unknownbuffer;

input=("");
input.Format("%s",wordbox.Text);
// InterfaceOff(0);
if (StrComp(input,""))
{ParseText(input);

counter=0;
mouse.Mode=7;
while (counter < 9) {//set cursor mode to typed word
if (input.Contains(curs[counter]))
mouse.Mode=counter;
counter ++;}
// Check for other verbs used
if (StrContains(input,"eat")>-1) mouse.Mode=9;
     
counter=1;
invspk=0;
inv[0]=0;
inv[1]=0;
while (counter <= Game.InventoryItemCount) {
if (input.Contains(inventory[counter].Name)) {//check if spoke name of item
if(invspk<2){
inv[invspk]=counter;
invspk++;}
else Display ("Please don't refer to more than two items at a time.");
if (player.InventoryQuantity[counter]>0){//See if player has any
if (mouse.Mode!=7)
inventory[counter].RunInteraction(mouse.Mode);
else player.ActiveInventory = inventory[counter];
if (invspk>1)
if((player.InventoryQuantity[inv[0]]>0)&&(player.InventoryQuantity[inv[1]]>0)){ 
player.ActiveInventory = inventory[inv[0]];
if(inventory[inv[1]].IsInteractionAvailable(mouse.Mode)) inventory[inv[1]].RunInteraction(mouse.Mode);
else {
player.ActiveInventory = inventory[inv[1]];
inventory[inv[0]].RunInteraction(mouse.Mode);} 
mouse.Mode=7;
}
}
else Display ("You don't have any %s!",inventory[counter].Name);}
counter ++;}
       
counter = 0;
while (counter < Game.CharacterCount) {
nchar.Format("%s",character[counter].name);
if (input.Contains(nchar)) {//check if spoke name of Character
if (player.Room==character[counter].room){ //See if character is in room
nchar.Format("%s",character[counter].name);
character[counter].RunInteraction(mouse.Mode);}
else
Display ("%s  isn't here!",character[counter].name);
}
counter ++;}

counter = 0;
while (counter < Room.ObjectCount){
if (input.Contains(object[counter].Name)>-1){ //check if spoke name of object
nobj.Format("%s",object[counter].Name);
object[counter].RunInteraction(mouse.Mode);}
counter ++;} 

counter = 1;
while (counter < 30){
if (input.Contains(hotspot[counter].Name)>-1){ //check if spoke name of hotspot
nhot.Format("%s",hotspot[counter].Name);
hotspot[counter].RunInteraction(mouse.Mode);}
counter ++;}

Game.GlobalStrings[1] = input; //Set for unique room commands

      ParseText(input);
      if (Said("quit"))   QuitGame(1);
      else if (Said("save")) SaveGameDialog();
      else if (Said("restore")) RestoreGameDialog();
      else if (Said("restart")) RestartGame();
      else if (Said("inventory"))   InventoryScreen();

         SetGlobalInt(6,1); //sends signal to room to contine parsing
}
} // end if interface 0



}
#sectionend interface_click  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart unhandled_event  // DO NOT EDIT OR REMOVE THIS LINE
function unhandled_event (int what, int type) {
if (player.ActiveInventory.ID>0)  invent.Format("%s",player.ActiveInventory.Name);
if (what==1){
  if(nhot=="") nhot.Format("s%",Game.GetLocationName(mouse.x, mouse.y));
  if(type==1) Display( "You see nothing intersing at the %s.", nhot);
  else if (type==2) Display( "You can't use with the %s!", nhot);
  else if (type==3) Display ("I can't use the %s with the %s", invent,nhot);
  else if(type==4) Display ( "I can't talk with the %s.", nhot);}

else if(what==2){
  if(nobj=="") nobj.Format("s%",Game.GetLocationName(mouse.x, mouse.y));
  if(type==0) Display( "You see nothing intersing about the %s.", nobj);
  else if (type==1) Display( "The %s is useless, leave it alone.", nobj);
  else if (type==2) Display ( "I don't feel like talking with the %s.",nobj);
  else if (type==3) Display ("I can't use the %s with the %s",invent,nobj);}

else if (what==3){
  if(nchar=="") nchar.Format("s%",Game.GetLocationName(mouse.x, mouse.y));
  if(type==0) Display( "%s is a very plain looking person.", nchar);
  else if (type==1) Display ( "I don't think that %s will like this.", nchar);
  else if (type==2) Display ( "%s has nothing to say to you.", nchar);
  else if (type==3) Display ( "%s has no interest in your %s",nchar,invent);
  else if (GetCursorMode()==8) Display( "Not at this time.");}

nchar="";
nobj="";
nhot="";
}

The problem I am now having appears to be with the PARS.Enabled option as when I use it I get an error message stating:

Error (line 23):expected semicolon after ')'

and if I try using gPARS.Enabled instead then I get the message:
Error (line 23):Undefined token 'gPARS'

Khris

Don't you make use of the auto-complete window at all?
It's supposed to be "gPars", and the property is "Visible", not "Enabled".

If the auto-complete doesn't show up as soon as you've typed the third letter, AGS doesn't know the command/object/attribute/property you're referring to, thus won't compile.

mkennedy

The problem seamed to be caused by not capitalizing the first letter in the name of the GUI, so gPars.Visible seamed to work, but gPARS.Visible did not. However once the parser GUI was turned on I couldn't get it to turn back off.  Anyway I decided to make the module work independant of the GUI by storing the typed letters in a buffer and then feeding the buffer into the input for the autoparse code. when the player hits the return key.  So far I've tried the module out with the Demo Quest 3 game, But I haven't been able to see how well it works with other games as the module requires version 2.72 and I don't know of any other open source ags games. If anybody else want's to test the module out with their AGS game they can get it at:

http://www.rudolphuebe.com/suf/uploads/autoparsermod.zip

If you encounter any bugs with the module or have any questions send me a PM and I'll see if I can help.


Khris

To clarify this:
Calling your GUI "pars" will make AGS declare two things:

1. An integer constant called "PARS" (Script name) holding the ID of the GUI, its number.
The GUI can thus be referenced by using the built-in array: gui[PARS]

2. An instance called "gPars" (Script O-Name).

gui[PARS] is essentially the same as gPars.

The same is true for Dialogs, Characters, Objects, Hotspots and InventoryItems.
Either use the array with the all-caps script name as the index or put a small d/c/o/h/i in front of the script name to get the Script O-Name (which is always shown right next to the script name in the editor, btw.). Then capitalization looks like this: "xXxxxx.."

Every other instance (e.g. GUI controls) can be accessed inside a script using the exact name it was given in the editor, including the chosen capitalization.

Keep in mind that you usually don't have to worry about capitalization at all because AGS automatically corrects mistakes when one makes use of the auto-complete window.
-----------------------------

To turn the parser GUI back off, use "gPars.Visible=false;"

monkey0506

Simply stating that using the autocomplete will always make it compile correctly and not using it will make it fail to compile is a logical fallacy. There are instances where the autocomplete may produce invalid scripts. It's not a high-risk, but it is possible. And inversely there are times when the autocomplete won't properly detect valid scripts, but that doesn't mean they won't compile.

The autocomplete serves more as a general guide than an absolute iron fist. Most of the time it should work properly to help generate proper scripts. But you can't just say that failure to use it will always result in invalid scripts...Just "to clarify this". ;)

Khris

No offense monkey, but maybe you should have read my post a bit more carefully.
I was talking about capitalization exclusively, plus I've employed the word "usually".

Nowhere did I state that "using the autocomplete will always make it compile correctly and not using it will make it fail to compile" :P

Plus, "make use of sth." doesn't mean to completely shut off the brain while doing it, IMO.

But: having a GUI called "gPars" and then typing "gpa" or "gPA" or whatever will show the ac-window with "gPars" in the top line, so as I said, if you make use of the window in a case like this, it'll correct the capitalization.

Or won't it? (rhetorical question)

SMF spam blocked by CleanTalk