Function to place characters randomly in a room

Started by Egmundo Huevoz, Thu 18/01/2018 10:08:52

Previous topic - Next topic

Egmundo Huevoz

Hello, everyone. I'm trying to make an extender function to place my characters randomly in any given room, and later call it from on_event. If all my rooms where the game resolution, it wouldn't be a problem. But I want this to work with my many scrolling rooms, so i figured I would use the Room.Width and Height commands. The thing is, I don't know how to convert an int (Character.Room) to a "Room*" (so I can use Room.Width/Height). Maybe there's even a better way to approach this, but I can't figure this out.

Code: ags
function PlaceRandomly(this Character*){
  Room* room_number = this.Room; // <--- This is my problem: Type mismatch: cannot convert 'int' to 'Room*'
  this.x=Random(room_number.Width);
  this.y=Random(room_number.Height);
  this.PlaceOnWalkableArea();
}

Khris

Just to get this out of the way, since you keep doing it, the variable name room_number is a really bad choice. It's a pointer to a Room instance, NOT a number, i.e. integer.
Anyway, converting would only be possible if AGS had an array of rooms so you could do
Code: ags
  Room* room = rooms[this.Room];

It doesn't though afaik, so this is not possible.
Code: ags
  int room_number = this.Room;  // possible, but pointless


Room.Width is static, used exactly like that in the manual's example. Which means there's only one available .Width, that of the currently loaded room.
To do what you want to do, you need to schedule the random placement, then call it in your on_event/eEventBeforeFadein block if (this.Room == player.Room), i.e. the player has entered the room the character is currently in, too.

Egmundo Huevoz

Quote from: KhrisJust to get this out of the way, since you keep doing it, the variable name room_number is a really bad choice. It's a pointer to a Room instance, NOT a number, i.e. integer.
Yes, I understand that. I named it like that so I can remember what it does, even though I know it's not really a number. Maybe current_room would have been better, idk.
Quote from: KhrisAnyway, converting would only be possible if AGS had an array of rooms
Yeah, I was hoping such an array existed, like with objects. Bummer. :(
Quote from: KhrisWhich means there's only one available .
I suspected as much, but I wasn't sure.
Quote from: Khrisyou need to schedule the random placement, then call it in your on_event/eEventBeforeFadein block if (this.Room == player.Room), i.e. the player has entered the room the character is currently in, too.
That's what I was trying to avoid, since this only happens when the player encounters a "random encounter" in a map room (you probably figured out I'm making a sort of RPG/adventure hybrid), therefore I only need this to happens in like 30, 40 rooms. I'll probably use a room property and call it from on_event only if the player is in a room with such a property. I guess the objective of this post was to find out if an array of rooms existed, or if there was any way of converting room to int. There isn't, though luck. (laugh)

Thanks again, Khris. And thanks for everything. Since I've come back to AGS I've learned to do a lot of things from you. I wouldn't be making this game if it wasn't for your help. Be sure you're gonna have a huge thank you in the credits! (nod)

Khris

You're welcome, and I'm looking forward to the game, we need more RPG/Adventure hybrids! I love those. ;-D

SMF spam blocked by CleanTalk