Place a new object in room

Started by OgreVorbis, Tue 11/12/2018 10:19:08

Previous topic - Next topic

OgreVorbis

I am making my first "real" game in AGS. I need to have the ability to place an inventory item anywhere in the game (in any room) and have it spawn a certain object. Is it possible to do this with out creating a dummy object in each room and then setting it to visible? It would be best if I could also designate within the walkable area. I really need a solution for this because there are a number of objects in my game that can be placed anywhere by the character and so I'll end up with a lot of dummy objects and it will take a lot of time. I could always make a designated area, but it would make the game less fun for other reasons.
If you know how, I'd appreciate a little example code. Don't just assume I know because I'm new to this.

Snarky


OgreVorbis

...add it as a character. I should of thought of that sooner. I'll give that a try.

OgreVorbis

#3
I'm not sure what event I should put the code in then.
I need something like an event for mouse click in every room. Is there a way to do this?
In it I would put something like:
Code: ags

if (cEgo.ActiveInventory == iFirePit)
cFirePit.ChangeRoom(cEgo.Room, mouse.x, mouse.y);

If there is a better way, let me know.Thanks!

Snarky

To avoid separate functions for every inventory item, use a mapping from item to character. If there are only a few you can just do a function with an if/else-if or switch/case block, but if there are more I would use the indices. If you have e.g. 30 inventory items, set aside at least 30 consecutive characters. Then the inv. item index + index of the first inv. character gives you the character you want.

Khris

Quote from: OgreVorbis on Tue 11/12/2018 10:58:16I need something like an event for mouse click in every room. Is there a way to do this?
Yes, if you add
Code: ags
function on_mouse_click(MouseButton button) {
  // code here
}
to your room script, AGS will call it before calling the global one.

However, just like Snarky suggests with the inventory items, never implement global functionality on a per-room or per-item basis. You will only end up with tons of hard to manage duplicate code.
The global on_mouse_click is called when you click anywhere inside a room, so you'd use the existing eMouseLeft block, check eMouseMode == eModeUseinv and Game.GetLocation(mouse.x, mouse.y) ==  eLocationNothing and handle the click accordingly.

Crimson Wizard

#6

Quote from: Snarky on Tue 11/12/2018 10:36:03
Check out this thread: http://www.adventuregamestudio.co.uk/forums/index.php?topic=55827.0
Quote from: OgreVorbis on Tue 11/12/2018 10:48:13
...add it as a character. I should of thought of that sooner. I'll give that a try.
Quote from: OgreVorbis on Tue 11/12/2018 10:58:16I'm not sure what event I should put the code in then.


I really suggest reading that thread to the end, it contains actual code examples and discusses potential problems you may encounter. Not all of it is related to placing items in rooms of course, so some things could be skipped.

I think actual inventory and UI code is being discussed starting from this post.

Snarky

Just a heads up: There are a lot of complications in that thread that presumably won't apply, but I nevertheless think this will be a pretty challenging task for someone who is new to AGS coding.

OgreVorbis

QuoteThe global on_mouse_click is called when you click anywhere inside a room, so you'd use the existing eMouseLeft block, check eMouseMode == eModeUseinv and Game.GetLocation(mouse.x, mouse.y) ==  eLocationNothing and handle the click accordingly.
Thanks for all the replies. I didn't have that many characters, so the easy method worked fine.

SMF spam blocked by CleanTalk