Author [EN] [PL] [ES] [PT] [IT] [DE] [FR] [NL] [TR] [SR] [AR] [RU] [ID] Topic: Random useless useful code  (Read 2848 times)

0 Members and 1 Guest are viewing this topic.

Offline TheouAegis

  • Amateur Auteur of GMvania
  • Master Hunter
  • *****
  • Posts: 1860
  • Gender: Male
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. The Great Defender will always defend the object of his or her fandom. Hack Master makes creations out of CV parts. (S)he makes Dr. Frankenstein proud.
    • GMvania Developer's Blog
    • Awards
  • Likes:
Random useless useful code
« on: June 29, 2013, 08:09:24 PM »
+1
Maybe I'll just post all my randomly useless useful codes in one thread...

So here's my newest one. It's useful for small numbers, not so much for larger numbers as it will get exponentially slower. What it does is generate an 4-digit number containing only zeroes and ones. No, it's not a binary number; it's just a number that contains only zeroes and ones. There are much faster methods to do this. MUCH faster. For example, you could randomly generate four booleans and piece them together; but that would be a useful code, and this thread is called "Random useless useful code", so such a useful code wouldn't belong here.

Code: [Select]
var num, arg, clone;
num=0
while true
{
    num = irandom_range(1,1111);
    clone = num;
    for (arg=0;arg<4;arg+=1)
    {
        clone = clone div 1;
        if clone & 1
            clone ^= 1;
        clone /= 10;
        if clone mod 1 > 0
            break;
    }
    if arg==4
        break;
}
return num;

(I typed this up for some guy that would never use it. And for myself. Really, just for myself. I wanted to challenge my logic skills for the night.)
Your mom has had more floppies put in her than a Commodore 64!


Follow my lack of progress on my game at my blog:
http://gmvania.blogspot.com

Offline TheCruelAngel

  • Sportin' Boxes since 1998
  • Legendary Hunter
  • ****
  • Posts: 516
  • Gender: Male
  • Awards SuperOld Dungeonite: Members who have been around since the oldOLD days.
    • Awards
  • Favorite Game: Super Castlevania IV (SNES)
  • Likes:
Re: Random useless useful code
« Reply #1 on: July 01, 2013, 02:58:25 PM »
0
This is a cool thread idea! :) When I come across useless useful code during my after hours coding projects (on hours is company owned, of course) I'll be sure to post them up too!

Though, for those not familiar with certain languages, it might be helpful to label it. Looking at what you've posted, my best guess is a scripting language like Perl, which I haven't touched in years:P

Offline TheouAegis

  • Amateur Auteur of GMvania
  • Master Hunter
  • *****
  • Posts: 1860
  • Gender: Male
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. The Great Defender will always defend the object of his or her fandom. Hack Master makes creations out of CV parts. (S)he makes Dr. Frankenstein proud.
    • GMvania Developer's Blog
    • Awards
  • Likes:
Re: Random useless useful code
« Reply #2 on: July 01, 2013, 08:54:03 PM »
0
GML. Based on various languages, like C and Perl and I think I've even heard Pascal floated around. It's a pretty easy language to read without much coding knowledge. I can understand it better than any C++ I read on the net.

This was the semi-useful code I was talking about that randomly picked a bunch of zeroes and ones and made them into a real number:

Code: [Select]
//argument0: number of digits in number (don't go too high!)
var digits;
digits = argument0;
var result, num, a;
result = 0;
for (a=0;a<digits;a+=1)
{
    num[a] = irandom(1);
    repeat a
        num[a] *= 10;
    result += num[a];
}
return result;

For those not familiar with GML, an argument is the value passed in the function call, like myScript(4) would generate a 4-digit fake binary number.
« Last Edit: July 01, 2013, 08:57:34 PM by TheouAegis »
Your mom has had more floppies put in her than a Commodore 64!


Follow my lack of progress on my game at my blog:
http://gmvania.blogspot.com

Offline TheCruelAngel

  • Sportin' Boxes since 1998
  • Legendary Hunter
  • ****
  • Posts: 516
  • Gender: Male
  • Awards SuperOld Dungeonite: Members who have been around since the oldOLD days.
    • Awards
  • Favorite Game: Super Castlevania IV (SNES)
  • Likes:
Re: Random useless useful code
« Reply #3 on: July 05, 2013, 04:32:45 PM »
0
I'm guessing GML doesn't support increment/decrement (++/--)? I don't know why, but I'm not a huge fan of non-hard typed languages. I need my int/bool/char defined for my variables! :p

And for the example, why didn't you just have the method name such that (also rewriting in C++ because it's good exercise. :)):

Code: [Select]
public uint RandomNumberGen(int digits)
{
  if(digits <= 0)
  {
    return 0;
  }
  else if(digits > 10)
  {
    throw(/*some error*/);
  }

  uint result = (digits > 1)?(rand() * POW(10, digits - 1)) : (rand() * 9);
 
  return result;
}

Also I didn't really check anything to make sure the code works, I just kind of whipped it up. So, hopefully the logic works...granted you'll need the std library and cmath to do anything...but I digress!

Offline TheouAegis

  • Amateur Auteur of GMvania
  • Master Hunter
  • *****
  • Posts: 1860
  • Gender: Male
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. The Great Defender will always defend the object of his or her fandom. Hack Master makes creations out of CV parts. (S)he makes Dr. Frankenstein proud.
    • GMvania Developer's Blog
    • Awards
  • Likes:
Re: Random useless useful code
« Reply #4 on: July 05, 2013, 05:22:46 PM »
0
Quote
result = (digits > 1)?(rand() * POW(10, digits - 1)) : (rand() * 9);

I don't think GM allows that. You can do lots of inline stuff, but not that I don't think.

You can do ++/-- in the newest version of GM, but I don't have it because many of the changes suck (their programmer logic is illogical: platform X can't handle this code, so we will remove it for all platforms). It irritates a LOT of people that you can't declare variable types. They're supposedly going to implement it in the next version. I really don't give a shit about trying to make GM noob-friendly. It's hardly noob-friendly as it is. You must learn how to code in GM in order to make decent games in it, and even if you just rely on drag'n'drop you still need to learn how to work within the constraints of the program. So I see no reason to cripple the program because certain aspects would not be useful to a complete newb. If they want to charge $100 for it, they should make it programmer-friendly, not idiot-friendly. Fuck the idiots. They can use RPG Maker.

Oh, there is a power() function in GM, but I was thinking when you convert it all into code, a repeat(x) call would be just about as fast as a power() call since all a power(x,y) does I would expect is multiply x times itself y-1 times in the same way as repeat(y)a*x.
Your mom has had more floppies put in her than a Commodore 64!


Follow my lack of progress on my game at my blog:
http://gmvania.blogspot.com

Offline TheouAegis

  • Amateur Auteur of GMvania
  • Master Hunter
  • *****
  • Posts: 1860
  • Gender: Male
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. The Great Defender will always defend the object of his or her fandom. Hack Master makes creations out of CV parts. (S)he makes Dr. Frankenstein proud.
    • GMvania Developer's Blog
    • Awards
  • Likes:
Re: Random useless useful code
« Reply #5 on: July 11, 2013, 09:07:20 PM »
0
I was trying to create a sprite flicker that started slow then got faster using a simple algorithm.

I failed.

In my giving up, I settled for this: a flicker kinda like a modern alarm clock (bee-bee-bee-beep...bee-bee-bee-beep...bee-bee-bee-beep). I'm not proud of it, but it was cool enough.

Code: [Select]
if timer
{
timer += 1;
if timer == $C0
    instance_destroy()
else
visible = !(timer>>3+2*visible & $F7 & 1)
}
Your mom has had more floppies put in her than a Commodore 64!


Follow my lack of progress on my game at my blog:
http://gmvania.blogspot.com

Offline Inccubus

  • Wannabe Great Old One
  • Master Hunter
  • *****
  • Posts: 3265
  • Gender: Male
  • Warrior
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. SuperOld Dungeonite: Members who have been around since the oldOLD days. Permanent Resident: Seems to always be around to post/reply.
    • Awards
  • Favorite Game: Vampire Killer (MSX)
  • Likes:
Re: Random useless useful code
« Reply #6 on: July 12, 2013, 07:45:33 AM »
0
You can do ++/-- in the newest version of GM, ...

Nah, it's had that for a pretty long time IIRC. GML is basically C++, more or less.
"Stuff and things."

Offline Dracula9

  • That One Guy
  • Master Hunter
  • *****
  • Posts: 2417
  • Gender: Male
  • Blargh
  • Awards 2015-01-Music Contest Gold Prize 2014-12-Music Contest Gold Prize 2014-11-November FinalBoss Sprite Contest 2nd Place Winner A great musician and composer of various melodies both original and game-based. 2018-06 Sprite Contest First Place
    • Awards
  • Favorite Game: Super Castlevania IV (SNES)
  • Likes:
Re: Random useless useful code
« Reply #7 on: July 12, 2013, 10:23:44 PM »
0
I was trying to create a sprite flicker that started slow then got faster using a simple algorithm.

I failed.

In my giving up, I settled for this: a flicker kinda like a modern alarm clock (bee-bee-bee-beep...bee-bee-bee-beep...bee-bee-bee-beep). I'm not proud of it, but it was cool enough.


Couldn't you just do that by having a timer variable control image_alpha?

Like,
(click to show/hide)

I mean, I know you're way better and more advanced in GML than I am, but it seems to me that you're making this more complicated than it ought to be. I could be wrong, though, since you work with it a very specific way for this project of yours.


Trøllabundin eri eg, inn í hjartarót.

Offline TheouAegis

  • Amateur Auteur of GMvania
  • Master Hunter
  • *****
  • Posts: 1860
  • Gender: Male
  • Awards The Retro Gamer: Has a heated passion for the oldschool VG Titles. The Great Defender will always defend the object of his or her fandom. Hack Master makes creations out of CV parts. (S)he makes Dr. Frankenstein proud.
    • GMvania Developer's Blog
    • Awards
  • Likes:
Re: Random useless useful code
« Reply #8 on: July 13, 2013, 10:09:57 AM »
+1
Why write out a data structure when you could have an algorithm that did the same thing?  Besides, it's all about the challenge. Most GM users don't challenge themselves and waste so much memory with workarounds like that. Also you would have to time that whole thing out. With an algorithm, you could just guesstimate and be satisfied with the results.

The sprite flicker thingy here was for someone else (he ended up not using it). He was making a door that just disappeared when all the enemies were defeated. I was like, "why do that when you could make it flicker out?" People see a whole slew of code and they think, "Oh my god that's too much code, I'm not using that." But if you can give them a single line of code, even if it makes no sense to them at all, they'll love ya for it. In the case of a flicker that speeds up over time, I was being stupid -- there is an easy way and I have seen it in code, but it can't be done with one variable and that was what hindered my creative process.

My latest code I worked on this week because I was too tired to stare at NES code is a seamless world wrap script. All snug in a single block of code (well 2 if you want to let the player be spawned somewhere other than the top-left corner of the room). I won't post the code here, just the demo and GMK. It's actually useful, unlike my other codes.

https://www.dropbox.com/s/zpeqyxfso4vezxw/TileScrollv2.zip


@Inccubus: ++/-- was only in GM since Studio. Okay maybe 8.1, but it wasn't in GM8 or earlier. You always had to do +=1. I think the ++/-- is so the compiler can make the game run faster.
« Last Edit: July 13, 2013, 10:24:04 AM by TheouAegis »
Your mom has had more floppies put in her than a Commodore 64!


Follow my lack of progress on my game at my blog:
http://gmvania.blogspot.com

Tags: