Jump to content


Photo
* * * * * 5 votes

Smart Codes


  • Please log in to reply
364 replies to this topic

#341 NukeTheCat

NukeTheCat

    GMC Member

  • GMC Member
  • 461 posts
  • Version:GM8.1

Posted 24 June 2012 - 01:47 PM

48 hour bump! :chikin

Create and scans a .ini highscore nearly instantly!
//should be defined in the first object created's Create Event
ini_open('custom_highscore.ini');
var addnew;
addnew = 11;
repeat(10)
{
addnew -= 1;
if !ini_key_exists('Names',string(addnew))
{
ini_write_string('Names',string(addnew),'<UNDEFINED>');
}
if !ini_key_exists('Scores',string(addnew))
{
ini_write_real('Scores',string(addnew),1000);
}
name[addnew] = ini_read_string('Names',string(addnew),'<UNDEFINED>');
score[addnew] = ini_read_real('Scores',string(addnew),1000))
};
ini_close();

I think it might work with a for loop, but it's better to be safer and stick to repeat.
Used it before, I spurged the above code through memory, so can somebody test it first?

To access the name and the score, you can use the variables:
name[1], name[2], name[3], etc...
score[1], score[2], score[3], etc...

You can test the highscore via:
for(a = 0; a >= 10, a +=1){
if score > score[a]
{
score = score[a];
name[a] = get_string('NEW HIGHSCORE','');
break;
}
};

Edited by NukeTheCat, 24 June 2012 - 01:54 PM.

  • 1

#342 CGPM

CGPM

    GMC Member

  • GMC Member
  • 51 posts
  • Version:GM8

Posted 25 June 2012 - 05:26 PM

On CubinJ's first code:
Instead of using
msec = 29
it could be
msec = room_speed-1
to allow for different room speeds.
  • 1

#343 eyeCube

eyeCube

    GMC Member

  • GMC Member
  • 206 posts

Posted 18 July 2012 - 04:35 AM

I have a contribution of my own for this topic.

I call it convert() but that's probably not a good name for the function.

It turns a real variable with a scale between two real numbers into a scale between two different real numbers.

For example: a variable with limits of 20 and 45. It can't be lower than 20, can't be higher than 45. Now say you want to display this value in a percentage. So you'd name the script convert and use this code:

new_variable=convert(variable,20,45,0,100)

Turns the scale of 20-45 into 0-100.

So a value of 20 returns 0, a value of 45 returns 100. A value of 32.5 returns 50. A value in between 20 and 45 returns the corresponding value between 0 and 100.



The script is:

//name: convert


//use:

//converts variable argument0 with limits argument1 and argument2:
//to limits argument3 and argument4
//example, converting scale of 7-15 to percentage (0-100)
//convert(variable,7,15,0,100)

return (argument0-argument1)*(argument4-argument3)/(argument2-argument1)+argument3

Edited by eyeCube, 18 July 2012 - 03:31 PM.

  • 0

#344 brod

brod

    Brian RODriguez

  • GMC Member
  • 2021 posts
  • Version:GM8

Posted 05 August 2012 - 03:23 PM

smooth linear movement can be optimized to this:
x = startx + mean(-cos(percent*pi), 1)*(endx - startx);
y = starty + mean(-cos(percent*pi), 1)*(endy - starty);

also just a note this isn't linear interpolation, linear interpolation would (by its definition) be
initial + (percent)*(final - initial);
i think it'd be less misleading if you called it a cosine interpolation instead.

Edited by brod, 05 August 2012 - 03:30 PM.

  • 0

#345 thatshelby

thatshelby

    GMC Member

  • GMC Member
  • 3823 posts
  • Version:GM8

Posted 24 October 2012 - 09:22 PM

Game Maker has an undocumented function for linear interpolation

lerp(x1, x2, amount)
  • 0

#346 hugonot

hugonot

    GMC Member

  • GMC Member
  • 19 posts
  • Version:Unknown

Posted 01 December 2012 - 11:54 PM

Thanks so much it's been so helpful!
  • 0

#347 bennikniet

bennikniet

    GMC Member

  • GMC Member
  • 14 posts

Posted 20 February 2013 - 07:02 PM

Snapping a variable to grid:

variable -= variable mod snap;

  • 0

#348 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1019 posts
  • Version:GM8

Posted 20 February 2013 - 07:35 PM

Wow... this is such an old topic. Its worth bumping though, its got some really valuable content! :)
  • 0

#349 deformed thought

deformed thought

    GMC Member

  • GMC Member
  • 104 posts
  • Version:GM7

Posted 23 February 2013 - 12:15 AM

Please consider these suggestions.

Execute code unknown at run-time:
// Set up string variables to store the code you create during the game.  For example, code_step = ''; (in your step event)
// Add code to the string.  You may add code as you generate it, for example in programs that draw to the screen and want a way to undo any line, or by loading it (although it's editable) through a file.

code_step = '';	// Executes code every step.

// An example of how it works:

code_step += 'draw_line(0,0,50,50);'+chr(13)
Append a newline character to any string. This is useful when # isn't acceptable (for example, interpreting the user's input and # cannot mean newline) or when you need to concatenate multiple strings.
Your_String_Name += chr(13);

// How to use choose() for more than 16 options with equal chance to select any option
Choose1 = choose(...);    // The name of these variables doesn't matter
Choose2 = choose(...);    // Replace the ... with the arguments you need (arguments is another word for parameters)
MyFinalChoice = choose(Choose1, Choose2);	// Make sure that Choose1 and Choose2 have the same number of arguments.  You can use this to choose as many arguments as you want.

If you use a variable that only ever has two different values, like a light switch, and the values can be +n and -n, where n = any numeral, this is the fastest way to toggle it:
My_Variable *= -1

Check if something is even or odd.
// Change the number in 'mod 2' to any number to check if it's a factor of and if it's divisible with My_Variable.

if My_Variable mod 2 == 0 {
return 1;
} else {
return 0;
}

  • 0

#350 Yambam

Yambam

    GMC Member

  • GMC Member
  • 355 posts
  • Version:GM8

Posted 15 April 2013 - 06:35 PM

Check if a value is odd: 

odd=variable&1

 

Stay around a instance on a constant distance: 

var vl,vx,vy;
vl=point_distance(x,y,instance.x,instance.y)
vx=(x-instance.x)/max(vl,0.0001)
vy=(y-instance.y)/max(vl,0.0001)


x=(instance.x+vx*distance+x)/2
y=(instance.y+vy*distance+y)/2
 

 

Move the player, for platformers. Works for ramps going up to 45 degrees up or down:

var spd;
spd=-4 //<-- Move 4 pixels left. You can for example replace this with 5 to move 5 pixels right.
repeat(abs(spd))
{
  if !place_free(x,y+1)&&place_free(x+sign(spd),y+1)
  {
    x+=sign(spd)
    y+=1
  }
  else if place_free(x+sign(spd),y)
    x+=sign(spd)
  else if place_free(x+sign(spd),y-1)
  {
    x+=sign(spd)
    y-=1
  }
  else
    break
}

 

EDIT: Fixed bugs in the second and third codes.


Edited by Yambam, 20 April 2013 - 09:06 AM.

  • 1

#351 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1019 posts
  • Version:GM8

Posted 19 April 2013 - 08:06 PM

Limit value

 

Limits x to the constraints of a and b

 

x=max(min(a,b),min(max(a,b),x))

 

I hope it hasn't cropped up before, this is just something I use on a regular basis and I think is useful

 

- Z


  • 0

#352 Yambam

Yambam

    GMC Member

  • GMC Member
  • 355 posts
  • Version:GM8

Posted 20 April 2013 - 09:32 AM

@Zesterer, you can do that using median too: x=median(x,a,b )

 

Another few handy snippets:

Check if point (x,y) is in a circle at the position (cx,cy) with radius cr: sqr(x-cx)+sqr(y-cy)<=cr

Check if the mouse is over a rectangle: mouse_x>=x1&&mouse_y>=y1&&mouse_x<x2&&mouse_y<y2

 

/* d3d_array_index(x,y,z,w,l)
 * 
 * Converts a 3D coordinate to a 2D coordinate for a array.
 * (x,y,z) is the 3D coordinate, w is the size of the 3D space
 * on the X axis and l is the size on the Y axis.
 * 
 * Example usage:
 * d3d_array_index(x,y,z,10,10,10)
 * 
 * blocks[index1,index2]="stone"
 * show_message("Set a value in the 3D blocks array to "+blocks[index1,index2]+".")
 * show_message("The 3D coordinates were (10,10,10),#the 2D coordinates are ("+string(index1)+","+string(index2)+")")
 */
var index;
index=3+argument0+(argument1+argument2*argument4)*argument3


index1=index div 32000
index2=index mod 32000
 

Edited by Yambam, 20 April 2013 - 09:32 AM.

  • 1

#353 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1019 posts
  • Version:GM8

Posted 21 April 2013 - 11:26 AM

@Yambam my bad ;)


  • 0

#354 shadow gamer

shadow gamer

    The Cloaked Shadow

  • GMC Member
  • 192 posts
  • Version:GM8

Posted 21 April 2013 - 09:05 PM

Of course, there are other methods, but my method on what I call the 'which' tip works as well.... I will give you the link explaining what it is, here: http://gamemakerhub..../which-tip.html


  • 0

#355 paul23

paul23

    GMC Member

  • Global Moderators
  • 3366 posts
  • Version:GM8

Posted 21 April 2013 - 10:02 PM

WHY would you use a global variable?
  • 0

#356 shadow gamer

shadow gamer

    The Cloaked Shadow

  • GMC Member
  • 192 posts
  • Version:GM8

Posted 21 April 2013 - 10:12 PM

It's more wide scale, and I don't see why else.

 

But then again, maybe you have a grudge against global variables.....


  • 0

#357 chaz13

chaz13

    GMC Member

  • GMC Member
  • 3830 posts
  • Version:Unknown

Posted 21 April 2013 - 10:21 PM

It's more wide scale, and I don't see why else.

 

But then again, maybe you have a grudge against global variables.....

 

It's also liable to cause more problems than it fixes.


  • 0

#358 shadow gamer

shadow gamer

    The Cloaked Shadow

  • GMC Member
  • 192 posts
  • Version:GM8

Posted 21 April 2013 - 10:28 PM

Hm. Maybe not a good smart code then, but it worked for me. I think if you keep it like it is and don't modify it, then it might work. But then again, maybe that's just the type of program I'm working on.


  • 0

#359 GStick

GStick

    All Secrets Exposed

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

Posted 22 April 2013 - 04:49 AM

The reason it's a poor choice for a global variable is because other instances likely won't have the cursor over their sprites, so if you need to use this method on other objects it's not going to work properly without even more globals.


  • 0

#360 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1019 posts
  • Version:GM8

Posted 23 April 2013 - 07:13 PM

Just use a local variable that is updated every frame in the step event by a single process, and then any other code in the object can refer to that variable? Efficient, fast, easy to code, and good practise.

 

- Z


  • 0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users