Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Rocco on Fri 08/08/2008 12:21:14

Title: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 12:21:14
I have a struct, for a certain game task.
One functionality should be, that you can start a timer to initate an action periodicly.

But to see if the timer is expired, i need to check in the rep_execute, and when the timer is off i need to call the memberfunction.
Unfortunatly i can't call a memberfunction like this
this.SomeAction();
in the rep_execute

How can i solve this problem?




Title: Re: Call Memberfunction in rep_execute?
Post by: Mazoliin on Fri 08/08/2008 12:36:52
This might, or might not work.

When you sheck if the timer is expired, aslo check if a bool is false, if it is, call the function and then set the bool to true.
Title: Re: Call Memberfunction in rep_execute?
Post by: Khris on Fri 08/08/2008 12:37:30
Either
a) export/import the struct in the module's header

or (way better)
b) put a repeatedly_execute inside the module script.
If AGS finds one, it gets called.

This way you won't have to add stuff to the global script, nor must the user of the module.

The list of all functions is here (http://www.adventuregamestudio.co.uk/manual/ScriptModules.htm).
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 12:47:40
thx, i have the rep_execute allready in the module script,
but it seems in the rep_execute (no matter if in the global or modul) AGS don't accepts the this-pointer.

but i cant see another way around, except  i generate an object in advance, and work with this object,
no matter if its needed in the game or not.
Title: Re: Call Memberfunction in rep_execute?
Post by: SSH on Fri 08/08/2008 12:51:10
But how should AGS know what "this" refers to?

Try this:


struct my_struct {
  import function do_stuff();
}

my_struct inst_name;

function repeatedly_execute() {
  inst_name.do_stuff();
}

Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 12:54:30
Quote from: Mazoliin on Fri 08/08/2008 12:36:52
This might, or might not work.

When you sheck if the timer is expired, aslo check if a bool is false, if it is, call the function and then set the bool to true.

this is not the problem, the problem is i cant call the function within the rep_execute.
the only way memberfunctions can called is within other memberfunctions.
Or with an specified object, but i dont know this object at this time, cause it can be generated by the developer somewhere in the game.
So the developer can write somewhere in the game.

GameTimeCalender myCalender;
myCalender.Increase();

but i want to implement the functionality,
that you can set an intervall at the beginning,
and then the Increase function must be called, when the timer_is_expired.

SSH: I know this alternative, but want to avoid it, if its possible,
because then i have to generate an object and set a date on suspicion in advance.
Title: Re: Call Memberfunction in rep_execute?
Post by: SSH on Fri 08/08/2008 13:11:47
So, you want to be able to generate objects outside of your module but for your module to know about them? That's not possible. You'll need to make the repeatedly_execute function in the same place that you declare the object.

However, if your module implements a game time calendar I can't see why anyone would want more than 1, so what's the harm in putting the declaration in your module? If they don't want it at all, they can just remove the module.
Title: Re: Call Memberfunction in rep_execute?
Post by: Khris on Fri 08/08/2008 13:15:31
Wouldn't this work:

GameTimeCalendar temp;  // temporary pointer

function GameTimeCalendar::Increase() {
  ...
  SetTimer(x, y);
  temp = this;
}

function repeatedly_execute() {
  if (IsTimerExpired(x)) {
    temp.Whatever();
  }
}
Title: Re: Call Memberfunction in rep_execute?
Post by: SSH on Fri 08/08/2008 13:20:20
No, it wouldn't. You'd need to declare temp as a pointer which you can't do to user-defined structs.
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 13:21:54
you are right, i will declare the object within the module, thx for all the help.  :)

Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 15:00:58
Another problem:

I have an defined object now:

GameTimeCalender myCalender;

but when im calling this function:
myCalender.Increase();

with declaration

GameTimeCalender::Increase()
{
  this.day++;
  ...
}

the myCalender.day hasnt changed   :(
Title: Re: Call Memberfunction in rep_execute?
Post by: Khris on Fri 08/08/2008 15:47:35
The problem seems to be something else, because this should definitely be working.

I tried:
// new module script

GameTimeCalendar myCalendar;

function GameTimeCalendar::Increase() {
  this.day++;
}

function on_key_press(int k) {
  if (k == 'C') {
    Display("%d", myCalendar.day);
    myCalendar.Increase();
    Display("%d", myCalendar.day);
  }
}

Worked fine.
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 16:16:55
Well its an unbelivable engima.
The function works when i call it per Hand, or with your method - works perfect.

But if it is called automaticlly in the rep_execute, it doesnt increase the var.
And the function is called for sure, i verified this.
I dont understand it.


function repeatedly_execute() {
  // put anything you want to happen every game cycle here
    if( calender.timer_activ )
    {   
      if(IsTimerExpired(calender.used_timer))
    {
       calender.Increase();
       Display("TIMER IS EXPIRED");
       SetTimer(calender.used_timer, calender.timer_intervall);
      } 

    }
}




Title: Re: Call Memberfunction in rep_execute?
Post by: SSH on Fri 08/08/2008 17:33:34
Are you checking that timer anywhere else, as IsTimerExpired only ever returns true once.

Much better to code your own timer by setting a variable and decrementing it every cycle,
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 17:54:42
But the Timer works without problems, and also the Increase function is called when it should be.
The only problem is, that it not really increase the value when its called this way, that is the mysterium.
Title: Re: Call Memberfunction in rep_execute?
Post by: monkey0506 on Fri 08/08/2008 17:55:36
Just for future reference, if you did need to call a function on user-defined instances of your struct you could create an Update method and tell the user they must call Update within rep_ex[_always] or your module won't work:

struct GameTimeCalendar {
 // ...
 int timer;
 import void Update();
};

void GameTimeCalendar::Update() {
 if (this.timer_activ) {
   if (!this.timer) {
     this.Increase();
     this.timer = this.timer_intervall;
   }
 }
}


Quote from: Your manual/readme/documentationNOTE: You *MUST* call the Update method for your instances of this struct or they will *NOT* be updated.

It's not as user-friendly as managed instances, but if you ever wanted to do something like this...

Edit: Fixed "timer" instead of "this.timer"
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 18:49:11
i dont get the clou about this.

what is the difference between the function, when its called from room-script ->       this.Increase();
and works as it should be, or the same function is called from the rep_execute every here and there and dont works.  ???

also the update function won't work - or better to say, the commands they are in there ->  this.Increase();
is also called in the update function but doesnt change the value.  :(

Its the first time, that i try to make a module in object oriented manner, all went really good, major functions worked perfect,
and in the end - the only thing is to implement the timer function,
at this point things run out of control, and now i'm contemplating suizide.  :P


Title: Re: Call Memberfunction in rep_execute?
Post by: Pumaman on Fri 08/08/2008 19:39:41
Are you sure it's actually the same instance that you're using in both places?

If your module declares this:
GameTimeCalender myCalender;

and increments it in repeatedly_execute; but then your main script code declares its own GameTimeCalendar then that one would not get incremented.

The easiest solution is, as monkey_05_06, to simply tell the user of the module that they need to call the Update method in their repeatedly_execute.
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Fri 08/08/2008 20:00:05
I have only one instance in the whole game (script).
in the complete room_script this function works (also in the Room_rep_Execute) - calender.Increase();
in the modul rep_exe... it want.
it is also called and running (there is no errormessage, i can see a display message within, but the var wont increase);

the update function dont work neither, (is the update function a special function within structs? if yes, is there a detailed description about it?)

i have the rep_exec in the module, so there is no need when the thing is working, that the user must call it byhimself, or?
Title: Re: Call Memberfunction in rep_execute?
Post by: SSH on Fri 08/08/2008 20:54:23
Try putting a breakpoint inside the rep_ex function and then step into the Increase method to see whats happening.
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Sat 09/08/2008 10:14:23
I dont know if its my fault - but maybe its a bug.

here is an short example what i did and whats going on -

module header -

struct Test {

import function SetRunIntervall(int day_intervall, int timer = 14);
bool timer_activ;
};

Test calendar;
export calendar;


module script

function Test::SetRunIntervall(int day_intervall, int used_timer)
{
 
  this.timer_activ = true;
    player.Say("%d - this-timer, %d - calendar_timer", this.timer_activ,  calendar.timer_activ);
}


and then in players enters screen after fade in

calendar.SetRunIntervall(100);

thats it -  and the result is
1 - this-timer, 0 - calendar_timer

i loaded an example up here ->  http://share-now.net/files/29323-this-test.zip.html
Title: Re: Call Memberfunction in rep_execute?
Post by: Khris on Sat 09/08/2008 13:35:26
That's the error right there:
Quote from: Rocco on Sat 09/08/2008 10:14:23
module header -
...
Test calendar;
export calendar;

Move that to the module script and put "import Test calendar;" into the header.

Don't declare stuff in the header because the header is put an top of every other script, thus creating many variables with the same name if you declare them inside.
Title: Re: Call Memberfunction in rep_execute?
Post by: Rocco on Sat 09/08/2008 14:34:30
Big thx, that was the mistake  ::)  :),
it seems that now all is working as it should be. :)