Script Problem

Started by , Mon 19/02/2007 18:20:35

Previous topic - Next topic

Bogald

Hi, could you help to "translate" this script to the new AGS language (v.2.72) because this script is a bit old.

function repeatedly_execute() {
  string buffer;
  string madetext;
  int cur_mode;
  int useorgive;
  StrCopy (madetext, "");
  cur_mode = GetCursorMode();
  if (cur_mode == MODE_WALK)
    StrCat(madetext,"Gehe zu ");
  else if (cur_mode == MODE_LOOK)
    StrCat (madetext,"Schau an ");
  else if ((cur_mode == MODE_USE) && (GetGlobalInt(1)==1))
    StrCat(madetext,"Benutze ");
  else if ((cur_mode == MODE_USE) && (GetGlobalInt(1)==0))
    StrCat(madetext,"Gib ");
  else if (cur_mode == MODE_TALK)
    StrCat(madetext,"Rede mit ");
  else if (cur_mode == 5)
    StrCat(madetext,"Nimm ");
  else if ((cur_mode == 4) && (GetGlobalInt(1)==1))
    {
    StrCat(madetext,"Benutze ");
    GetInvName (player.activeinv, buffer);
    StrCat(madetext,buffer);
    StrCat(madetext," mit ");
    }
  else if ((cur_mode == 4) && (GetGlobalInt(1)==0))
    {
    StrCat(madetext,"Gib ");
    GetInvName (player.activeinv, buffer);
    StrCat(madetext,buffer);
    StrCat(madetext," an ");
    }

  else if (cur_mode == 8) {
    if (GetGlobalInt(80) == 1) StrCat(madetext,"SchlieàŸe ");
    if (GetGlobalInt(80) == 2) StrCat(madetext,"Gib ");
    if (GetGlobalInt(80) == 3) StrCat(madetext,"Öffne ");
    if (GetGlobalInt(80) == 4) StrCat(madetext,"Drücke ");
    if (GetGlobalInt(80) == 5) StrCat(madetext,"Ziehe ");
    }

  // Find out what's under the cursor, and add it to the status line
  GetLocationName(mouse.x,mouse.y,buffer);
  StrCat(madetext,buffer);
  SetLabelText ( 0, 12, madetext);
  }


Lt. Smash

function repeatedly_execute() {
  String buffer;
  String madetext;
  int cur_mode;
  int useorgive;
  madetext.Copy();
  cur_mode = mouse.Mode;
  if (cur_mode == MODE_WALK)
    madetext.Append("Gehe zu ");
  else if (cur_mode == MODE_LOOK)
    madetext.Append("Schau an ");
  else if ((cur_mode == MODE_USE) && (GetGlobalInt(1)==1))
    madetext.Append("Benutze ");
  else if ((cur_mode == MODE_USE) && (GetGlobalInt(1)==0))
    madetext.Append("Gib ");
  else if (cur_mode == MODE_TALK)
    madetext.Append("Rede mit ");
  else if (cur_mode == 5)
    madetext.Append("Nimm ");
  else if ((cur_mode == 4) && (GetGlobalInt(1)==1))
    {
    madetext.Append("Benutze ");
    buffer = player.ActiveInventory.Name;
    madetext.Append(buffer);
    madetext.Append(" mit ");
    }
  else if ((cur_mode == 4) && (GetGlobalInt(1)==0))
    {
    madetext.Append("Gib ");
    buffer = player.ActiveInventory.Name;
    madetext.Append(buffer);
    madetext.Append(" an ");
    }

  else if (cur_mode == Cool) {
    if (GetGlobalInt(80) == 1) madetext.Append("SchlieàŸe ");
    if (GetGlobalInt(80) == 2) madetext.Append("Gib ");
    if (GetGlobalInt(80) == 3) madetext.Append("Öffne ");
    if (GetGlobalInt(80) == 4) madetext.Append("Drücke ");
    if (GetGlobalInt(80) == 5) madetext.Append("Ziehe ");
    }

  // Find out what's under the cursor, and add it to the status line
  buffer = Game.GetLocationName(mouse.x,mouse.y);
  madetext.Append(buffer);
  SetLabelText ( 0, 12, madetext);
  }

I think that should help ;)

Ashen

#2
Looks pretty good, except:

Code: ags

madetext.Copy();


Won't do anything (you haven't told it where to copy madetext to - String Buff = madetext.Copy() for example). I think it what you want is:
Code: ags

madetext = "";


SetLabelText is also obsolete - but it's harder to know what to replace it with, since we'd need the Label's Scriptname to use the Label.Text property.
Something like:
Code: ags

Label *theLab = gui[0].Controls[12];
theLab.Text(madetext);

MIGHT work (should do, but untested) - but use the actual Label name to be sure.

And in the copy, else if (cur_mode == 8 ) became else if (cur_mode == Cool) (damn 8) smilie)- don't forget to change it back.


EDIT:
Ah, crap. I remember String.Append won't work either, come back to post, and find monkey's beaten me to it. AND actually bothered to improve the code, instread of just rote translating it. Damn monkey, making me look bad ... :-[
I know what you're thinking ... Don't think that.

monkey0506

#3
Code: ags
function repeatedly_execute() {
  String madetext;
  if (mouse.Mode == eModeWalkto) madetext = "Gehe zu ";
  else if (mouse.Mode == eModeLookat) madetext = "Schau an ";
  else if ((mouse.Mode == eModeUse) && (GetGlobalInt(1) == 1)) madetext = "Benutze ";
  else if ((mouse.Mode == eModeUse) && (GetGlobalInt(1) == 0)) madetext = "Gib ";
  else if (mouse.Mode == eModeTalk) madetext = "Rede mit ";
  else if (mouse.Mode == eModePickup) madetext = "Nimm ";
  else if (mouse.Mode == eModeUseinv) {
    if (GetGlobalInt(1) == 1) {
      madetext = "Benutze ";
      if (player.ActiveInventory != null) madetext = madetext.Append(String.Format("%s mit ", player.ActiveInventory.Name));
      }
    else if (GetGlobalInt(1) == 0) {
      madetext = "Gib ";
      if (player.ActiveInventory != null) madetext = madetext.Append(String.Format("%s an ", player.ActiveInventory.Name));
      }
    }
  else if (mouse.Mode == 8) {
    if (GetGlobalInt(80) == 1) madetext = "SchlieàŸe ";
    if (GetGlobalInt(80) == 2) madetext = "Gib ";
    if (GetGlobalInt(80) == 3) madetext = "Öffne ";
    if (GetGlobalInt(80) == 4) madetext = "Drücke ";
    if (GetGlobalInt(80) == 5) madetext = "Ziehe ";
    }
  // Find out what's under the cursor, and add it to the status line
  Label* statuslabel = gui[0].Controls[12].AsLabel;
  if (statuslabel != null) statuslabel.Text = String.Format("%s%s", madetext, Game.GetLocationName(mouse.x, mouse.y));
  }


No offense Lt. Smash, but that code is absolutely wrong on several levels. That's improper usage of Strings for one thing, you never even assign any values. In fact, the game would crash at the first madetext.Append because of madetext still being null.

As far as I can tell, there's no advantage here to using cur_mode instead of mouse.Mode. Previously it was common to store the value in a variable, but it's no longer needed.

Furthermore, you did nothing to correct the copying and pasting of the 8) smiley changing to the text "Cool".

I would suggest changing "if (mouse.Mode == 8 )" to the new enumerated value. This will be the name of your cursor without any spaces and "eMode" at the front of it. For example, if the cursor is named "My other cursor", replace the "8" with "eModeMyothercursor". This makes the code more readable, as well as helping to prevent errors if you used the wrong cursor mode.

Also, I would suggest assigning your status label a script o-name (read the manual entry on "Upgrading to AGS 2.7" for information on how to do this). Then you can change this code:

Code: ags
Label* statuslabel = gui[0].Controls[12].AsLabel;
  if (statuslabel != null) statuslabel.Text = String.Format("%s%s", madetext, Game.GetLocationName(mouse.x, mouse.y));


To this code (I'll assume you'll use the script o-name of lblStatusbar):

Code: ags
lblStatusbar.Text = String.Format("%s%s", madetext, Game.GetLocationName(mouse.x, mouse.y));


I hope you can get something out of this aside from just copying and pasting the code.

[EDIT:]

Ashen actually you're incorrect. His code where it says "madetext.Append" doesn't do anything. String.Append doesn't actually modify the String, it returns a copy of the modified String, so when using String.Append you have to use the format "str = str.Append(otherStr);". You were right that madetext.Copy() doesn't actually do anything, but, as I've mentioned, neither does his madetext.Append-ing.

P.S., you can use the [ code ] and [ /code ] tags (without spaces) to enclose your code snippets. This will help prevent things like:

Code: ags
if (mouse.Mode == 8)


Changing into:

if (mouse.Mode == 8)

Since "8)" is a smiley in bbCode, but if you enclose it in the code tags, it won't be changed to a smiley.

[After Ashen's edit:]

Ahhh...I have waited long and hard for this day to come. And finally here it is!

No hard feelings mate, I was just in the middle of typing when you posted, and you made the same mistake that Lt. Smash did, so it was an easy one to catch.

The majority of the time I'm willing to help people out like this ("improv[ing] the code"), because I know that if people hadn't helped me out when I was getting started with AGS I wouldn't know nearly as much as I do now. Which is actually somewhat frightening seeing as the code I'm currently working on is implementing (via a very complex bit of coding which will probably be too slow anyway) a 3-dimensional vector. The best part about it is that each dimension is critically dependent on the last, so there's not even any way to split it up. But...I'm getting off topic here. So...

Bogald


SMF spam blocked by CleanTalk