Author Topic: The getStateVals is giving me a headache, someone help?  (Read 1984 times)

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
The getStateVals is giving me a headache, someone help?
« on: August 01, 2012, 03:01:57 AM »
So. I need to save a reference to an Object, to another Object.
I can use getStateVals to save an uid of the target object to the source object.
This works..... to a point.

When the source object is a planet, it works beautifully.
When the source object is a system, however, as soon as I try to retrieve the value in client, getStateVals returns false.
Works fine in the server code, it seems.

I have added isSetting to the resources.xml.
I have called (in the server) getState() just to make sure the state is allocated.

I am at loss here.
Here's the part of the code that set up the State, running on server side.

Code: [Select]
const string@ strRallyToObject = "CustomRallyPoint";

Code: [Select]
uint cnt = getSystemCount();
int numPlanets = -1;
for (uint i = 0; i < cnt; ++i) {
System@ sys = getSystem(i);
Object@ sys_obj = sys;
if (@sys == null)
continue;

uint UID = 0;
State@ s = sys_obj.getState( strRallyToObject );
float tmp1 = 0.f, tmp2 = 0.f, tmp3 = 0.f, tmp4 = 0.f;
if(!sys.isVisibleTo(emp)) {
SysObjList objects;
objects.prepare(sys);

// Set up custom Rally Point for system
float tmp = 0;
if( sys_obj.getStateVals(strRallyToObject, tmp1, tmp2, tmp3, tmp4) ){
sys_obj.setStateVals(strRallyToObject, 0.f, 0.f, 0.f, 0.f);
if(log) print( "Resetting Rally Point for " + sys_obj.getName() + " uid = " + sys_obj.uid );
}
else
if(log) print( "Failed resetting Rally Point for " + sys_obj.getName() + " uid = " + sys_obj.uid );

objects.prepare(null);
}
}


As you can see, it outputs verification with uid.
Code: [Select]
Resetting Rally Point for Haol uid = 1058
Resetting Rally Point for Vilo uid = 1110
Resetting Rally Point for Uza uid = 1188
Resetting Rally Point for Daol uid = 1196
Resetting Rally Point for Derm uid = 1202

Here's the set up in the resources.xml
Code: [Select]
<resource name="CustomRallyPoint">
<isSetting/>
</resource>

Now here's the client code that reads this value.
The client code is executed as a response to context menu, inside of addGalacticCommands()

Code: [Select]
Object@ rally = null;
Object@ obj = sysFrom.toObject();
float tmp1 = 0.f, tmp2 = 0.f, tmp3 = 0.f, tmp4 = 0.f;
print( "Getting Rally Point for " + obj.getName() + " uid = " + obj.uid );
if( obj.getStateVals(strRallyToObject, tmp1, tmp2, tmp3, tmp4) ) {
uint UID = uint(tmp1);
print( "tmp = " + tmp1 + ", " + tmp2 + ", " + tmp3 + ", " + tmp4 );
print( "Get Rally point = " + UID );
if( UID != 0 ){
@rally = getObjectByID(UID);
}
}
else
print( "Failed getting Rally Point for " + obj.getName() + " uid = " + obj.uid );

And this is what I get:
Code: [Select]
Getting Rally Point for Uza uid = 1188
Failed getting Rally Point for Uza uid = 1188

Now I have almost identical code in addGeneralOrders()

Code: [Select]
Object@ rally = null;
float tmp1 = 0.f, tmp2 = 0.f, tmp3 = 0.f, tmp4 = 0.f;
print( "Getting Rally Point for " + from.getName() );
if( from.getStateVals(strRallyToObject, tmp1, tmp2, tmp3, tmp4) ) {
uint UID = uint(tmp1);
print( "tmp = " + tmp1 + ", " + tmp2 + ", " + tmp3 + ", " + tmp4 );
print( "Get Rally point = " + UID );
if( UID != 0 ){
@rally = getObjectByID(UID);
}
}
else
print( "Failed getting Rally Point for " + from.getName() );

And this one works fine, as long as the 'from' is a planet.
I really need to test it with Ships too.

seronis

  • Distracted
  • ***
  • Posts: 338
  • Karma: +25/-0
    • View Profile
    • Steam Profile Link
Re: The getStateVals is giving me a headache, someone help?
« Reply #1 on: August 01, 2012, 08:46:37 AM »
Object@ sys_obj = sys;

Does angelscript allow the auto cast there?  Wouldnt you need

Object@ sys_obj = sys.toObject();

?
Any code/mods I post on this site should be considered Public Domain. Borrow it, steal it, shred it into little pieces to your hearts content. I dont require credit but I appreciate bug fixes and suggestions.

seronis

  • Distracted
  • ***
  • Posts: 338
  • Karma: +25/-0
    • View Profile
    • Steam Profile Link
Re: The getStateVals is giving me a headache, someone help?
« Reply #2 on: August 01, 2012, 08:56:21 AM »
from.getStateVals( ... )

Why do you bother checking the return value of this? The return value (a bool) is only gonna state whether a value had already been assigned. The only thing you should be worried about is the  val1 - val4 values. Check them for what state you want them to have.  It seems to me your client code is working fine, its the server code not ever SETTING any initial values for your new state on ships that is being overlooked.

Also I havent messed with resources too much, only states. Why do you create a resource entry and what affect does this have?

-edit- for clarification
« Last Edit: August 01, 2012, 08:59:35 AM by seronis »
Any code/mods I post on this site should be considered Public Domain. Borrow it, steal it, shred it into little pieces to your hearts content. I dont require credit but I appreciate bug fixes and suggestions.

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #3 on: August 01, 2012, 09:22:07 AM »
Object@ sys_obj = sys;

Does angelscript allow the auto cast there?  Wouldnt you need

Object@ sys_obj = sys.toObject();

?

Holly crap, I didn't see that.
That might be what's causing the issues.

Although compiler didn't give me any grief, and the getName() actually return correct value later in the same code.
I know you can do it other way around (from Object@ to System@).
« Last Edit: August 01, 2012, 09:34:32 AM by Jyin »

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #4 on: August 01, 2012, 09:25:11 AM »
from.getStateVals( ... )

Why do you bother checking the return value of this? The return value (a bool) is only gonna state whether a value had already been assigned. The only thing you should be worried about is the  val1 - val4 values. Check them for what state you want them to have.  It seems to me your client code is working fine, its the server code not ever SETTING any initial values for your new state on ships that is being overlooked.

Also I havent messed with resources too much, only states. Why do you create a resource entry and what affect does this have?

-edit- for clarification

The thing is, the State may or may not be allocated.
If they are not allocated, getStateVals will return false.
If it return false, it's my understanding that, you can't assign new values.

On the server side, you can use getState to allocate State.

seronis

  • Distracted
  • ***
  • Posts: 338
  • Karma: +25/-0
    • View Profile
    • Steam Profile Link
Re: The getStateVals is giving me a headache, someone help?
« Reply #5 on: August 01, 2012, 09:28:53 AM »
If it returns false as far as I know it only means its not YET assigned values. I use a few states in tweaks of my own and I never check the return value of that. I just make sure my stringname is unique and assign a value as needed. I've also never used an entry in resource.xml

Basically the state is allocated the first time you 'set' values. This is how std::maps work at least and it was my assumption thats how states are implimented
« Last Edit: August 01, 2012, 09:31:09 AM by seronis »
Any code/mods I post on this site should be considered Public Domain. Borrow it, steal it, shred it into little pieces to your hearts content. I dont require credit but I appreciate bug fixes and suggestions.

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #6 on: August 01, 2012, 09:37:42 AM »
If it returns false as far as I know it only means its not YET assigned values. I use a few states in tweaks of my own and I never check the return value of that. I just make sure my stringname is unique and assign a value as needed. I've also never used an entry in resource.xml

Basically the state is allocated the first time you 'set' values. This is how std::maps work at least and it was my assumption thats how states are implimented

That's how *server* side works, I believe.
On the *client* side, I don't think you can allocate State that isn't already allocated by server.
Thus, it's important to check the return value of get before you set.
The resources.xml allows the client to access server's State. If you don't set that, get will never return true.

seronis

  • Distracted
  • ***
  • Posts: 338
  • Karma: +25/-0
    • View Profile
    • Steam Profile Link
Re: The getStateVals is giving me a headache, someone help?
« Reply #7 on: August 01, 2012, 09:39:37 AM »
OK i just confirmed with GGLucas in irc.  I was correct that the xml entry is not required at all, its just used to tweak some of the metadata tags on states that you might be using.  using  setStateVals() is always a safe operation and will create any non existant states.

One thing i didnt know is ggl said that when you call setStateVals() any 'State@ blah' references you've already pulled from that object are probably now invalidated.  No different than in C++ when you have an Iterator to a container of some type and you adjust its contents.
Any code/mods I post on this site should be considered Public Domain. Borrow it, steal it, shred it into little pieces to your hearts content. I dont require credit but I appreciate bug fixes and suggestions.

seronis

  • Distracted
  • ***
  • Posts: 338
  • Karma: +25/-0
    • View Profile
    • Steam Profile Link
Re: The getStateVals is giving me a headache, someone help?
« Reply #8 on: August 01, 2012, 09:40:59 AM »
That's how *server* side works, I believe.
On the *client* side, I don't think you can allocate State that isn't already allocated by server.
Thus, it's important to check the return value of get before you set.
The resources.xml allows the client to access server's State. If you don't set that, get will never return true.
You never really NEED to check for true. If a state doesnt exist your variables will be assigned default values of 0.0f.  So if you see your VALUES dont have meaningful data just do something knowing the state hasnt yet been assigned. A state not being assigned should be safely handled in any code.

-edit-

That aside, ggl said you're right about the 'isSetting' xml entry allowing a client to SET a state from client space. Shouldnt be needed for GETting a state though. Thanks for posting your issue. I love helping with problems cause I always end up learning a bit too =-)
« Last Edit: August 01, 2012, 09:45:04 AM by seronis »
Any code/mods I post on this site should be considered Public Domain. Borrow it, steal it, shred it into little pieces to your hearts content. I dont require credit but I appreciate bug fixes and suggestions.

GGLucas

  • Dr. Evil
  • BMS Staff
  • Delusional
  • *
  • Posts: 1877
  • Karma: +300/-6
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #9 on: August 01, 2012, 09:47:50 AM »
Quote
The resources.xml allows the client to access server's State. If you don't set that, get will never return true.

This is true, you need <isSetting/> to modify from the client.

Quote
On the *client* side, I don't think you can allocate State that isn't already allocated by server.

This is false. If a state is marked as a setting, the client will be able to allocate it.

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #10 on: August 01, 2012, 10:11:54 AM »
This is true, you need <isSetting/> to modify from the client.

This is false. If a state is marked as a setting, the client will be able to allocate it.

Humm.. I tried that, and I keep getting getStateVals() to return false when I didn't call getState() in the server beforehand.

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #11 on: August 01, 2012, 10:15:02 AM »
Humm.. I tried that, and I keep getting getStateVals() to return false when I didn't call getState() in the server beforehand.

As far as I'm aware that's why the if statement as an error check is there, getStateVals will not initialize a state because unlike getState() it will not handle the state not existing beforehand.
GA - Mod Team
GA Forums

seronis

  • Distracted
  • ***
  • Posts: 338
  • Karma: +25/-0
    • View Profile
    • Steam Profile Link
Re: The getStateVals is giving me a headache, someone help?
« Reply #12 on: August 01, 2012, 10:27:34 AM »
Oh that makes sense. So the if check would be useful on statevals that DID NOT have the xml entry.
Any code/mods I post on this site should be considered Public Domain. Borrow it, steal it, shred it into little pieces to your hearts content. I dont require credit but I appreciate bug fixes and suggestions.

GGLucas

  • Dr. Evil
  • BMS Staff
  • Delusional
  • *
  • Posts: 1877
  • Karma: +300/-6
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #13 on: August 01, 2012, 10:48:15 AM »
Quote
Humm.. I tried that, and I keep getting getStateVals() to return false when I didn't call getState() in the server beforehand.

getStateVals() won't allocate a state on either client or server, but setStateVals() will on the client if the state is marked as a setting. (and always on the server of course)

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #14 on: August 01, 2012, 10:49:58 AM »
Actually on client, you should *always* check the return value, just in case.

Here's what I found out.

I ripped out all the related code from both server/client side.
I dropped this code in the triggerContextMenu() in context_menu.as
I left the resources.xml alone, so the setting is in there still.

Code: [Select]
// TEST CODE
print( "Testing getStateVals() on " + selObj.getName() + " uid = " + selObj.uid );
float tmp1 = 0.f, tmp2 = 0.f, tmp3 = 0.f, tmp4 = 0.f;
selObj.getStateVals(strRallyToObject, tmp1, tmp2, tmp3, tmp4);
print( "previouse tmp = " + tmp1 + ", " + tmp2 + ", " + tmp3 + ", " + tmp4 );

tmp1++; tmp2++; tmp3++; tmp4++;
print( "input tmp = " + tmp1 + ", " + tmp2 + ", " + tmp3 + ", " + tmp4 );
selObj.setStateVals(strRallyToObject, tmp1, tmp2, tmp3, tmp4);

tmp1 = 0.f; tmp2 = 0.f; tmp3 = 0.f; tmp4 = 0.f;
selObj.getStateVals(strRallyToObject, tmp1, tmp2, tmp3, tmp4);
print( "output tmp = " + tmp1 + ", " + tmp2 + ", " + tmp3 + ", " + tmp4 );
// END TEST CODE
Here's the interesting part.

Code: [Select]
Testing getStateVals() on Zerz IV uid = 3778
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 1, 1, 1, 1
Testing getStateVals() on Zerz IV uid = 3778
previouse tmp = 1, 1, 1, 1
input tmp = 2, 2, 2, 2
output tmp = 2, 2, 2, 2
Testing getStateVals() on Zerz IV uid = 3778
previouse tmp = 2, 2, 2, 2
input tmp = 3, 3, 3, 3
output tmp = 3, 3, 3, 3

As you can see, works perfectly for planets, even if the State was never allocated on server side.

Code: [Select]
Testing getStateVals() on Zerz Star uid = 1189
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 0, 0, 0, 0
Testing getStateVals() on Zerz Star uid = 1189
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 0, 0, 0, 0
Testing getStateVals() on Zerz Star uid = 1189
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 0, 0, 0, 0

Now the problem starts, when I target @Star. Notice the output does not match the input.

Code: [Select]
Testing getStateVals() on Zerz uid = 1188
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 0, 0, 0, 0
Testing getStateVals() on Zerz uid = 1188
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 0, 0, 0, 0
Testing getStateVals() on Zerz uid = 1188
previouse tmp = 0, 0, 0, 0
input tmp = 1, 1, 1, 1
output tmp = 0, 0, 0, 0

And of course, doesn't work with @System either.
I am start to think this is a bug.
« Last Edit: August 01, 2012, 10:51:54 AM by Jyin »

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #15 on: August 01, 2012, 11:00:50 AM »
Just to be sure, I repeated the test with standard game code.
It does the same thing.

GGLucas

  • Dr. Evil
  • BMS Staff
  • Delusional
  • *
  • Posts: 1877
  • Karma: +300/-6
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #16 on: August 01, 2012, 11:20:10 AM »
Ah, apologies, I did not read your question correctly.

setStateVals on the client makes sure you have permission to alter the settings on that object, that is, whether you own it: we wouldn't want you to be able to change settings on enemy planets and screw with their game plan. Since stars and systems are owned by space, not the player, you are not able to change settings states on them regardless.

Think of it in multiplayer. If we did allow you to change states on stars, then everyone in the same game would be able to change the star's states, which would be rather chaotic.

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #17 on: August 01, 2012, 11:22:38 AM »
Ah, so the State isn't empire/player specific.
That's what I was missing.

So, how do I save custom data is tied to system/star that is empire/player specific?
« Last Edit: August 01, 2012, 11:24:10 AM by Jyin »

Azalrion

  • Delusional
  • ****
  • Posts: 1325
  • Karma: +147/-1
  • Memory Murderer
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #18 on: August 01, 2012, 11:39:50 AM »
Ah, so the State isn't empire/player specific.
That's what I was missing.

So, how do I save custom data is tied to system/star that is empire/player specific?

Code: [Select]
Object@ void setStat(const Empire@, string&, float)
Object@ float getStat(const Empire@, string&) const

Don't remember if stats can be set from client scripts though, you may need to do what the tutorial and various bits of GA do in using server messages to handle it.
GA - Mod Team
GA Forums

Jyin

  • Sentient
  • **
  • Posts: 66
  • Karma: +6/-0
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #19 on: August 01, 2012, 11:45:01 AM »
Going to have to go about this whole differently.
Oh well.

GGLucas

  • Dr. Evil
  • BMS Staff
  • Delusional
  • *
  • Posts: 1877
  • Karma: +300/-6
    • View Profile
Re: The getStateVals is giving me a headache, someone help?
« Reply #20 on: August 01, 2012, 11:50:17 AM »
Quote
Don't remember if stats can be set from client scripts though, you may need to do what the tutorial and various bits of GA do in using server messages to handle it.

Even though the functions are bound to object, only System@s actually have stat managers attached to them, so these function only work on systems.

You cannot set these stats from the client, but as Azalrion said, you can use server messages to set them through the server.