Universal Behavior Setting

Forum for plugin, effect and behavior programmers. Please use the Help & Support forum for help using Construct.

Post » Mon Jun 22, 2009 3:44 pm

GetData() is nothing more than a general-purpose plugin-defined function that other plugins can call to get data or perform actions specific to that plugin. 'id' is intended to determine what to do, and the void* param is simply four bytes of data to pass as a parameter. The return value is so the callee can get data back from the call (or it can write out to the pointer 'param').

If the basic syntax is confusing you, you might want to go over a C++ tutorial or book again some time.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,518

Post » Mon Jun 22, 2009 4:10 pm

ok
simple example

[code:2x70bejr]long ExtObject::GetData(int id, void* param)
{
//this would just return the second parameter as an integer....sorry no asterisk needed
return (int)param;
}[/code:2x70bejr]

sorry, you don't need any asterisks there, I just compiled an example to make sure this works


[code:2x70bejr]int* MyIntPointer; // a pointer to an integer

YourObject->CallFunction(1,MyIntPointer);[/code:2x70bejr]


not sure if it was my confusion that confused you,
or if you're not clear on pointers themselves

but the way they work is like so:

int* MyIntPointer; // a pointer to an integer, holds the memory address of an integer
int MyInt; // I'm pretty sure you know what this is :wink:

MyIntPointer = &MyInt // Now MyIntPointer points to the Address of MyInt(& is the address of operator)

[code:2x70bejr]MyInt = 3;[/code:2x70bejr]

now

*MyIntPointer is equal to 3
if I say
[code:2x70bejr]*MyIntPointer = 5;[/code:2x70bejr]
then
MyInt is now equal to 5

the asterisk before a pointer is called the dereference operator, and makes it so you're basically operating directly on the thing pointed to

MyIntPointer without the asterisk, is still equal to &MyInt

hope this made some sense
Edit:: if you read this before my last edit, read the first part again
it's (int)param, not int(param)
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Mon Jun 22, 2009 5:44 pm

So, if I understand this correctly, your example would merely return the value of the integer that MyIntPointer is pointing to, right? My code:

[code:jwg63bgz] int* MyIntPointer;
int MyInt;
MyIntPointer = &MyInt;
MyInt = 5;
stuff = (*i)->GetData(1, MyIntPointer);[/code:jwg63bgz]
The code I put in the GetData() function is the same as what you have.

Shouldn't what I have above return 5? I'm using a Construct expression to access the value of stuff, but it always comes back as a random value between 20 million and 25 million. :P I must still be doing something wrong. :oops:
Moderator
B
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Post » Mon Jun 22, 2009 5:53 pm

no
I bet I said something wrong
gimme a few minutes
I'm going to start from an empty sdk
and tell you exactly
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Mon Jun 22, 2009 6:09 pm

k
this is tested, it returns the right value and everything :D
forreal this time
the exact code you just posted
and this in getdata will return your param back to you
:
[code:1nhqf3aj]long ExtObject::GetData(int id, void* param)
{
int* myint =(int*)param;
return *myint;
}[/code:1nhqf3aj]

sorry for all the wrong answers :)

and to be clear, this also works:
[code:1nhqf3aj]long ExtObject::GetData(int id, void* param)
{

return *(int*)param;
}[/code:1nhqf3aj]

you are type casting the void pointer to a int pointer ( (int*) )
then you are dereferencing this pointer to get the value of what it's pointing to

please post back to make sure this worked for you
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Mon Jun 22, 2009 9:57 pm

Ooooooooo... it turns out the problem was something entirely seperate from the GetData() code. :oops: I was trying to get the data from the object that the behavior was linked to, not the actual behavior. So, long story short, everything works properly now, and I'm an idiot. :P

Sorry for all the trouble, and thanks for the help. :)
Moderator
B
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Previous

Return to Construct engineering

Who is online

Users browsing this forum: No registered users and 0 guests