Using some of the excellent suggestions found in the topic above (making an RPG system in AGS) I've decided to try and script a simple turn-based battle system. So far so good and I feel like I'm making progress.
I've decided to use a very, very simple derivative of the d20 system, where I use a selection of dice to calculate results. So 2d6 for example, would generate a number between 2 and 12. (2 x 6-sided dice).
To help keep these functions to a minimum, I've created a number of functions in the GlobalScript.asc document of my game. So for example, for a d4 I've scripted:
function roll_d4()
{
roll_value = 0;
d4=Random(3);
d4++;
roll_value = d4;
}
This is working fine and is giving me a random number between 1 and 4. It's generated first as the Global Integer "d4" and then moved into the global integer "roll_value". This because at some points I will want to combine odd dice together (a d4 + a d6 for example) so the overall "roll_value" is consistently called to provide outcomes.
So far so good and I've tested this and it's working fine when I call these functions from a GUI button.
Now we get to my problem. In my current Battle Room, I would like to call these functions from within the room script. The reason is that I "set up" the monster based on a monster_id which is set before the player enters the battle room.
So if monster_id = 1, then a certain monster is loaded (hit points, attack points, character view, attack view, etc). If monster_id is 2, a completely different monster is loaded onto the same character with different variables.
Now, my monster_id 1 has hit points ranging between 2 and 8 (2d4). I've created the function roll_2d4 in my globalscript file and would like to call it from within the room script: function room_Load()
When I try to call roll_2d4 (which isn't defined as a function in this specific room), AGS can't compile and gives me the error "Undefined token 'roll_2d4'."
Now I'll be using roll_2d4 in quite a few different rooms and scenarios (dialogue checks, difficulty checks, etc) so I don't really want to script my whole dice-setup every single time I want to call it somewhere in the game. I thought the GlobalScript was the best place for this, if I wanted to script custom functions that were accessible anywhere. Am I doing something wrong, do I need to import the GlobalScript function somehow into the room script before I can acccess it? Or should I store these dice-rolling functions elsewhere?
Any help you can give would be greatly appreciated, as always. I fear I am getting overly ambitious with this little game, but I'm having a blast trying to come up with a battle system and building a bit of an adventure-rpg hybrid game. So any help people can give would be most welcome.
Thanks again!
Of course you can do that, declare your function in the global script's header like:
import function roll_d4();
This will make it "visible" for all your room scripts, and dialog scripts.
This manual's article explains how to make functions and variables available in other scripts:
https://adventuregamestudio.github.io/ags-manual/ImportingFunctionsAndVariables.html
You may also simplify your function and make it return value instead of using any global variables:
int roll_d4()
{
return Random(3) + 1;
}
Then this may be used simply as:
int roll1 = roll_d4();
int roll2 = roll_d4();
int combined_roll = roll_d4() + roll_d6(); // assuming roll_d6 is done in a similar way
I'd be curious to see your work on this, Hobbes, as I was planning something kinda similar for the next project.
Thank you so much Crimson Wizard, that's working a treat! I've got my virtual dice rolling in different places without any difficulty now. :-)
Newwaveburritos, happy to write it up in the future once I've got it working... it's a really nice trial-by-error mess at the moment though.
Or pass the sides to the function:
int Roll(int sides) {
return Random(sides - 1) + 1;
}
Then
int roll1 = Roll(10) + Roll(6);