Hi! So I have a script where I (amongst other things) want to pass an argument to play an audioclip, for this I used 'this *AudioClip'. Which causes a "undefined token" when I try to run the game, but if I remove it, there's no problem at all The script is its own file and I have imported the script through its header. Here's the code (for both with 'this *AudioClip' and without it):
Call function: keymash(aSilence, 380, 177, 21, 4); // This line gives me the error: Error (line 247): Undefined token 'keymash'
function keymash(int x, int y, int loop, int delay)
{
gprompt.BackgroundGraphic = 137;
gprompt.SetPosition(x, y);
gprompt.Visible = true;
while(mashes <= 10)
{
WaitKey(10000);
if (IsKeyPressed(eKeySpace) == true)
{
gprompt.BackgroundGraphic = 136;
cvanessa.Animate(loop, delay, eOnce, eBlock);
mashes += 1;
gprompt.BackgroundGraphic = 137;
}
}
}
function keymash(this AudioClip*, int x, int y, int loop, int delay)
{
gprompt.BackgroundGraphic = 137;
gprompt.SetPosition(x, y);
gprompt.Visible = true;
while(mashes <= 10)
{
WaitKey(10000);
if (IsKeyPressed(eKeySpace) == true)
{
gprompt.BackgroundGraphic = 136;
cvanessa.Animate(loop, delay, eOnce, eBlock);
AudioClip.Play();
mashes += 1;
gprompt.BackgroundGraphic = 137;
}
}
}
Maybe I'm butchering the use of pointers, but that's why I ask, to get pointed in the right direction. :)
Thanks
If you're using the this keyword, shouldn't it be:
aSilence.keymash(380, 177, 21, 4);
Instead? You seem to have inadvertantly made an extender function (http://www.adventuregamestudio.co.uk/manual/ags40.htm#extenderfunctions).
That gave me this error at the line: AudioClip.Play();
Error (line 18): must have an instance of the struct to access a non-static member
Oh! I missed that. Replace
AudioClip.Play ();
with
this.Play ();
When referencing stuff you declare with the this keyword, use this inside the function, instead of the class.
That worked, thanks a lot Scavenger. The manual doesn't do a good job to explain how pointers can be used in-depth, but I found the article on the wiki right after you replied, I'll be reading it through now in case I come across something similar in the future. Thanks :)
If you're referring to the "AGS Pointers for Dummies" article, I just want to mention that it only makes mention of extender methods, but doesn't really explain in depth. They are somewhat an extension of how pointers in AGS are used, so it might be worth adding something more about those if you found the manual entry (http://www.adventuregamestudio.co.uk/manual/ags40.htm#extenderfunctions) unclear. Basically what you need to understand is that you use an extender method if you want to add a new function to the built-in structures (like if you want to add a "Scream" function for all characters). You would use normal pointers if you're doing something more generic, type agnostic, or operating on multiple types.
Yeah I meant the pointers for dummies article. Also I didn't notice that part of the manual, thanks for the link and the explanation, I think I get it now.
I just read a bit about it and I want to confirm something. So basically the 'this' pointer points to specific member-function of a class? Say I create a class that has a function that do some math to combine two values, then I create another function were I check if the value of the current object is smaller than another, I'd write it like this:
class class_name
{
class_name(int x, int y)
{
nr1 = x;
nr2 = y;
}
int Combined()
{
return nr1 + nr2;
}
int Check(class_name another)
{
return this->Combined < another.Combined;
}
private:
int nr1;
int nr2;
};
So when I want to call the function, I write it like this:
Object1.Check(Object2); // provided that I have created two objects to compare.
So my understanding is that this part: Object1. refers to my 'Combined' function, because of the syntax: 'this->Combined' which refers to the objects individual interpretation of the 'Combined' function (based on its own private values).
But in AGS the 'this' keyword refer to types, which is the "object" (in lack of a better word) of a structure, so basically (if I can draw some parallels to C++) it refers to the whole object instead of a member-function inside the object? Correct? If so, can I use the 'this' keyword for custom created types, or is it only for the built in?
Please call me out if I'm completely mistaken, it's pretty late over here and I'd better sleep. But I got kind of invested in the concept over the course of the evening lol.
Let's say you do want to add a Scream method to the Character class.
In AGS, you can use an extender method for this:
void Scream(this Character*) {
this.Say("AAAAAAAAAAH!");
}
What AGS now does is add this to its own Character class:
// inside class Character { ... }
void Scream() {
this.Say("AAAAAAAAAAH!");
}
So when you now call cDude.Scream();, "this" will point to cDude.
You can create your own classes using the struct keyword:
struct MyClass {
int a;
void setA(int value);
};
void MyClass::setA(int value) {
this.a = value;
}
You can now do
MyClass theInstance;
theInstance.setA(5);
"this" points to theInstance, and thus the function will set theInstance.a to 5.
The entry in the manual for the this keyword (http://www.adventuregamestudio.co.uk/manual/ags46.htm#this) is a bit sparse, but if you have an understanding of C/C++ classes then the basic usage is the same. As Khris demonstrated though, AGS doesn't have a -> (structure dereference) operator, it just uses the . (structure reference) member.
Oh, also, AGS structs don't allow constructors or destructors, which is another difference to keep in mind if reading materials about C++ classes.
Alright, so it's basically the same between the two. Thanks again. haha