how to? String to type

Started by Theme, Fri 22/01/2010 22:18:00

Previous topic - Next topic

Theme

Hi. Is there any way to convert a object name string into a type, like this:

(Object) Game.GetLocationName(mouse.x,mouse.y).Transparency = 50;

(Object) "apple".Transparency = 50;

o/

Goldfish

? if you mean

oObject1.whatever

to

apple.whatever

then rename the object from the room -> objects menu.

Crimson Wizard

#2
QuoteHi. Is there any way to convert a object name string into a type, like this:

(Object) Game.GetLocationName(mouse.x,mouse.y).Transparency = 50;

(Object) "apple".Transparency = 50;

You do not need this, at all.

If you want to manipulate a specific object, just use its script name, like:

oApple.Transparency = 50;

If you want to do this with an object which is beyond mouse pointer, you do:

Object * obj = Object.GetAtScreenXY(mouse.x, mouse.y);
if (obj != null)
{
   obj.Transparency = 50;
}

monkey0506

If it's within the room script you can use the script name like:

Code: ags
oApple.Transparency = 50;


Otherwise you would need some sort of method to find an object by it's name:

Code: ags
Object* GetObjectByName(String name) {
  if (String.IsNullOrEmpty(name)) return null;
  if (player.Room == 1) {
    if (name == "apple") return object[0]; // object 0 in room 1 is the apple
    if (name == "banana") return object[1];
    if (name == "pear") return object[2];
    if (name == "peach") return object[3];
    if (name == "grape") return object[4];
  }
  else if (player.Room == 2) {
    if (name == "door") return object[0]; // but in room 2 object 0 is the door
    if (name == "table") return object[1];
    // ...etc...
  }
  // ...etc...
  return null; // if no object was found for this room by the given name, return null
}


Also AGS doesn't support the "function().Property" syntax, you would have to store the result first:

Code: ags
String name = Game.GetLocationName(mouse.x, mouse.y);
Object *obj = GetObjectByName(name);
if (obj != null) obj.Transparency = 50;


However in this case you'd probably just want to use Object.GetAtScreenXY instead:

Code: ags
Object *oat = Object.GetAtScreenXY(mouse.x, mouse.y);
if (oat != null) oat.Transparency = 50;


I knew I'd been beaten but I like typing :D

Theme

thanks. I used

Code: ags

  if(GetLocationType(mouse.x,mouse.y) == eLocationObject) {
    
    Object *obj = Object.GetAtScreenXY(mouse.x, mouse.y);
    if (obj != null) obj.Transparency = 0;    
    
  }


So it's not possible to convert types directly

o/

Crimson Wizard

Quote from: Exsecratus on Fri 22/01/2010 23:01:31
So it's not possible to convert types directly
The "conversion" you spoke above can't be called "type conversion". It is rather finding an object by its name, which is far not the same.

Yet there are such conversion functions like IntToFloat and FloatToInt (maybe something else).

monkey0506

Any type which has a unique identifier and a global access point can technically be converted to at least one of the basic types (int, float, String) and then converted from there, but no it's not directly possible.

For example you could do:

Code: ags
String ToString(this Object*) {
  return String.Format("%d", this.ID);
}

Object* ToObject(this String*) {
  return object[this.AsInt];
}

String s = object[5].ToString();
Object *o = s.ToObject();


If you wanted to use the name instead of the ID property then you could implement a finding function like I showed above.

SMF spam blocked by CleanTalk