GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2010-07-19 03:42:30

mollecular_gems
Member
Registered: 2010-07-19
Posts: 1

How do I make my own functions?

I need more help with my rpg. How do i make functions? i need to include a function:attack() (is that lua) for the obj_charec . I would also like a function:level_up() for the player. How do I make my own functions.
-MAx

Offline

#2 2010-07-19 15:45:04

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: How do I make my own functions?

A function is called a "script" in Game Maker. They are general purpose with global scope. Because Game Maker lacks methods and namespaces one must be careful to avoid naming conflicts.

More information about scripts can be found here:
http://www.gmlscripts.com/gml/scripts

There is a mechanism similar to object methods called "user defined events". While these are specific to objects they do not take arguments and they do not return a value. You are also limited to creating only 16 of them per object.


Abusing forum power since 1986.

Offline

#3 2010-07-19 23:13:19

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: How do I make my own functions?

I prefer to use script_execute, as oposed to user events. more flexible and you dont loose yourself trying to figure what event (0,1,...) your code related to attack is...

define your scripts

script add2values, takes 2 arguments,
return argument0 + argument1;

script mult2values takes 2 agruments
return argument0 * argument1;

Object0, Parent

Object1, parent is object0, create event
MathScript = add2values;

Object2, parent is object0 create event
MathScript = mult2values;

on object0 collision with object0
script_execute(MathScript, 5,10);
with(other) script_execute(MathScript, 5,10);

note that within the script (function), the collision event especialy, you can use other, to access instance variables of the 2 objects involved even when the script is called via script execute.

script_execute(MathScript, 5,10); //other in script would be instance id this <self> collided with
with(other) script_execute(MathScript, 5,10); //other in script would be instance whose collision event is being executed

DO NOT confuse script execute and execute string... it's <almost> as fast to call a script via script_execute as it would be to call it directly...

Last edited by icuurd12b42 (2010-07-19 23:17:33)

Offline

#4 2010-07-20 02:22:10

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: How do I make my own functions?

I create variables to keep track of my event numbers (eg. ev_attack = 0, ev_dodge = 1, etc), but until recently I've rarely used user defined events for anything.

I never understood the value of script_execute except for being able to pass scripts to other scripts, but I can see now how your example could be useful for object oriented tasks.


Abusing forum power since 1986.

Offline

#5 2010-07-20 19:20:18

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: How do I make my own functions?

You should take a look at my command stack tutorial on the gmc... script execute is the key to dynamic and versatile AI; code and behaviour switching on the fly is neet.


I used to use event_user() a lot... the pain about it is the actual UI, not the event number in code... I had one setup where 0 would be apply damage, 1 would be show effect, 2...  up to 10... at one point I could not figure out which event, say the apply damage was... FInding myself opening each code box try to locate the deffective code. Surely I should have decalred ev_damage = 0 and so forth somewhere. I least I would have found my stuff more easy.

Last edited by icuurd12b42 (2010-07-20 19:26:03)

Offline

#6 2010-07-21 11:01:05

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: How do I make my own functions?

Yeah, tracking down the correct code block is always a pain with user events. I use comment actions to help with that, but you still have to click through a lot of events to find what you are looking for sometimes.

I'll check out your command stack, that's the sort of application I could envision using script_execute for. It might be useful for a project I'm working on but I have a feeling it would be overkill for an old-school vertical-scrolling shooter.


Abusing forum power since 1986.

Offline

#7 2010-07-21 19:20:05

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: How do I make my own functions?

Yeah, I use command stack for Desktop Death Match, a sister game to The Tank Game. really, the tank game spawned this command stack. Odly enough, I also use it in my Visual Music application, to define visualisation schemes and a space game I'm working on... Where space ships switch from mining asteroids, bringing in resources to the space sation and fighting with enemie. It 's a really cool system.

Offline

Board footer

Powered by FluxBB