Jump to content


Photo
* * * * * 5 votes

Smart Codes


  • Please log in to reply
364 replies to this topic

#361 Yambam

Yambam

    GMC Member

  • GMC Member
  • 354 posts
  • Version:GM8

Posted 24 April 2013 - 11:17 AM

Ever heard of instance_position? To check if the mouse is located over a instance:

if instance_position(mouse_x,mouse_y,all)=instance
  ...

Another code snippet:

//ds_grid_string(grid,spaces)
var grid,spaces,str,val;
grid=argument0
spaces=max(argument1,6)
str=""
for(yy=-1;yy<ds_grid_height(grid);yy+=1)
{
  for(xx=-1;xx<ds_grid_width(grid);xx+=1)
  {
    if xx=-1&&yy=-1
      val=spaces_right("|GRID",spaces-1)+"|"
    else if xx=-1
      val=spaces_right("|"+string(yy),spaces-1)+"|"
    else if yy=-1
      val=spaces_right("|"+string(xx),spaces-1)+"|"
    else
    {
      val=ds_grid_get(grid,xx,yy)
      if is_real(val)
        val=string(val)
    }
    str+=spaces_right(val,spaces)
  }
  str+=nl
}

Edited by Yambam, 24 April 2013 - 01:33 PM.

  • 0

#362 csanyk

csanyk

    GMC Member

  • GMC Member
  • 128 posts
  • Version:Unknown

Posted 26 April 2013 - 09:30 PM

6. 360 degree turning
image_single=facing/10
facing = point_direction(x,y,mouse_x,mouse_y)

 

Shouldn't that be:

 

6. 360 degree turning

image_angle=facing/10
facing = point_direction(x,y,mouse_x,mouse_y)


  • 0

#363 Yambam

Yambam

    GMC Member

  • GMC Member
  • 354 posts
  • Version:GM8

Posted 27 April 2013 - 11:02 AM

@csanyk Actually, it is image_single (like image_index but stops the animation when changed). The idea of that snippet is to have a sprite with 36 (360/10) images, all representing a different angle (0, 10, 20, 30, ..., 360).

 

That actually made me think of a new smart code:

//Code for a sprite where every group of N amount of subimages represent the animations of the different angles.
var facing,N,firstimg,lastimg;
facing=direction                                   //<-- In what direction to face?
N=4                                                //<-- How many subimages per angle?
firstimg=floor(floor(facing*image_number/360)/N)*N //<-- First subimage. Don't change.
lastimg=firstimage+N                               //<-- Last subimage. Don't change.

image_index=max(firstimg,image_index mod lastimg)

Edited by Yambam, 27 April 2013 - 11:03 AM.

  • 1

#364 brod

brod

    Brian RODriguez

  • GMC Member
  • 2021 posts
  • Version:GM8

Posted 28 April 2013 - 07:05 AM

Don't know if it's been mentioned yet, but

 

current_weapon = ((current_weapon-1 mod weapon_num) + weapon_num) mod weapon_num;

 

can be improved to:

 

current_weapon = (current_weapon + weapon_num - 1) mod weapon_num;


  • 1

#365 MetroidMan347

MetroidMan347

    GMC Member

  • GMC Member
  • 823 posts
  • Version:GM:Studio

Posted 13 May 2013 - 09:46 PM

I've recently needed to identify multiple instances of the same object a lot. Here's what I do.

///Whenever you create the object
obj = instance_create(0,0,obj_player);
obj.uniqueID = instance_number(obj_player);
///obj_player Destroy Event
var tempID;
tempID = uniqueID;
with (obj_player)
    {
    if (uniqueID > tempID)
        {
        uniqueID -= 1;
        }
    }
///Accessing a specific instance
var tempID = 0; //This can be whichever one you want to find
with (obj_player)
    {
    if (uniqueID == tempID)
        {
        //Do stuff
        }
    }

A rundown:

First of all when the object is created it is given an ID based on how many of that particular object in the room (you can use a codeless parent if you want to find one of many different objects but that's beside the point) then if one of them gets destroyed if had a higher ID it'll go down one. This means that the next one created will be given the correct value.

 

This is what I'm doing in an online game I'm making to make sure the packets are sent to the right client.

Hope this'll be helpful!

 

DFTBA!

Laterz,

M


Edited by MetroidMan347, 14 May 2013 - 11:44 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users