ENIGMA Forums

Contributing to ENIGMA => Function Peer Review => Topic started by: Josh @ Dreamland on September 09, 2010, 12:40:21 pm

Title: Instance Interface How-to
Post by: Josh @ Dreamland on September 09, 2010, 12:40:21 pm
Some functions need to interface with the current instance or with all instances.
The new instance system I was singing praises about for so long offers a simple interface to do this.

To fetch the currently active instance, use
Code: [Select]
enigma::instance_event_iterator.
Example:
Code: [Select]
int instance_destroy()
{
  enigma::instance_event_iterator->inst->unlink();
  return 0;
}

Iterating instances is about as easy.
As I've said, ENIGMA uses a unified iterator class. The class contains two members of concern; a pointer to the instance (guaranteeing only id and object_index members) and a pointer to the next iterator. If at any point the pointer is NULL, iteration is over.

An iterator can be fetched by an integer just like in EDL by using enigma::fetch_inst_iter_by_int(obj).

Example:
Code: [Select]
int instance_find(int obj, int num)
{
  int nth=0;
  for (enigma::inst_iter *it = enigma::fetch_inst_iter_by_int(obj); it != NULL; it = it->next)
  {
    nth++;
    if (nth>num)
    return (int) it->inst->id;
  }
  return noone;
}

A system has not yet been added to manipulate locals that are not guaranteed (such as health).
Variables that are key parts of the system (such as x and y) are included in different tiers of ENIGMA's instance system. The lowest tier is collisions, implementing bbox_* variables, and solid. The graphics tier implements such variables as sprite_index and image_index (which may eventually be removed as its use seems to indicate that placing it in system tiers is unnecessary). x,y, speed, direction, hspeed, and vspeed are implemented in the tier below that. Variables id and object_index are implemented in the bottommost tier.

Title: Re: Instance Interface How-to
Post by: TheExDeus on September 09, 2010, 03:52:48 pm
How to interface with rooms? I am trying to make sprite_tiled, but I need to know room_width/room_height, or even better would be view_width and view_height, as that would make sprites tile only in view, and thus eliminate call when outside the screen.
I tried including roomsystem.h and then just use room_width and room_height, but it trows:
Quote
../../Universal_System/roomsystem.h:46: error: `string' does not name a type
../../Universal_System/roomsystem.h:85: error: `string' does not name a type
../../Universal_System/roomsystem.h:86: error: `string' does not name a type
So I guess its not what I want to include? I saw that there are file like GMinstance.h and another file instance.h, which looks similar in function, so are some files redundant?
Title: Re: Instance Interface How-to
Post by: Josh @ Dreamland on September 09, 2010, 04:05:39 pm
HaRRiKiRi: You should just be able to use them as-is. The resource is included after the rest of the variables.
Title: Re: Instance Interface How-to
Post by: TheExDeus on September 10, 2010, 04:31:47 am
Something like enigme:room_width? Because room_width isn't declared in the functions scope and it trows an error.

edit: Nevermind. extern int room_width, room_height; fixed it.

Also, I know that backgroundstruct.h exists, but there are no GSbackground.h and .cpp files, so can I try to create them so I can make some background functions? Or is it better for me to wait a little so you can make them in the distribution?
Title: Re: Instance Interface How-to
Post by: Josh @ Dreamland on September 10, 2010, 12:41:20 pm
If you feel you can, go for it. I forget where I left off with backgrounds; they may or may not be loaded properly. See backgroundinit.cpp.

Looks like I left off after checking how many backgrounds there are. I can finish that file later today.

If you are operating from "Stable," I suggest you unzip and run again, marking "Trunk" and "I'm a dev, don't touch my changes."

If you check out from the trunk, you'll be informed of an update every time I commit something new.
Title: Re: Instance Interface How-to
Post by: TGMG on September 27, 2010, 01:05:36 pm
Whats the best way to get variables like sprite_index?
I was using this code which works in certain events, but causes a segfault in the create event :/ Is there a better way to do this?
((enigma::object_graphics*)enigma::instance_event_iterator->inst)->sprite_index=spritep;
Title: Re: Instance Interface How-to
Post by: TheExDeus on September 27, 2010, 03:43:24 pm
I see that TGMG added some basic background support. This will allow me to make all of the background functions. Thou I downloaded the newest from SVG, then recompiled, but I still don't see draw_background() function even thou its in GSbackground.cpp. I guess LGM needs to be updates as well? If so, then I will wait. :)
Title: Re: Instance Interface How-to
Post by: TGMG on September 27, 2010, 03:58:25 pm
@HaRRiKiRi - hey :) yeah i am in the process of adding them, i still have to commit the writing and reading code, test on windows and then you will see them in the lgm window. I'll update this post when i have everything sorted out.

Try now (remember to recompile the compiler, so delete the .dll file, lgm will compile it for you when the dll file is missing), atm its still quite basic.
Title: Re: Instance Interface How-to
Post by: Josh @ Dreamland on September 27, 2010, 08:04:12 pm
*slightly scared about new system he has no knowledge of*
TGMG: In the events of the object, sprite_index = spritep will suffice. If called from the create event, I understand the problem. I'll fix that ASAP.
Title: Re: Instance Interface How-to
Post by: TheExDeus on September 28, 2010, 02:43:36 am
Quote
Try now (remember to recompile the compiler, so delete the .dll file, lgm will compile it for you when the dll file is missing), atm its still quite basic.
Great work. :) I will make those functions when I get home.
Title: Re: Instance Interface How-to
Post by: TGMG on September 28, 2010, 04:16:00 am
*slightly scared about new system he has no knowledge of*
TGMG: In the events of the object, sprite_index = spritep will suffice. If called from the create event, I understand the problem. I'll fix that ASAP.
aye sorry about that, but its very similar to the way sprites are done.

well i was implementing the action functions and then realised that they always segfault in the create event, so I was using:
(enigma::object_graphics*)enigma::instance_event_iterator->inst)->sprite_index=spritep;
to access it
Title: Re: Instance Interface How-to
Post by: Josh @ Dreamland on September 28, 2010, 12:00:45 pm
I see. Yeah, syntax checker doesn't like macros at the moment, but when I have that fixed, #define action_set_sprite(X) sprite_index = (X); is really the way to go.

Title: Re: Instance Interface How-to
Post by: TGMG on September 29, 2010, 05:58:44 am
Ah thanks :)
Yeah I was going to implement them as functions then convert to macros when the parser can detect them but I just realised that this works:
Quote
void action_sprite_set(double spritep,double subimage, double speed);
#define action_sprite_set(spritep, subimage, speed) sprite_index=(spritep); image_index=(subimage);
So i can implement them that way for now :)

Although multi line macros don't work, when writing out the code to the event it includes the \ like so:
Quote
variant enigma::OBJ_obj_0::myevent_create()
{
  sprite_index =(spr_1);
  \ image_index =(0);
  ;
 
  return 0;
}
Title: Re: Instance Interface How-to
Post by: Josh @ Dreamland on September 29, 2010, 07:03:47 am
Hm. Does that cause a compile error? It shouldn't. Either way, I'll take care of it.