Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: FanOfHumor on Sat 26/02/2022 15:47:28

Title: (Solved)creating an object through script with an integer for a name.
Post by: FanOfHumor on Sat 26/02/2022 15:47:28
It would be useful to me if I could create new objects through script everytime a hotspot is clicked.
I want the name of the new hotspot to be its name plus an integer number.I have the integer adding everytime so that It would make a new object with a different name.Is it possible to add an integer to a new objects name?

Code (ags) Select

int c2;
function hHotspot5_AnyClick()
{
  Object* pipe+c2;
  c2+=1;
}
Title: Re: creating an object through script with an integer for a name.
Post by: Snarky on Sat 26/02/2022 17:02:36
No.

But what you can do is create an array of objects, and use the number as the array index.
Title: Re: creating an object through script with an integer for a name.
Post by: FanOfHumor on Sat 26/02/2022 18:06:24
I'm not sure what you mean by an array of objects.
Title: Re: creating an object through script with an integer for a name.
Post by: Crimson Wizard on Sun 27/02/2022 06:15:32
At the moment AGS does not support creating new objects in script, it can only use objects that you created in the editor.

Writing "Object* pipe" won't create an object itself, but would create a pointer variable to object, which may be after assigned to an actual object.

Could you tell what exactly are you trying to make with these objects? There may be solutions, depending on what you are intending to do.
Title: Re: creating an object through script with an integer for a name.
Post by: Khris on Mon 28/02/2022 09:57:05
You need to create the Objects in the editor first. Say your room is using five objects currently. Now you create ten additonal objects and assign sprite 0 to them.

When you need them in-game, you use the object[] array.

Code (ags) Select

int c2 = 5; // object[0] - object[4] are used for regular objects, so we start at 5

function hHotspot5_AnyClick()
{
  Object* o = object[c2];
  o.Graphic = 123;
  o.Visible = true;
  // etc.

  c2++;
}


You can now use a single function to interact with all of them:

Code (ags) Select
function dynObjects_AnyClick()
{
  Object* o = Object.GetAtScreenXY(Mouse.x, Mouse.y);
  if (o == null) return;

  int id = o.ID;
  if (id == 5) {  // object[5] was clicked
    // some code here
  }
  // etc.
}


Now paste  dynObjects_AnyClick  into each object's "any click on" field.
Title: Re: creating an object through script with an integer for a name.
Post by: FanOfHumor on Mon 28/02/2022 15:03:50
I was hoping to be able to simplify this.Just In case you can't see what it is exactly its animating every time the mouse is clicked without restarting because its using other objects to animate the other clicks.
(https://i.imgur.com/ZVq3dSS.gif)



This is the script I used.
Code (ags) Select

int c1;
function hHotspot4_AnyClick()
{
  if(c1==0)
  {
    bubbles.SetView(6);
    Bubble.Play();
    bubbles.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c1==1)
  {
    bubbles101.SetView(6);
    Bubble.Play();
    bubbles101.Animate(0, 3, eOnce, eNoBlock);
  } 
  if(c1==2)
  {
    bubbles102.SetView(6);
    Bubble.Play();
    bubbles102.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c1==3)
  {
    bubbles103.SetView(6);
    Bubble.Play();
    bubbles103.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c1==4)
  {
    bubbles104.SetView(6);
    Bubble.Play();
    bubbles104.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c1==5)
  {
    bubbles105.SetView(6);
    Bubble.Play();
    bubbles105.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c1==6)
  {
    bubbles106.SetView(6);
    Bubble.Play();
    bubbles106.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c1==7)
  {
    bubbles107.SetView(6);
    Bubble.Play();
    bubbles107.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c1==8)
  {
    bubbles108.SetView(6);
    Bubble.Play();
    bubbles108.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c1<9)
  {
    c1+=1;
  }
  if(c1==9)
  {
    c1=0;
  } 
}
int c2;
function hHotspot5_AnyClick()
{
  if(c2==0)
  {
    bubbles2.SetView(19);
    Bubble.Play();
    bubbles2.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c2==1)
  {
    bubbles201.SetView(19);
    Bubble.Play();
    bubbles201.Animate(0, 3, eOnce, eNoBlock);
  } 
  if(c2==2)
  {
    bubbles202.SetView(19);
    Bubble.Play();
    bubbles202.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c2==3)
  {
    bubbles203.SetView(19);
    Bubble.Play();
    bubbles203.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c2==4)
  {
    bubbles204.SetView(19);
    Bubble.Play();
    bubbles204.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c2==5)
  {
    bubbles205.SetView(19);
    Bubble.Play();
    bubbles205.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c2==6)
  {
    bubbles206.SetView(19);
    Bubble.Play();
    bubbles206.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c2==7)
  {
    bubbles207.SetView(19);
    Bubble.Play();
    bubbles207.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c2==8)
  {
    bubbles208.SetView(19);
    Bubble.Play();
    bubbles208.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c2<9)
  {
    c2+=1;
  }
  if(c2==9)
  {
    c2=0;
  }
}
int c3;
function hHotspot6_AnyClick()
{
  if(c3==0)
  {
    bubbles3.SetView(20);
    Bubble.Play();
    bubbles3.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c3==1)
  {
    bubbles301.SetView(20);
    Bubble.Play();
    bubbles301.Animate(0, 3, eOnce, eNoBlock);
  } 
  if(c3==2)
  {
    bubbles302.SetView(20);
    Bubble.Play();
    bubbles302.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c3==3)
  {
    bubbles303.SetView(20);
    Bubble.Play();
    bubbles303.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c3==4)
  {
    bubbles304.SetView(20);
    Bubble.Play();
    bubbles304.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c3==5)
  {
    bubbles305.SetView(20);
    Bubble.Play();
    bubbles305.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c3==6)
  {
    bubbles306.SetView(20);
    Bubble.Play();
    bubbles306.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c3==7)
  {
    bubbles307.SetView(20);
    Bubble.Play();
    bubbles307.Animate(0, 3, eOnce, eNoBlock);
  }
    if(c3==8)
  {
    bubbles308.SetView(20);
    Bubble.Play();
    bubbles308.Animate(0, 3, eOnce, eNoBlock);
  }
  if(c3<9)
  {
    c3+=1;
  }
  if(c3==9)
  {
    c3=0;
  }
}

Title: Re: creating an object through script with an integer for a name.
Post by: Crimson Wizard on Mon 28/02/2022 20:50:47
Maybe you will be able to use Overlays instead. Overlays may be created in script, and do not have a limit. The possible problems with them are
a) you have to make your own variables or arrays to store and use them.
b) they are positioned in screen coordinates instead of room ones, but if your room is non-scrolling, that might not be a problem at all;
c) overlays have fixed sprite, and do not have "Animate" command, so if you need them to animate you'd have to write extra script for that.


Overlays are created like:
Code (ags) Select

Overlay *over = Overlay.CreateGraphical(X, Y, SPRITE_NUMBER, true);

they dont have a "Move" command, so their coordinates should be changed by hand, in room's RepExec function:
Code (ags) Select

over.Y = over.Y - speed; // just an example of overlay moving up


For more information see https://adventuregamestudio.github.io/ags-manual/Overlay.html

Unfortunately I don't have much spare time right now; but maybe someone else could give more detailed information on this?
Title: Re: creating an object through script with an integer for a name.
Post by: FanOfHumor on Mon 28/02/2022 21:06:34
How would I script an overlay to animate?Is this similar to animating dynamic sprites?Also could it create new overlays with an adding integer in its name when the hotspots clicked.

Something similar to this?
Code (ags) Select

Overlay *over+c1 = Overlay.CreateGraphical(X, Y, SPRITE_NUMBER, true);
C1+=1;
Title: Re: creating an object through script with an integer for a name.
Post by: Crimson Wizard on Mon 28/02/2022 22:42:23
Quote from: Pajama Sam on Mon 28/02/2022 21:06:34
How would I script an overlay to animate?

In short, you delete the previous overlay and recreate with the new sprite on the last position.

Quote from: Pajama Sam on Mon 28/02/2022 21:06:34
Also could it create new overlays with an adding integer in its name when the hotspots clicked.

This may be done using "arrays".
Topic on arrays in the manual: https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#arrays

Rough example:
Code (ags) Select

// array of Overlay pointers, size of MAX_BUBBLES
Overlay* bubbleOverlays[MAX_BUBBLES];

int c1;
function hHotspot4_AnyClick()
{

    bubbleOverlays[c1] = Overlay.CreateGraphical(X, Y, SPRITE_NUMBER, true);
    c1+=1;

    // if reached array limit, reset the counter
    if (c1 == MAX_BUBBLES) {
        c1 = 0;
   }
}
Title: Re: creating an object through script with an integer for a name.
Post by: FanOfHumor on Mon 28/02/2022 23:33:59
Thanks! I'll try that out.