(Solved)creating an object through script with an integer for a name.

Started by Pajama Sam, 26 Feb 2022, 15:47

« previous - next »

Pajama Sam

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

int c2;
function hHotspot5_AnyClick()
{
  Object* pipe+c2;
  c2+=1;
}

Snarky

No.

But what you can do is create an array of objects, and use the number as the array index.

Pajama Sam

I'm not sure what you mean by an array of objects.

Crimson Wizard

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.

Khris

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

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
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.

Pajama Sam

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.




This is the script I used.
Code: ags

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;
  }
}


Crimson Wizard

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

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

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?

Pajama Sam

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

Overlay *over+c1 = Overlay.CreateGraphical(X, Y, SPRITE_NUMBER, true);
C1+=1;

Crimson Wizard

Quote from: Pajama Sam on 28 Feb 2022, 21:06
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 28 Feb 2022, 21:06
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

// 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;
   }
}

Pajama Sam


SMF spam blocked by CleanTalk