Home   Help Search Login Register  

Author Topic: Reference Types and Dynamic gotos  (Read 1191 times)

0 Members and 1 Guest are viewing this topic.

Rocko Bonaparte

  • Guest
Reference Types and Dynamic gotos
« on: 11 Dec 2004, 02:00:01 »
I'm finding myself in a situation where the easiest way out would be exploiting some more advanced scripting syntax.  It will take many lines of code to just try what I'm doing, so I'm hoping to save two hours and just ask of my possibilities.

Can I pass variables as reference types?  Specifically, I want a global variable to go to two different scripts in my init.sqs.  I want both to be able to modify it.  Is this possible?

Second, I think I need something like function pointers.  Specifically, a variable that can store a jump point.  I see that goto requires quotes around the jump point, which makes me believe it's a string.  Does that mean a variable string can be used as a goto target?

I can work around these, but the code would be long and ugly, so I'm hoping for an easy way out.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Reference Types and Dynamic gotos
« Reply #1 on: 11 Dec 2004, 02:33:06 »
1)when you modify the global variable, do you want to use it later in something else? if so, i think you're out of luck, but you could make another variable, to assign your new value to
if it is staying in the script, then just do this:
_myLocalVariable = myglobalvariable
then _mylocalvariable will have the same value as the global variable
but if you change a global in one script that changes it everywhere, so the last change made will be what it is

2)why would you want to assign a variable for a goto label?
that doesn't give you less work or save space in any way, the only thing i can get is you want to conditionally go to a different label, but still it wouldn't change much for you... perhaps more explaination?

Rocko Bonaparte

  • Guest
Re:Reference Types and Dynamic gotos
« Reply #2 on: 11 Dec 2004, 05:16:35 »
The globals would stay in the scripts.

As for dynamic gotos, it's for the AI script I'm writing.  I had posted a thread about it asking how it would perform when completed.  It's a big state machine that orders the unit somewhere, and then pauses until the unit goes there or does whatever it needs to do.  The problem is if something happens along the way--specifically, the unit gets killed.  The script would lose coherency because the units respawn.

Rather than write the code for handling a respawn over and over and over again in the state machine, I would want to go to that common code first.  If the unit wasn't killed, then it would go to whatever it was just assigned and just execute that code.  

What I'm thinking of writing now is a calling a function for the common code.  For testing if the unit died, I think I need a script that just waits to see if the unit was killed.  I can't trust the AI script to handle it right because it delays for many seconds at points.  During that delay, the unit could have died and respawned.  According to the script logic, the unit was alive before the delay, and it was alive after the delay, so all is well.  I'd need the global variable to notify it that it had died in the interim.

Offline Sui

  • Former Staff
  • ****
    • OFPEC
Re:Reference Types and Dynamic gotos
« Reply #3 on: 11 Dec 2004, 06:31:55 »
Hmm... I'm not too sure what you mean by your first question...
A global variable is global. Both scripts can use it, and modify it.
However so can any other script. You mean you want it specifically for those two scripts and no others?

Your second question is a bit easier... yes you can.
For example:

Quote
#return1
? (condition1): _return = "return1"; goto "subroutine"
exit

#return2
? (condition2): _return = "return2"; goto "subroutine"
exit

#return3
? (condition3): _return = "return3"; goto "subroutine"
exit

#subroutine
action dependant on return
goto _return

Was that more or less what you were after?
I use this all the time... I can effectively put as many scripts as I want into one single .sqs file this way. And I can have them all reference each other using local variables, as they are technically all the same script ;)

Offline Fragorl

  • Coding Team
  • Former Staff
  • ****
Re:Reference Types and Dynamic gotos
« Reply #4 on: 11 Dec 2004, 08:22:35 »
#Edit

Whoops should have read Sui's post!
« Last Edit: 11 Dec 2004, 08:23:53 by Fragorl »

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Reference Types and Dynamic gotos
« Reply #5 on: 11 Dec 2004, 16:34:37 »
2)you could do something like this:
#start
goto "common"
#continue
code stuff



#common
stuff you don't want to repeat
goto "continue"

then just put that goto whereever you need the common code
« Last Edit: 11 Dec 2004, 16:35:36 by Triggerhappy »

Rocko Bonaparte

  • Guest
Re:Reference Types and Dynamic gotos
« Reply #6 on: 11 Dec 2004, 22:00:01 »
2)you could do something like this:
#start
goto "common"
#continue
code stuff



#common
stuff you don't want to repeat
goto "continue"

then just put that goto whereever you need the common code
That's along the lines of what I wanted to do.  After the common code block, I wanted to go to someplace else in the program that was set previously in a variable.  Generally, I just have to keep track of where I was in the script with the least amount of overhead code.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Reference Types and Dynamic gotos
« Reply #7 on: 12 Dec 2004, 04:36:11 »
hhmm, well, i say just try it out in a small script
assign a string to a varible and test it

Rocko Bonaparte

  • Guest
Re:Reference Types and Dynamic gotos
« Reply #8 on: 13 Dec 2004, 04:44:33 »
Dynamic gotos and reference types both seem to work.  I've rewritten my script to exploit it.  When a unit dies, the AI script can reset itself and start manuevering from the spawn point.  I've moved code like that to a common logic block that gets run before any of the orders.  The amount of code I have floating around isn't so bad, and I've finished adding all the waypoints.  I need to spice up the logic in some cases, but it's mostly there.