• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Game Mechanics [SOLVED] Letting players add custom behavior to objects

Mornedil

Member
I've had an idea for a while but not sure the best way to do it, so I thought I'd ask what everyone here on the forums think.

Players will be able to add their own behaviors to objects through a simple drag-and-drop system, then use those objects in gameplay. Imagine the drag-and-drop system in GameMaker:Studio, where you can add different actions, and if-then-else blocks as well.
The part about making the drag-and-drop editor is pretty straight forward, what I want to ask about is what you think would be the best way to perform the list of actions during gameplay? What would the easiest way to make it? What would give the best performance? etc, etc.

I have 2 different ideas so far:
1. save all D&D actions to an array, and loop through it during gameplay to perform the actions:
I've tried this by storing values in a 2D array in the following pattern:
[n, 0] = script_index
[n, 1] = first argument
[n, 2] = second argument
etc, etc.
Then I just loop through the array and use script_execute() to execute the script_index with the arguments stored in the array. I've tested it and it works, although I have to add all behaviors as script resources, including scripts that simulate if-statements (if it's true it performs X amount of actions below it, else it skips them). This method would mean a lot of extra work to build from scratch how the system works. I'm also not sure how well this system would perform on a large scale.

2. Generate a string code from the actions, and use an interpreter during gameplay to perform the actions:
This seems pretty straight forward, just taking the player's D&D actions, generate a string from them, and let the interpreter do it's work. Then it's just the question of finding the best interpreter, I've seen a few GML interpreters on the marketplace. So the real question here is performance, as far as I know interpreters are not exactly known to be fast, although I'm not sure how slow they are exatly.

These are the 2 ideas I have right now, maybe there are even better ways. How would you proceed with this?
 
Last edited:
Do the array method. WAY faster. Alternatively, if you want to let the users actually type in real code, you can use a string based parser, but I would again then highly suggest converting that to the array format internally for quicker access.
 

Mornedil

Member
Do the array method. WAY faster.
Thanks! I'll experiment with it a bit more and see what I'll do in the end. I'm not sure how much faster the array method will be once it's fully developed though. Right now I can only store constant values in the array, for example:
Code:
r[0,0] = scr_move_towards
r[0,1] = 320
r[0,2] = 240
r[0,3] = 5
//This lets me simply execute the script with the arguments:
script_execute(r[0,0], r[0,1], r[0,2], r[0,3])
But this is too limited, players should be able to perform calculations and use variables, and I see no way to do this unless I store it as a string and then parse and evaluate it during runtime:
Code:
r[0,0] = scr_move_towards
r[0,1] = "enemy.x"
r[0,2] = "enemy.y - 50"
r[0,3] = "scr_get_distance(x, enemy.x) * 0.1"
But this means I have to parse every argument of every script individually to return the correct values before executing them, which could easily stack up to quite a lot of individual parsing calls every frame.
Wouldn't this also become very slow in the end? This makes me unsure whether it's worth to do the Array system (since it's a lot more work) if it wouldn't be much faster than interpreting one big piece of code instead of multiple short strings.
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
IMO whether something is slow or not doesn't matter as long as it runs fine on a "minimum system limits" computer you have access to (I'm so poor I always use a several years old computer with under-average specs... it's less than 6 months since I stopped using my old Windows XP machine :p). If it starts causing lag issues during test playing, it might be worth investigating, but don't do anything until it causes problems. It's more likely it won't cause problems, so preventing them is just wasting time.
 

Mornedil

Member
Thanks for the input :>. Sounds like a good plan, no need to waste time on something if the easy way turns out to be problem free. So I think I'll start off using an interpreter, which would mean minimal work on this part of the project right now.. and if that turns out to cause lags or slowdowns later on my Samsung Galaxy S2 (which I'd consider pretty outdated, heh), then I'll try to refine it by developing the array system further.
 
  • Like
Reactions: Yal
Top