Charas-Project

  • Home
  • Help
  • Search
  • Calendar
  • Login
  • Register
*
Please login or register.

Login with username, password and session length
 

News:

New forum theme up and running!



  • Charas-Project »
  • Game Creation »
  • Requests »
  • Tutorials »
  • Neat GML tricks
« previous next »
  • Print
Pages: [1]

Author Topic: Neat GML tricks  (Read 4848 times)

Offline fruckert

  • Star-Star-Star-Star
  • Sage
  • *
  • Posts: 8,148
  • Not intended for public consumption
Neat GML tricks
« on: July 27, 2009, 10:21:49 PM »
Over my year of programming in GML I've acquired a few cool tricks.
Well, mainly one that isn't an entire engine or blatantly obvious/talked about quite a bit, but as I find more I'll put them in this thread.

=Creating an instance with variables=
Because this would have been far too long to put in the title, here's a better description.
Say, you want an instance that has little instances following it and it alone.
But, when you put into the follower objects code to follow the parent object, having multiple parents can screw it up.
So, you do something like this:
Code: (Parent Object Create) [Select]
var n; //temporarily declaring the n variable, which is what I normally use for temporary things
n=instance_create(x,y,O_follower);/*here's where the cool stuff happens.
When you create an object, it returns its id, which is unique to ever instance.
You can then set the object that you just created's variables afterwards, kind of like this:*/
n.follow=self.id//you need the self.id part, so that the instance follows the object you just created.
motion_set(random(360),2)//just so the other instance follows.
Code: (Follower Object Step) [Select]
//This is going to be primitive, because I don't want to write an entire system for following something right now.
action_potential_step(follow.x,follow.y,1,0)/*this is an action command.
It's equivalent is the 'Step Avoiding' tile in the Move tab in item properties.
Also, sorry if it doesn't work, because I've never actually used it.*/
There is a function for potential step, but I just don't remember it at the moment.

=Action Commands=
I just recently found these out.
They're commands that correlate to exact tiles, for some reason. I say some reason, because you can make a lot better code without them.
Here is a complete list of all of them, around the bottom of the page.
I personally don't use them, but they might help in learning syntax and stuff.

=Alarmless Timers=
These are pretty common, but whatever.
Basically it's a variable that goes up with each step, and when it reaches a certain number, the "alarm" triggers.
Code: (Step Event) [Select]
//Remember to set up the variables in the create event, alright?
if timergo=1 then timer+=1//timergo is just a variable that'll go false when the timer reaches whatever.
if timer=90
{
      //whatever code you need
     timergo=0//this isn't needed, neither is the variable itself, you can make the timer recursive, so it'll go on forever.
     timer=0//this resets the timer, it is needed.
}
Why would you want to use this code?
It's a lot more accurate, somehow. I'm not too sure on the technical details.

=Execute String=
This is the thing Ed talked about.
A string is a variable, but it contains words.
It looks like this:
Code: (Whatever) [Select]
n="This is a string. Note the commas."
It can contain some code.
In an id creation event (right click on an object in a room, it'll detail it's creation event), you can set the variable to a code.
Code: (ID creation event) [Select]
n="instance_create(self.object_index)
Code: (Object creation event) [Select]
execute_string(n)
Note: Don't use the code I just gave you. It's useless. And, used correctly, can be the equivalent of a forkbomb.
This can be very helpful, however.
How?
It can be used to make a chest in an RPG spawn different items, making it so you only need one object.
That's how the chests in Sparkengine work.
You can also use execute_file(), and write a file (if you're using a text file, use the full name, as in code.txt), but I personally don't recommend this due to the ease of people rewriting it.

=Isometric Depth-the Cheap way=
Code: (Step Event) [Select]
//Yeah, I know, this is ridiculously easy code to pull off, but it can be pretty cool.
depth=-y

=Switches=
Switches are little blocks of code that trigger when certain variables are at a certain value.
They are very useful when you don't want to have lines and lines of if codes.
I'm having a bit of trouble figuring out a useful example, so here's how to call them.
Code: (Wherever, I personally use them in the Step event) [Select]
switch(variable here)//I most often use them for keyboard_check stuff
{
     case condition: /*the colon is needed, and in the keyboard_check example, you can do stuff like
ord('1') or vk_up...stuff like that*/
     action; //self explanatory
     break; //to make sure your code doesn't loop forever, this can be removed but I personally don't do that
     //repeat as necessary
}

=Music Code=
This is some code I use for music.
It's very simple, and works nicely, if a tad jarring.
Code: (Step Event) [Select]
if !variable_global_exists("BGMp") variable_global_set("BGMp",0)//in case the variable doesn't exist, so you can just copy/paste this into your game :P
if !variable_global_exists("BGM") variable_global_set("BGM",/*default music*/)//same here
if !sound_isplaying(global.BGM)
{
     sound_stop(global.BGMp)
     sound_loop(global.BGM)
     global.BGMp=global.BGM
}
This way, for say, a room creation event (room settings it be located), you can just change global.BGM to another song, and it'll automatically change.


Very quickly, I apologize for my wording, if you don't understand it, just ask and I'll elaborate.
Once I remember/find out some more neat little doohickey's, I'll post em up on this thread.

EDIT: Added Isometric Depth code.
« Last Edit: August 01, 2009, 04:32:54 AM by fruckert »
Logged
Quote
Ellie: I had a slice of ham in my hand. I was going to drop it, so I slapped it hard. It attached itself to the wall

Offline X_marks_the_ed

  • trygtt o sizg msw kisg
  • Royal
  • *
  • Posts: 4,394
  • WHAT THE WHY ARE THESE BUTTONS
Re: Neat GML tricks
« Reply #1 on: July 27, 2009, 10:26:03 PM »
Did you ever get to fiddling around with execute_string?

You can do some neat **** with that.
Logged

Offline fruckert

  • Star-Star-Star-Star
  • Sage
  • *
  • Posts: 8,148
  • Not intended for public consumption
Re: Neat GML tricks
« Reply #2 on: July 27, 2009, 10:26:34 PM »
Oh yes, I made an entire chest engine for Patchwork with that.
Logged
Quote
Ellie: I had a slice of ham in my hand. I was going to drop it, so I slapped it hard. It attached itself to the wall

Offline fruckert

  • Star-Star-Star-Star
  • Sage
  • *
  • Posts: 8,148
  • Not intended for public consumption
Re: Neat GML tricks
« Reply #3 on: July 28, 2009, 01:11:32 AM »
Just added two more.
Logged
Quote
Ellie: I had a slice of ham in my hand. I was going to drop it, so I slapped it hard. It attached itself to the wall

Offline X_marks_the_ed

  • trygtt o sizg msw kisg
  • Royal
  • *
  • Posts: 4,394
  • WHAT THE WHY ARE THESE BUTTONS
Re: Neat GML tricks
« Reply #4 on: July 30, 2009, 01:21:05 AM »
Quote
You can also use execute_file(), and write a file (if you're using a text file, use the full name, as in code.txt), but I personally don't recommend this due to the ease of people rewriting it.

+5 respect for thinking ahead.

Execute string can also be useful if you have many variables that are similar, as in Bout!, by combining it with the 'string()' command. As in, you have the variables Ed_health, Fruckert_health, Charas_health, etc. Instead of...

Code: [Select]
if global.p1character = 'Ed' {Ed_health -= 20}
if global.p1character = 'Fruckert' {Fruckert_health -= 20}
if global.p1character = 'Charas' {Charas_health -= 20}
etc...

You can just have...

Code: [Select]
execute_string( string(global.p1character) + '_health -= 20')
As long as you keep the character name values the same as the health variable names.



...and I might as well mention the ever-useful for and while loops.
« Last Edit: July 30, 2009, 01:32:13 AM by X_marks_the_ed »
Logged

Offline fruckert

  • Star-Star-Star-Star
  • Sage
  • *
  • Posts: 8,148
  • Not intended for public consumption
Re: Neat GML tricks
« Reply #5 on: July 30, 2009, 01:49:37 PM »
Quote from: X_marks_the_ed on July 30, 2009, 01:21:05 AM
...and I might as well mention the ever-useful for and while loops.
Yeah...I kinda suck and I still haven't figured out how to do for loops.
Every time I set one up, it goes on forever.

Another useful thing would be switch's.
*goes and rewrites first post*
Logged
Quote
Ellie: I had a slice of ham in my hand. I was going to drop it, so I slapped it hard. It attached itself to the wall

Offline fruckert

  • Star-Star-Star-Star
  • Sage
  • *
  • Posts: 8,148
  • Not intended for public consumption
Re: Neat GML tricks
« Reply #6 on: August 01, 2009, 04:33:11 AM »
Added music code.
Logged
Quote
Ellie: I had a slice of ham in my hand. I was going to drop it, so I slapped it hard. It attached itself to the wall

  • Print
Pages: [1]
« previous next »
  • Charas-Project »
  • Game Creation »
  • Requests »
  • Tutorials »
  • Neat GML tricks
 

  • SMF 2.0.10 | SMF © 2015, Simple Machines
  • XHTML
  • 2O11
  • RSS
  • WAP2
  • Simple Machines Forum