Permission to copy, edit, paste, and use codings thread

Status
Not open for further replies.
My name is Jason and I am new here.


This thread is for people who wish to speed up coding time by copying, editing, pasting, using etc. permissioned to copy, edit, paste, use etc. codings.


Post coding and explain what it or they or both do here.


I shall go first


Create Event:
Code:
increase_hp_1A = 0;
by_this_many_times_1A = 17;
j = 116;
hp = 115;
^Sets variables to chosen values.


Step Event:
Code:
if (keyboard_check_pressed(ord("X")))
{
  x = xstart;
  y = ystart;
}
if (keyboard_check(vk_up))
{
  y -= 2;
}
if (keyboard_check(vk_right))
{
  x += 2;
}
if (keyboard_check(vk_left))
{
  x -= 2;
}
if (keyboard_check(vk_down))
{
  y += 2;
}
if (instance_exists(Healing_Rose))
{
  if (place_meeting(x,y,Healing_Rose))
  {
    if (increase_hp_1A = by_this_many_times_1A)
    {
      j = j;
    }
    else if (increase_hp_1A != by_this_many_times_1A)
    {
      hp += 1;
      increase_hp_1A += 1;
    }
  }
  else if (!place_meeting(x,y,Healing_Rose)
  {
    increase_hp_1A = 0;
  }
}
^ If X button is pressed (pushed) down go to starting x position and starting y position

if up arrow button is being held down go up by two pixels

if right arrow button is being held down go right by two pixels

if left arrow button is being held down go left by two pixels

if down arrow button is being held down go down by two pixels

if one or more Healing_Rose exists in room the if colliding at all with a Healing_Rose
then if variable increase_hp_1A is equal to by_this_many_times_1A then variable
j is equal to j
otherwise if variable increase_hp_1A is not equal to by_this_many_times_1A then
variable hp gets increase by 1 and variable increase_hp_1A gets increased by 1
but if not colliding at all with a Healing_Rose then variable
increase_hp_1A equals to 0
 

Yal

🐧 *penguin noises*
GMC Elder
I really like this script to create arrays in-line. Like, you know, a 'real' programming language where arrays is a first-order-citizen!
Code:
///array(val1,val2,...)
var a, c;
for(c = argument_count-1;c >= 0;c--){
    a[c] = argument[c];
}
return a;
 
R

rui.rosario

Guest
I really like this script to create arrays in-line. Like, you know, a 'real' programming language where arrays is a first-order-citizen!
Code:
///array(val1,val2,...)
var a, c;
for(c = argument_count-1;c >= 0;c--){
    a[c] = argument[c];
}
return a;
You could further expand it to merge arrays passed as references (I don't have GM:S installed, or I would modify it accordingly).

That way when you ran out of the 16 argument you could write something like
Code:
array(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
      array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
      array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
And maybe rename your version to multiArray (since you could add arrays to arrays you could create multi-dimension arrays inline with that script. The previous example would create a two dimensional array with 3 rows and 16 columns, or 3 columns and 16 rows, depending on interpretation)
 

Yal

🐧 *penguin noises*
GMC Elder
You could further expand it to merge arrays passed as references (I don't have GM:S installed, or I would modify it accordingly).
The thing is, I only ever use very small arrays (tuples for things like coordinates in 3D space) or very large arrays (e.g. master data tables for my RPG items), so I don't need a script that concatenates arrays. Big arrays are much handier to edit by other means, and small arrays... is exactly what my script is used for.
 
R

rui.rosario

Guest
I don't need a script that concatenates arrays.
I just posted it as a sort of workaround to something like
Code:
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ..., 99, 100};
In other programming languages.

Not this particular array though, as hard-coding this would be stupid, instead of using a loop to create it.
 

GMWolf

aka fel666
Quick and easy movement
Uses up, down, left & right keys.
Pretty much all of my projects use this one.
Code:
var dx = keyboard_ckeck(vk_right) - keyboard_check(vk_left);
var dy = keyboard_check(vk_down) - keyboard_check(vk_up);
x += dx * my_speed;
y += dy * my_speed;

Fixed speed movement
Uses up, down, left & right keys. esures the speed is always my_speed; (even in diagonals)
Code:
var dx = keyboard_ckeck(vk_right) - keyboard_check(vk_left);
var dy = keyboard_check(vk_down) - keyboard_check(vk_up);
var dl = sqrt(sqr(dx) + sqr(dy));
x += dx * my_speed / dl;
y += dy * my_speed / dl;
Its possible i made a mistake. writing this on the fly
 
S

Snail Man

Guest
I use this smooth rotation script a lot. I modified it from one that I think was on the old GMC at some point, but I can't track down the original for the life of me.
Code:
///rotate_smooth(destination_angle, current_angle, rotation_speed, speed_limit)
var destination_angle, current_angle, angle_difference;
destination_angle = argument0;
current_angle = argument1;
angle_difference= destination_angle-current_angle;

if ((angle_difference >= 180) && (current_angle < 180) )
{
     current_angle += 360;
}
else 
if (angle_difference <= -180)
{
     current_angle -= 360;
     return(current_angle);
}

//Apply rotation speed limit, or none if it's set to -1
if (argument3 == -1)
{
     current_angle += angle_difference / argument2;
}
else
{
     if (angle_difference / argument2 < argument3)
     {
          current_angle += angle_difference / argument2;
     }
     else
     {
          current_angle += argument3 * sign( angle_difference );
     }
}
return(current_angle);
 

Yal

🐧 *penguin noises*
GMC Elder
The topic starter's post is almost a full screen's height, so I think you're entitled to create walls of text at your own discretion.
 
oops, I guess my instinct is rigtht, it belongs someplace else. It apparently exceeds the max message length. Mods, if you see this post, please remove it. Sorry.
 
Last edited:
P

Paolo Mazzon

Guest
This is something that should be a part of Gamemaker, but a simple inline script makes up for it. I use it a bunch; and since its inlined, YYC versions aren't impacted.
Code:
///ternary(if, true, false)
gml_pragma("forceinline")
if (argument[ 0 ])
    return argument[ 1 ];
else
    return argument[ 2 ];
 
Z

Zekka

Guest
This is something that should be a part of Gamemaker, but a simple inline script makes up for it. I use it a bunch; and since its inlined, YYC versions aren't impacted.
Code:
///ternary(if, true, false)
gml_pragma("forceinline")
if (argument[ 0 ])
    return argument[ 1 ];
else
    return argument[ 2 ];
Do we know for sure if it has the same short-circuiting behavior as in C?

For instance, what happens on YYC and non-YYC if you write this?

Code:
/// show_return_0(msg)
show_message(argument0);
return 0;

/// test code
ternary(true, show_return_0("In C, you would see me."), show_return_0("In C, you would not see me."));
My prediction: on both YYC and non-YYC, both functions will be called.
 
L

leonfook29

Guest
Code:
/// show_return_0(msg)
show_message(argument0);
return 0;

/// test code
ternary(true, show_return_0("In C, you would see me."), show_return_0("In C, you would not see me."));
My prediction: on both YYC and non-YYC, both functions will be called.
Strangely, you're right. It shows both even if i didn't return 0 to it. And it shows "In C, you would not see me." first, which is even stranger.
 
It's because both arguments are evaluated before being sent to the script, surely. Oh, wait, now I see what you're talking about with the short circuiting and inline.... but it isn't working?
 
Last edited:
Z

Zekka

Guest
EDIT: It looks like my suspicions were confirmed, unfortunately. It's kind of a pity, since the short circuiting is useful, but I think there's a good justification. Thank you leon for running the test!


Re Paolo -- it's not ordinary for function arguments to be short-circuited, which is why I provided the test case. For instance, in most C-like languages including C, this would evaluate both arguments:

Code:
int ternary(char c, int x, int y) {
  if (c) { return x; } else { return y; }
}
C's ternary operator is short-circuited.

Code:
#define ternary(c, x, y) (c) ? (x) : (y)
But inlining int ternary(char, int, int) in a way that introduced short circuiting would slightly change the meaning of the program if evaluating the arguments had side effects, like in my example.

Short-circuiting would usually change the meaning in Game Maker too, for the same reasons, so I would be surprised if inlining your function introduced short-circuiting. Try my test case to see, then compare with this C test case:

Code:
#include <stdio.h>
int puts_0(char* c) {
  puts(c);
  return 0;
}

int main() {
  return 1 ? puts_0("This will be printed.") : puts_0("This will not be printed.");
}
 
Z

Zekka

Guest
One other note, by the way -- in C and related languages, arguments are often not evaluated in a guaranteed order. I personally think this is confusing, but that means that the following code --

Code:
do_z(do_x(), do_y()
-- can be equivalent to either of these

Code:
var x = do_x();
var y = do_y();
do_z(x, y);
Code:
var y = do_y();
var x = do_x();
do_z(x, y);
It's possible GM consistently evaluates arguments in reverse order (there's a reason to do this, but it's a little bit silly), but it's also possible it evaluates them in arbitrary order. Further testing would be interesting, and I have a feeling that if YYC's optimizer is clever, YYC will behave differently than ordinary Game Maker. (since evaluating the arguments in a different order is a common optimizer trick)
 
P

Paolo Mazzon

Guest
If you use it without YYC, it's not going to be inlined and thus the interpreter will evaluate both values before being processed. However, gml_pragma inlines it for YYC, which would end up coming out to this
Code:
if (true)
    show_whatever
else:
    show_whatever
Obviously it's not going to show else.

EDIT: As for the reverse argument evaluation, it kind of makes sense. If Gamemaker uses a first-in-last-out stack, they could just be pushing the arguments onto the stack, resulting in the last one (y in your example) to be the first evaluated.
 
Last edited by a moderator:
Z

Zekka

Guest
Game Maker, like C, is "call-by-value" meaning that it evaluates all arguments to a function before running the function. Usually when we talk about inlining as an optimization, we assume it won't change the meaning of the code.

There's actually two ways you could "inline" the code, one which does change the behavior and one which doesn't. One is to inline the expressions (this is what you are expecting, and it's usually called "call-by-name") and one is just to inline the body of the function called:

(starting from this)
Code:
ternary(true, show_return_0("In C, you would see me."), show_return_0("In C, you would not see me."));
(way 1 -- the call-by-name version you think the inliner will produce)
Code:
if (true) {
  return show_return_0("In C, you would see me.");
} else {
  return show_return_0("In C, you would not see me.");
}
(way 2 -- the call-by-value version I think the inliner will produce)
Code:
// var arg0 = true; // specialized out, it's a constant with no side effects
// inliner observes this function gets called no matter what in non-inlined version
var arg1 = show_return_0("In C, you would see me.");
// inliner observes this function gets called no matter what in non-inlined version
var arg2 = show_return_0("In C, you would not see me.");
if (true) {
  return arg1;
} else {
  return arg2;
}
This preserves the meaning of the non-inlined program.

E: made fixes to demo code, improved description
 
Last edited by a moderator:
P

Paolo Mazzon

Guest
kk so instead of bickering some more I went and tested your theory and it appears that GM inlining is in fact call by value. (YYC showed both, but in order.)

so ya no short curcuiting
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Ummm... sorry to be "that guy" but this is not really the sort of thing the programming forum is for, sorry. This kind of topic is fun for those that participate, but once it goes past a page or two of posts it's pretty much useless as everything except the first and last post are what people will see... it's also of no help to people who are searching for things. My suggestion would be to make an off-topic post requesting code snippets then compile them all into a tutorial or an extension and then post THAT in the appropriate forum.
 
Status
Not open for further replies.
Top