Is it possible to add objects at runtime?

Started by jumpjack, Mon 09/01/2023 16:48:04

Previous topic - Next topic

jumpjack

I can't find any suitable function to add/create room objects at runtime, is it possible? If not, can I "hookup" an overlay or a sprite (the only things I know how to create at runtime) to an object created in editor, so as to be able to use object functions on it?

Cassiebsg

Objects can't be created at run time, but you can create as many "dummy" objects as you need, and change it's sprite at runtime. No problem there.

I can't remember if it's possible to change the name of the object at runtime (the one used to refer to it in game - as in mouse over) but if not, you can create a custom property to hold the name and refer to it. Meaning that you can then also change the "name of the object" at runtime via the custom property.
There are those who believe that life here began out there...

jumpjack

I don't get it: can I do it or not?
How can I create "something" on which I can use these functions?

Cassiebsg

#3
Open your room, create a new object. Place it somewhere (even outside of the room).

Now you have an object you can use. you can call it oDummy as an exemple. and let's say it's object[2]....


Now, you just use it as regular object, cause that's what it is.

In room you'll just use oDummy.Animate... while in Global you can refer to it as object[2].Animate...

You assign the new sprite as in the manual example:

Quote
Code: ags
object[2].Graphic = 100;

or

Code: ags
oDummy.Graphic = 100;

will change the object 2 (oDummy)'s image to the image stored in the sprite manager's slot 100


You can also use dynamic sprite/overlay to assign the sprite, check the manual on how to do that.

There's really not much more than that, really.
If you need to change the name of the object at runtime, create a custom property for it, and then use Object[2].GetTextProperty to access and use it.
You will need to code how to handle this for the mouse over or any other place you need to refer to the name of this object.
There are those who believe that life here began out there...

Crimson Wizard

#4
Both answers are: "no", but there may be workarounds (of varied levels of ugliness).

For the first case, the existing workaround is known as "object pool", when you create alot of hidden objects in the editor, and then reuse them at runtime, remembering which ones are "in use" and which are "free". Same works for characters, and anything else. That is how arcade games were created in AGS in the past.

For the second case, depending on which particular functions do you want to use, you may sometimes utilize a hidden "reference object".
For one example, if you make a hidden object, you could animate it and make Overlay follow that animation, by setting current object's frames in late_repeatedly_execute_always.
For pathfinding, you may order a transparent object to move around, and sync Overlay's position with it.

Alternatively, you would have to reimplement these functions in script.

SMF spam blocked by CleanTalk