Aller au contenu

Photo

Error: CreateObject after a conditional


13 réponses à ce sujet

#1
georage

georage
  • Members
  • 247 messages
Why does this give me an error?

E: 14:30:52 - trigger_zyyna.nss - trigger_zyyna.nss(98): Incorrect variable state left on stack (while compiling var_constants_h.nss)

if(!IsObjectValid(GetObjectByTag("gelda")))                    
object oGelda=CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda"))); 


But this does not ...

object oGelda;                
if(!IsObjectValid(GetObjectByTag("gelda")))                    oGelda=CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda")));


Another annoying change in DAO!

Modifié par georage, 09 décembre 2009 - 07:34 .


#2
Ranlas

Ranlas
  • Members
  • 96 messages
I guess they didn't want to deal with scoping issues of having variables conditionally declared. Is it really that much of an issue? Just declare everything at the start. It's generally easier to read, anyways.

#3
Phaenan

Phaenan
  • Members
  • 315 messages
Nah, declarations in conditional blocks work fine, well, at least as long that the declared variables aren't later used outside the said block's scope.
Plus, the compiler message ain't about the declaration anyway so I'm not sure that's the root of the problem. Try with some {} around the block. It should fix the issue even with the declaration inside the block, and that nicer for the compiler and for human eyes, anyway. ^_^

Modifié par Phaenan, 09 décembre 2009 - 08:14 .


#4
DavidSims

DavidSims
  • BioWare Employees
  • 196 messages

georage wrote...

Why does this give me an error?

E: 14:30:52 - trigger_zyyna.nss - trigger_zyyna.nss(98): Incorrect variable state left on stack (while compiling var_constants_h.nss)

if(!IsObjectValid(GetObjectByTag("gelda")))                    
object oGelda=CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda"))); 


But this does not ...

object oGelda;                
if(!IsObjectValid(GetObjectByTag("gelda")))                    oGelda=CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda")));


Another annoying change in DAO!


It's a bit odd I guess, but why would you possibly want to declare a variable within an if statement that doesn't have curly brackets? You'd never be able to use the variable anyway.

This works:

if(!IsObjectValid(GetObjectByTag("gelda")))                    
CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda"))); 


as does this:

if(!IsObjectValid(GetObjectByTag("gelda")))   
{                 
object oGelda=CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda"))); 
}



#5
Sunjammer

Sunjammer
  • Members
  • 925 messages

georage wrote...

Another annoying change in DAO!

Assigning the return value from a function to a variable declared in a single line if statement is entirely pointless because you can never use it within scope. Consquently this change is actually an improvement because compilier is preventing potential mistakes: either you intended it to be a multi-line if statement and omitted the braces or you intended it to be a single line if statement in which case the assignment is superfluous.

#6
georage

georage
  • Members
  • 247 messages
OK, I got it, I think.

EDIT: I need a break! Brain=Hurts

Modifié par georage, 09 décembre 2009 - 09:20 .


#7
georage

georage
  • Members
  • 247 messages
There must be something I don’t grasp concerning tag-based scripts.



I have no creatures placed in my area or module.



I am trying to spawn them when a player enters the area.



This works:

if(IsObjectValid(GetObjectByTag("gelda")))

CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda")));




This does not:

if(!IsObjectValid(GetObjectByTag("gelda")))

CreateObject(OBJECT_TYPE_CREATURE,R"gelda.utc",GetLocation(GetObjectByTag("wp_ftalk_gelda")));






I am trying to check if a unique object exists before I create another one.


#8
Sunjammer

Sunjammer
  • Members
  • 925 messages
How are you triggering it? From the Area, a Trigger, something else?

#9
georage

georage
  • Members
  • 247 messages
From a trigger at this point.



It seems to work the opposite as intended, which has me pulling my hair out.




#10
Phaenan

Phaenan
  • Members
  • 315 messages
Don't quote me on this, but your hair-pulling issue may come from using IsObjectValid() as I somewhat understand it's different from a test against the OBJECT_INVALID constant. (and I'm not sure how it copes with getting direct functions results as parameters) (but I may well be paranoid) (and completely wrong) (I bet I can put more text within those brackets than outside !)

Modifié par Phaenan, 09 décembre 2009 - 11:25 .


#11
FalloutBoy

FalloutBoy
  • Members
  • 580 messages

georage wrote...

From a trigger at this point.

It seems to work the opposite as intended, which has me pulling my hair out.


I agree, that seems backwards. Maybe this isn't the best way to check if something exists in a level or not. Maybe GetObjectByTag checks to see if the object exists as a resource, regardless if it has been spawned somewhere or not. You might test with something like this:

// this should fail
result = IsObjectValid( "fasdfkjas" )
DisplayFloatyMessage( IntToString(result) );

// not sure what this will do
result = IsObjectValid( "gilda" );
DisplayFloatyMessage( IntToString(result) );

CreateObject( ... )

// this should succeed
result = IsObjectValid( "gilda" );
DisplayFloatyMessage( IntToString(result) );

Modifié par FalloutBoy, 10 décembre 2009 - 12:07 .


#12
georage

georage
  • Members
  • 247 messages
Per FalloutBoy's idea, a creature that is in the area returns a value of 1, a creature that is not there is 0. (from a heartbeat script)



int x=IsObjectValid(GetObjectByTag("gelda")); //=1

int y=IsObjectValid(GetObjectByTag("nobody")); //=0



int x=(!IsObjectValid(GetObjectByTag("gelda"))); //=0

int y=(!IsObjectValid(GetObjectByTag("nobody"))); //=1












#13
georage

georage
  • Members
  • 247 messages
Ummm ... I am not sure why, but moving my heartbeat system to the one proposed by FalloutBoy solved this problem.


#14
kilrex

kilrex
  • Members
  • 69 messages

georage wrote...

Per FalloutBoy's idea, a creature that is in the area returns a value of 1, a creature that is not there is 0. (from a heartbeat script)

int x=IsObjectValid(GetObjectByTag("gelda")); //=1
int y=IsObjectValid(GetObjectByTag("nobody")); //=0

int x=(!IsObjectValid(GetObjectByTag("gelda"))); //=0
int y=(!IsObjectValid(GetObjectByTag("nobody"))); //=1


since !1 = 0 and !0 = 1, you're results are to be expected.
instead consider this

int x=IsObjectValid(GetObjectByTag("gelda")); //=1
int
y=IsObjectValid(GetObjectByTag("nobody")); //=0

int z=(!IsObjectValid(GetObjectByTag("gelda"))); //=0
int a=(!IsObjectValid(GetObjectByTag("nobody"))); //=1

!x will be 0
!y will be 1

so z will also be 0
and a will be 1

you need to break this down in and start witht he inner most function/method to see what it can return
and then check if all the possible results are valid values for the function containing the it.

So if GetObjectByTag can return something that causes IsObjectValue to mess up, you need to validate that the result from GetObjectByTag is something that you IsObjectValid can handle.

    object foo;
    int result = 0;
    foo = GetObjectByTag("gelda");
// TEST foo, if and then only if it's something IsObjectValid can use call
    if  ( /* foo is valid for IsObjectValid */ ) result = IsObjectValid(foo);
    else /* don't call IsObjectValid */ PrintToLog("not a valid input for IsObjectValid");


as for what IsObjectValid can accept, I don't know, I'll have to refer you to RTFM...
or RTPNEM (PNE = practically non-existent)