Define Object in Script Only

I

iam1me

Guest
Hey all. I'm returning to Game Maker after a number of years. Like the new Game Maker Studio 2 UI.

I like to avoid the DnD interface and stick with scripting as much as possible. Unfortunately the manual is very much geared towards DnD. Is there a way to define objects in script only?

Thanks
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
When you create a new project, you can pick "GML project" and that gets rid of DnD entirely. The manual has separate sections for DnD/GML
upload_2019-3-30_14-38-14.png
 
I

iam1me

Guest
When you create a new project, you can pick "GML project" and that gets rid of DnD entirely. The manual has separate sections for DnD/GML
View attachment 23964
I did do that - and I've checked the manual. So far I haven't had any luck finding how to define an object in script only.

You can use the instance_create_XXX functions to create an instance from an object - but it requires an object ID.

I'm pretty sure I could do this in the old version of Game Maker - albeit it wasn't well documented then either.
 
I

iam1me

Guest
The function to create objects dynamically has been removed from newer versions of GameMaker.
Damn :/

Perhaps there is a better approach under GM2? For instance, I was going to start by creating an inventory object/class with the ability to set the capacity, add/remove items, and perhaps some additional features like grouping/splitting items of a particular type. No UI (that can be added later).

Perhaps approach it like a C-style struct and use a field enumeration...
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Damn :/

Perhaps there is a better approach under GM2? For instance, I was going to start by creating an inventory object/class with the ability to set the capacity, add/remove items, and perhaps some additional features like grouping/splitting items of a particular type. No UI (that can be added later).

Perhaps approach it like a C-style struct and use a field enumeration...
In that case, data structures, or syntax extensions in GMEdit
 
T

Taddio

Guest
Can't you simply put the obj_name as an argument in your script?
scr_script(obj_to_spawn)?

But if strictly for an inventory, I would check ds_maps, where you can save keys and values together. Very useful for inveentories, as you could have a key 'Life Potion' for example, and you have the qty of Life Potion as the map value of the 'Life Potion' key.
 
I

iam1me

Guest
Can't you simply put the obj_name as an argument in your script?
scr_script(obj_to_spawn)?
My goal isn't to spawn a pre-defined object, but to define the object itself from scratch. Like defining a class in an OOP Language. I dislike being forced to use their Object UI for doing such a basic task. You used to be able to do this. Now I gotta figure out a different paradigm for modeling my code.

But if strictly for an inventory, I would check ds_maps, where you can save keys and values together. Very useful for inveentories, as you could have a key 'Life Potion' for example, and you have the qty of Life Potion as the map value of the 'Life Potion' key.
An inventory is just an example object. I'm really concerned about how to go about structuring my code such that I can define objects with properties & functions, create instances thereof, etc.

Depending upon the specifics of the inventory, a ds_map might or might not be a good idea. For instance: if you supported multiple entries in the inventory being the same type, that wouldn't work. However, if you assumed all repeat entries are grouped together - then yes.

In this case I'm thinking of creating a point & click adventure engine - so a ds_map would potentially be appropriate.
 
T

Taddio

Guest
Yeah, every key must be unique and hold only one valur in a map, may or may not be what you're looking for.
In your case, since you seem to have a pretty good idea of the way you want to go, but are not 100% set yet, maybe just take a couple hours/a day to do a couple prototypes and tests in a fresh GM session. That may very well spark something up!
You shouldn't have any problem making a point-and-click, it's just going to be the transition to the updated GM that's going to hold you back, in the beggining
 

samspade

Member
My goal isn't to spawn a pre-defined object, but to define the object itself from scratch. Like defining a class in an OOP Language. I dislike being forced to use their Object UI for doing such a basic task. You used to be able to do this. Now I gotta figure out a different paradigm for modeling my code.



An inventory is just an example object. I'm really concerned about how to go about structuring my code such that I can define objects with properties & functions, create instances thereof, etc.

Depending upon the specifics of the inventory, a ds_map might or might not be a good idea. For instance: if you supported multiple entries in the inventory being the same type, that wouldn't work. However, if you assumed all repeat entries are grouped together - then yes.

In this case I'm thinking of creating a point & click adventure engine - so a ds_map would potentially be appropriate.
My suggestion would be to use the engine the way it is meant to be used. Otherwise, find a different engine.

While GMS requires at least one object in the room to really do anything - e.g. update anything - you don't technically need more than that. You could create an entire game with just one object where all other 'objects' are defined inside of that main object. In my opinion, this is a waste of time.

While there are definitely times to create lightweight objects - e.g. your own particle system which will be slower than the built in GM one but faster than an instance based one - and there are many times to create lightweight data objects - 'objects' which hold nothing but data - if you want a traditional object that has both code and logic don't make your own. It's far more work, and it means not only that you can't access most of the benefits of the engine you'll still be forced to use its UI.
 
I

iam1me

Guest
My suggestion would be to use the engine the way it is meant to be used. Otherwise, find a different engine.

While GMS requires at least one object in the room to really do anything - e.g. update anything - you don't technically need more than that. You could create an entire game with just one object where all other 'objects' are defined inside of that main object. In my opinion, this is a waste of time.

While there are definitely times to create lightweight objects - e.g. your own particle system which will be slower than the built in GM one but faster than an instance based one - and there are many times to create lightweight data objects - 'objects' which hold nothing but data - if you want a traditional object that has both code and logic don't make your own. It's far more work, and it means not only that you can't access most of the benefits of the engine you'll still be forced to use its UI.
I still intend to use GM Objects on the graphical side of things, for setting up sprites and rooms and such. That doesn't mean you need to stick everything into your UI objects. To the contrary, it is best to have divisions between your UI code/objects and your internal data structures and algorithms.

As far as "more work" - I completely disagree with you. It may take some upfront work to figure out a good paradigm for modeling your code - but once you've settled on a given paradigm you can code far faster than you can using a UI.
 
I

iam1me

Guest
Using enums + arrays appears to work OK. Although it might be difficult to inherit using this approach.

I defined the following enum for my Inventory struct:
Code:
/// @description The field enumeration of the inventory struct
enum Inventory {
    ///The number of items that can fit into the inventory
    Capacity,
 
    /// The number of items currently in the inventory
    Count,
 
    ///The array of items
    Items,
 
    ///The size of the inventory struct (must be the last field)
    SizeOf
}
inventory_create(capacity) script:
Code:
/// @function inventory_create(capacity)
/// @description Creates a new inventory
/// @param {real} capacity The initial capacity of the inventory

var inv = array_create(Inventory.SizeOf, undefined);
inv[Inventory.Capacity] = argument0;
inv[Inventory.Count] = 0;
inv[Inventory.Items] = array_create(argument0);
return inv;
inventory_insert_at( inventory, index, item_id ) script:
Code:
///@function inventory_insert( inventory, index, item_id )
///@description Inserts the item into the inventory at the specified index. Will error if there is already an item at that index.
///@param {array} inventory The Inventory struct
///@param {real} index The Index to insert the Item at
///@param {real} item_id The Id of the Item to insert

if(argument2 == 0) {
   show_error("item_id is 0", true);                     
}
var items = argument0[Inventory.Items];

if(items[argument1] != 0) {
   show_error("Attempt to insert item over another item", true);
}

items[@argument1] = argument2;
argument0[@Inventory.Count] += 1;
inventory_to_string( inventory ) script:
Code:
///@function inventory_to_string( inventory )
///@description turns the inventory into a comma-delimited list of values
///@param {array} inventory The Inventory struct

var str = ""

if(argument0[Inventory.Capacity] = 0) { return; }

var items = argument0[Inventory.Items];
str += string(items[0]);



for(var i = 1; i < argument0[Inventory.Capacity]; i++) {
   str += "," + string(items[i]);
}

return str;
 
Last edited by a moderator:
Top