object_load, anyone?

Posted by flashback on April 22, 2006, 9:36 p.m.

*Please note that this script requires registration and has only been tested on gm6.1

Have you ever wanted to load objects externally, to make your game load faster or to make an easy plugin system? This is the answer. A simple script, and how to make the files necessary.

First off, the script (name it object_load):

//Loads an External Object
//Returns a handle for the new object
//Made by Flashback. Give credit if y'want.
//argument0: location of the object file (in a string)
var object,n,event,sprite,nn,retval;
var eventcode,eventlines,eventnum;
n=0
nn=0
object = file_text_open_read(argument0)
while(file_text_eof(object)==0) {
event[n] = file_text_read_string(object)
file_text_readln(object)
eventnum[n] = file_text_read_string(object)
file_text_readln(object)
eventlines = file_text_read_real(object)
file_text_readln(object)
eventcode[n] = ""
cont=1
while(cont==1) {
if(file_text_read_string(object)!="--endcode--")
{
eventcode[n] += file_text_read_string(object)
file_text_readln(object)
cont=1
}else{
cont=0
}
}
n+=1
}
file_text_close(object)
retval = object_add()
repeat(n) {
execstring = "event=" + event[nn]
execstring += " object_event_add("
execstring += string(retval)
execstring += ",event,"
execstring += eventnum[nn] + ",'"
execstring += eventcode[nn] + "')"
execute_string(execstring)
nn += 1
}
return retval;

An example of usage would be:

object=object_load("object.obj")

A neat thing about this is that it can load from any txt-like file - name it whatever you want in notepad, or use txt files, even!

The file format:

This is all made by me. Just format your files like this:

Quote:
[event type]

[event number]

[number of lines of code]

[event code]

The event types and the event numbers are available in the manual from The Game Maker Language (GML) -> Game play -> Generating events. That outlines how the number thing affects the event, so read it - no need to re-document it here.

An example object file would be:

ev_create
0
2
show_message("Hello world")
alarm[2] = 60
ev_alarm
2
1
show_message("I'm still here")
ev_destroy
0
1
show_message("Goodbye world")

Have fun!

Comments

melee-master 18 years ago

Heh, nice. If I ever wanted to do this though, I'd just code it on my own. This would be really handy for adding game mod abilities.

chiggerfruit 18 years ago

dude… it's like… sex with numbers and code…

firestormx 18 years ago

Awesomeness. XD

Like melee said, it'd be for game mods and such.

V 18 years ago

really, that looks pretty confusing right there. oh well. i'll be happy to tell you that i have recently been checking out everyones archives, and found this in yours. you need that for your banner!!!

<img src="http://www.64digits.com/users/flashback/matrix.png">

flashback 18 years ago

I used to use that as a banner. In version 1. Now stop snooping there.

Juju 18 years ago

It appears noone has thought about speed.

This will be slower than a sloth in a glue factory when it's actually run, especially if you have lots of the same instance.

flashback 18 years ago

And someone obviously never ran it.

Juju 18 years ago

Someone doesn't need to run it when when someone is speaking from experience. I've run a single execute_string function in one of my destructible terrain engines and it reduced the FPS by 10.