I want to add a button that lets me re-load any room's room_Load(); script.
If I simply tell the globalscript to do the function room_Load(); whenever I press a certain button, it does not recognize the function, since the function room_Load(); is in the local room script, not the global script.
I tried this in the global script to work around it:
function roomupdate();
{
room_Load();
}
but it doesn't work.
The only way to do that, afaik:
// room script, below room_Load
function on_call(int p) {
if (p == 1) room_Load();
}
// global script
CallRoomScript(1);
Note that the on_call function part has to be added to every room script.
An alternative way would be to put all the room_Load stuff inside on_event / eEventBeforeFadeIn / player.Room == x. Then call on_event(eEventBeforeFadeIn, player.Room); in on_key_press().
Quote from: Khris on Sat 06/04/2013 13:11:28
Note that the on_call function part has to be added to every room script.
I don't think it has to be, the CallRoomScript only does something if "on_call" function is present in the room, otherwise it does nothing (meaning, no errors should occur).
LATER CORRECTION: I meant to say, you do not have to put it to the rooms, that should not be affected but this command (button click). Of course, you still has to put it in every room that has to be.
I would also recommend to not use plain literal (1), but make some defines or enumerate values, so that you could easily use same value for every similar situation; like put this in GlobalScript.ash:
#define ROOM_SCRIPT_ON_LOAD 1
Then use CallRoomScript(ROOM_SCRIPT_ON_LOAD).
Yeah, I did mean every room that needs the functionality. Since the OP said "any room", I assumed it needed to go in all.
Thank you very much for your replies. I'm on to make it work.
I wonder, can I simplify the script a bit? or would that mess it up? What if I did this:
// room script, below room_Load
function on_call(1)
{
room_Load();
}
// global script
function bCamp_OnClick(GUIControl *control, MouseButton button)
{
TIME += 15000;
Display("After half an hours rest, you feel ready to move on.");
//thingamagig about restoring health values, not coded yet
CallRoomScript(1);
}
where bCamp is my "rest" button and TIME is my rep-exec timer keeping track of game time.
Forgive me if I ask a stupid question here, but shouldn't there be a couple more brackets, like this:
// room script, below room_Load
function on_call(int p) {
if (p == 1)
{room_Load();
}
}
// global script
CallRoomScript(1);
???
First of all, you can get rid of the parameter check if you aren't going to use CallRoomScript() for anything else:
function on_call(int p) {
room_Load();
}
You can't just put 1 in the brackets though, AGS calls the on_call function using the parameter supplied in the CallRoomScript(1) call, and expects a function with an int parameter.
When you define a function's parameters, you need to state data type (here: int) and name (here: p, but can be named whatever you want). Actual values can only be put in function calls.
(This can be confusing because we're writing a function that is called by AGS, while we usually call functions provided by AGS.)
As for the curly brackets, they are only needed if more than one command is supposed to be executed depending on the if check.
If it's only one line after an if clause, you may omit the brackets.
It's basically a matter of personal preference.
Some coding rules require to have the brackets in any case since omitting them can cause an error if the unwary programmer adds a line in the if clause body and forgets to add the brackets.
Around here the brackets are often left out for one-liners, although I like to use them for added code robustness.
Edit: Too slow. I'm out. :P
Tested and working. Thank you very much.