So naive: instancing an object

Started by fratello Manu, Sat 28/11/2020 07:57:38

Previous topic - Next topic

fratello Manu

Hi everyone!

I'm quite sure this is a really naive question but I couldn't find any similar previous thread.

I want to define a temporary object (or even a DynamicSprite as well) locally in a function, just to display a one-shot animation on screen (e.g. explosion of something, not suitable as a permanent object).

I created the explosion View, then I made a function as:

Code: ags

function ShowExplosion(int x, int y)
{
    Object* objExplosion;
    objExplosion = new Object;

   objExplosion.X = x;
   objExplosion.Y = y;
   
   objExplosion.SetView(......proper view...);
   objExplosion.Animate(...proper animation....);
}


but I get a compiler error: built-in tipe "object" cannot be instantiated directly.

Then I tried to declare the objExplosion object globally instead of inside my function to see if something changes, according to instructions for the New statement on manual.... Nope.

Obviously, in C# I'd just write:

Code: ags

Object objExplosion = new Object();


So, I don't understand what I'm doing wrong.

Thanks!

Snarky

The problem is just what the error message tells you: built-in types cannot be instantiated directly. AGS has to keep track of instances of game types like characters, room objects, etc., and that means that you cannot create new ones yourself. For some types, like DynamicSprite, there are factory functions that allows you to create them, while othersâ€"including room objectsâ€"are limited to what's defined in the AGS project.

In this case, the easiest solution is probably to create an explosion room object in the editor, and just set it to be hidden until the explosion happens. In other cases you can have generic objects or characters that you can reuse for different purposes, or you can display things on overlays or GUIs.

Also note that even if you could create your own room objects dynamically, the function you wrote wouldn't work unless the animation was blocking, because the object would go out of scope immediately when the game left the function, and it would disappear or crash the game (this typically happens with DynamicSprites unless you're careful).

eri0o

If you do need to instantiate something, you can have a bunch of pre-created dummy characters that you pickup from a room that's not part of your game, moved from there to the current room, do what you must, and put them back in that room once business are taken care. In this way you are not creating them dynamically but you still get to do similar things.

Crimson Wizard

#3
Quote from: eri0o on Sat 28/11/2020 13:18:22pre-created dummy characters that you pickup from a room that's not part of your game

You can assign Character.Room to non-existing room number, commonly negative (e.g. -1), this will efficiently hide them from the game. The only exception is player character ofcourse, because assigning Room for them makes game go there, which will result in error in case of a missing room.


As for instancing the game objects, it's sort of in plans to let users spawn them at runtime, but that would require some changes in the engine first. But this will still not be done with "new" keyword, due to how ags script currently works it can only be used to create user structs (unless there will be more changes to script compiler). So, like Snarky said, they wil likely be performed through helper functions, similar to existing DynamicSprite.Create().

eri0o

Quote from: Crimson Wizard on Sat 28/11/2020 13:24:29
You can assign Character.Room to non-existing room number, commonly negative (e.g. -1), this will efficiently hide them from the game.

8-0 I didn't knew that! Thanks! Learned something today :)

fratello Manu

Quote from: eri0o on Sat 28/11/2020 13:18:22
If you do need to instantiate something, you can have a bunch of pre-created dummy characters that you pickup from a room that's not part of your game, moved from there to the current room, do what you must, and put them back in that room once business are taken care. In this way you are not creating them dynamically but you still get to do similar things.

Thanks!! I did like that.

SMF spam blocked by CleanTalk