Home   Help Search Login Register  

Author Topic: Tidy things up  (Read 2491 times)

0 Members and 2 Guests are viewing this topic.

Offline Proxemo

  • Members
  • *
Tidy things up
« on: 22 Sep 2007, 11:39:57 »
Hello. I consider myself new to scripting but here is the thing. I would like to know if it is possible to tidy things up. What I mean by this is right now I have script per trigger and I have quite a few triggers. Is there a way where instead of having a script per trigger, I am able to merge some scripts together and refer them to triggers on the map?

In other words, can I have trigger (A) point to a specific portion on alpha.sqs and trigger (B) point to a different portion on alpha.sqs? These triggers are activated by blufor presence.

Offline Cheetah

  • Former Staff
  • ****
Re: Tidy things up
« Reply #1 on: 22 Sep 2007, 11:46:45 »
Alright, so your idea is to make a huge script of which different portions are activated depending on which triggers activates the script?

To do this:

split your alpha.sqs into several portions by using:
Code: [Select]
; start of script
goto (_this select 0);

#portion1

blablabla.. code code

#portion2

blabla.. code code

And in each trigger:
Code: [Select]
["portion2"] exec "alpha.sqs";
This will send an array to the script (with parameters), here the array (1st element) contains a string "portion2".

At the start of the script, I extract this string out of the array passed to the script and use it to go to the correct portion. Should work.
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline Proxemo

  • Members
  • *
Re: Tidy things up
« Reply #2 on: 22 Sep 2007, 21:39:57 »
That is exactly what I want but a quick question.

Do I have to put "end" at the end of every portion or will "portion" end the previous command for me?

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re: Tidy things up
« Reply #3 on: 22 Sep 2007, 22:19:33 »
u shud use da exit command after each part...

if u use functions on the other hand (highly recomended... they work beter :D)

u wud have it look dis way

Code: [Select]
if ("portion1" in _this) then
{
     code
};

if ("portion2 in _this) then
{
     code
};

and then u wont need any exit command :D

LCD OUT

[edit] oops... some strange bug in my msg... fixed :D
« Last Edit: 01 Jan 2009, 14:18:10 by Planck »
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline hoz

  • OFPEC Site
  • Administrator
  • *****
Re: Tidy things up
« Reply #4 on: 23 Sep 2007, 05:42:59 »
Another way of doing it with sqf is using the switch command.

Code: [Select]
_event = _this select 0;

switch (_event) do
{
   case "SomeEvent":
   {
        some commands here
   };
   case "AnotherEven":
   {
    some different commands here
   };
   case "YetAnotherEvent":
   {
        Evenmore here.
   };
};

I used this structure for a cam script recently, where i could include the intro, outro cam, and some middle cutscene all in the same script.
« Last Edit: 23 Sep 2007, 05:45:22 by hoz »
Xbox Rocks

Offline Proxemo

  • Members
  • *
Re: Tidy things up
« Reply #5 on: 23 Sep 2007, 07:32:56 »
Appreciate your help fellas but when I try to use an sqf file, I receive the error:

'|#|;}'
Error Missing {

Like I said earlier, I am new and have only used sqs files. Since you guys introduced me to sqf, I have no clue on how to fix it. I don't know the syntax so whether it is a missing { or something I have done, I have no clue.

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re: Tidy things up
« Reply #6 on: 23 Sep 2007, 09:34:36 »
it may b cuz of how u called da function (bla = [parameters] execVM "functionname.sqf" is wat u need... dont use exec)

if its not dat... its probably cuz u have a missing { :P

go check dat } in ur function has a matching { somewere :P :D

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Wolfrug

  • Addons Depot
  • Former Staff
  • ****
  • Official OFPEC Old Timer
Re: Tidy things up
« Reply #7 on: 23 Sep 2007, 10:08:23 »
Also remember that .sqf scripts are generally nearly identical in use to .sqs (don't try to say different here, OFPECers, you can use all the same for...do, while...do, if...then...else forms in .sqs as you can in .sqf  ;) ), except that .sqfs need a ; after every finished command, and uses sleep instead of ~ for waiting. Oh, and you can't use goto, of course.  ;)

(I think of .sqf as glorified trigger-line mini-scripts, really).

Wolfrug out.
"When 900 years YOU reach, look as good you will not!"

Offline Cheetah

  • Former Staff
  • ****
Re: Tidy things up
« Reply #8 on: 23 Sep 2007, 13:18:46 »
Here is a tutorial about SQF scripting.
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline Baddo

  • Former Staff
  • ****
  • Reservist Jaeger
Re: Tidy things up
« Reply #9 on: 23 Sep 2007, 16:12:25 »
I advice against putting everything to a single file.

That's for sure not a programming practice you will thank yourself later.

The problem with putting too much into one file becomes clear when your script gets big. You will have trouble managing it, and keeping it error-free. The goto style programming is a sure way to confuse yourself, I suggest to avoid that. It's just too easy to make an error when building big scripts which use goto, so it's best to avoid it if you can, and yes you can avoid it.

Write small functions to accomplish clearly defined tasks. Some good examples can be found from the Editors Depot. Then use those functions together to accomplish the tasks your code needs to do in your mission. You will need to carefully determine which parts of your scripts can be separated into functions. Think about things which can be done so that the function does not need to know anything specific to the mission you are creating, or anything specific to other functions (try to keep dependencies to minimum and you will have re-usable code). For example one clear rule I can give to you that your functions should not have to rely on any global variables.

This is definitely the way to go instead of writing one big script which tries to handle the whole mission.

You should try to separate:

- Utility functions/scripts which do small tasks, and need not to know anything specific to your mission in order to work. An example of what one such file could do: eject a group out of a flying helicopter.

- Mission logic functions/scripts, these are the ones which use the utility functions/scripts to accomplish some small sub-tasks in a certain order and timing. An example: a script which makes the helicopter fly into a drop zone, and then calls the utility function to eject the group.

« Last Edit: 23 Sep 2007, 16:22:57 by Baddo »

Offline Proxemo

  • Members
  • *
Re: Tidy things up
« Reply #10 on: 24 Sep 2007, 09:59:04 »
Thnx for the reply fellas. I haven't tested it out yet but I just wanted to say thanks again. I'm not putting the entire mission into one sqf. It is mainly for text (hint/hintc) commands. I'm making a tutorial and I have a lot of these running around. I thought that the more sqs files you have the slower/longer you it will take to load. Not sure if there is any validity to this.

As far as sqs/sqf, I have to do more reading on the subject. Right now they are the same to me but Baddo cleared things up a little bit. I appreciate it.

Offline Cheetah

  • Former Staff
  • ****
Re: Tidy things up
« Reply #11 on: 24 Sep 2007, 18:52:14 »
Code: [Select]
I thought that the more sqs files you have the slower/longer you it will take to load. Not sure if there is any validity to this.
Hmm, they aren't all active at the same time, thus if you have five of 1kb each and only one is active you have 1kb in the cache. While this is 5kb if they are all in the same file. Notice that it isn't as simple as this ;)
Like missions? Help with Beta Testing! or take a look at the OFPEC Missions Depot for reviewed missions!

Offline hoz

  • OFPEC Site
  • Administrator
  • *****
Re: Tidy things up
« Reply #12 on: 25 Sep 2007, 02:58:47 »
I would do something like this. I've become a big fan of the switch command.   :good:

init.sqf or init.sqs
Code: [Select]
Chatter = compile (preprocessFileLineNumbers "chatter.sqf");
To call the particular event

["SomeEvent"] call Chatter


chatter.sqf
Code: [Select]
_event = _this select 0;

switch (_event) do
{
   case "SomeEvent":
   {
       Hint "some commands here";
   };
   case "AnotherEven":
   {
    HintC "some different commands here";
   };
   case "SideChatEvent":
   {
       player sidechat "A sidechat even";
   };
};

This loads the chatter.sqf when the mission loads, and then when you use call it pulls the text from memory.
« Last Edit: 25 Sep 2007, 03:01:20 by hoz »
Xbox Rocks

Offline Proxemo

  • Members
  • *
Re: Tidy things up
« Reply #13 on: 25 Sep 2007, 08:05:23 »
Thnx for the info. Again forgive the noob questions but to what hoz wrote:

What advantage does switch commands have over sqf?

I came in thinking sqs was the way to go and I learn 3 different ways of writing it :P

My head hurts >:(

Thanks again fellas.