Forum

> > Stranded II > Scripts > Wait function
Forums overviewStranded II overview Scripts overviewLog in to reply

English Wait function

7 replies
To the start Previous 1 Next To the start

old Wait function

Kabbotta
User Off Offline

Quote
I have had difficulty creating a continuous process that can be cancelled using a script. Here is an example of what I have tried.

on:use {
          $tmp = getplayerweapon();
          // Set Goal
          local $goal, $cur, $targ;
          $goal = 4; $cur = 0;
          // Gather
          if ( $branches == 0 ) {
               msg "Tree is stripped of branches.", 800;
          } elseif ( $tmp == 87 ) {
               loop( "count", $goal ) {
                    local $id;
                    $id = create( "item", 24 );
                    store $id, "unit", 1;
                    $branches--; $cur++;
                    process "Removing branch", 600;
               }
          } else {
               msg "Need an axe.", 800;
          }
          freevar $tmp, $id;
     }
}

The problem is that the loops repeats before the process ends. So clearly I can see the process is run separately, but I couldn't find anything like a wait function that would the loop to allow the process to end. I realize my whole concept might be wrong. I have also tried using a sequence. I know how to program so if you have a completely different way a quick pointer would be helpful.

old Re: Wait function

DC
Admin Off Offline

Quote
you can't let scripts wait because that would pause the whole game! everything is executed instantly and the process command can't change that.

BUT the process command has a third parameter which can be used to specify a global event which will be triggered when the process is done.

you can use this for delayed execution. the following code is untested but I hope you get the idea. the loop is performed by using an additional event which is executed by the process command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
on:use {
	$tmp = getplayerweapon();
	// Set Goal
	local $goal, $cur, $targ;
	$goal = 4; $cur = 0;
	// Gather
	if ( $branches == 0 ) {
		msg "Tree is stripped of branches.", 800;
	} elseif ( $tmp == 87 ) {
		process "Removing branch", 600,"rem_branch";
	} else {
		msg "Need an axe.", 800;
	}
	freevar $tmp, $id;
}

// this event is the new loop!
on:rem_branch{
	local $id;
	$id = create( "item", 24 );
	store $id, "unit", 1;
	$branches--; $cur++; $goal--; // <- note: goal is decreased here
	if ($goal>0){
		// continue with the loop if goal is >0
		process "Removing branch", 600,"rem_branch";
	} else {
		// ... otherwise quit the loop by not calling process anymore
		freevar $id;
	}
}

note: please use the [code]-tag for codes and use tab to indent your code.

note²: there was one } too much in your code (at the end).

old Re: Wait function

Hurri04
Super User Off Offline

Quote
so I interprete that you want to implement a script that only allows the player to gather branches from a tree if the tree isnt "out of branches", is that right?

in that case, I dont think you need a loop. also it seems to me that you use a few local variables which arent needed. further there might be an error with the if commands.

I'll try and rewrite the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
on:create {
	local "$branches";
	$branches=random(10, 20);
}

on:use {
	if(getplayerweapon()==87) {
		if($branches>0) {
			$branches--;
			process "Removing branch", 1000, "removing_branch";
		}else{
			msg "Tree is stripped of branches!";
			speech "negative";
		}
	}else{
		msg "I need to equip an axe!";
		speech "negative";
	}
}

also create a new file "game_branch.inf" in the sys folder and write this into it:
1
2
3
4
5
script=start
on:removing_branch {
	find 24, 1;
}
script=end
More >

old Re: Wait function

Kabbotta
User Off Offline

Quote
This is what I have settled on for now. I used a modified version of DC's suggestion. I had to make the recursive events local because I wanted to be able to keep track of the number of branches/leaves for each different tree and I couldn't see how to make the process call a local event directly. If anyone can see a way to clean it up it seems a little messy to me, but it works ; ) Thanks a lot for the help so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
on:start {
	local $branches, $leaves;
	$branches = 20; $leaves = 30;
}
on:use {
	$break = 0;
	scantarget;
	$tmp_id = targetid();
	$tool = getplayerweapon();
	if ( $branches == 0 ) {
		msg "Tree is stripped of branches.", 600;
	} elseif ( $tool == 87 ) {
		event "gthr_branch", "object", $tmp_id;
	} elseif ( $tool == 28 ) {
		event "gthr_leaves", "object", $tmp_id;
	} else {
		msg "Need a tool.", 600;
	}
}
on:gthr_branch {
	if ( $break == 1 ) {
		msg "Stopped gathering.", 600;
	} elseif ( $branches > 0 ) {
		$branches--;
		local $id;
		$id = create( "item", 24 );
		store $id, "unit", 1;
		process "Removing branch.", 1400, "recurse";
		animate $hands, 11, 20, 0.52, 3;
		freevar $id;
	} else {
		msg "Tree is stripped of branches.", 600;
	}
}
on:gthr_leaves {
	if ( $break == 1 ) {
		msg "Stopped gathering.", 600;
	} elseif ( $leaves > 0 ) {
		$leaves--;
		local $id;
		$id = create( "item", 15 );
		store $id, "unit", 1;
		process "Removing leaf.", 1100, "recurse";
		animate $hands, 11, 20, 0.58, 3;
		freevar $id;
	} else {
		msg "Tree is stripped of leaves.", 600;
	}
}
// Global recursive event written into game.inf
on:recurse {
	if ( $tool == 87 ) {
		event "gthr_branch", "object", $tmp_id;
	} elseif ( $tool == 28 ) {
		event "gthr_leaves", "object", $tmp_id;
	}
}
// Break action code also in game.inf
	on:keydown01 {
		event "break", "global";
	}
	on:break {
		$break = 1;
	}
edited 1×, last 24.02.12 05:53:56 am

old Re: Wait function

Corvallis5
User Off Offline

Quote
it looks great but maybe instead of using the variable tool, you could just replace that with playergotweapon so something like
1
2
3
if (playergetweapon(28)) {
script;
}
instead of
1
2
3
if ($tool==28) {
script;
}
that way the game has 1 less variable.
but to be honest it doesn't really make a difference if you change it or not .

old Re: Wait function

Hurri04
Super User Off Offline

Quote
so I understand that you want the player to collect one branch/stick after another until there is none left or he interrupts the process by pressing a key, is that correct?

first of all you should know that it is best in S2S (Stranded2Script) to use as few events as possible because they use up quiete a lot of calculation time and therefore eventually might slow down weaker computers if they appear in larger numbers.

next, the scantarget and targetid commands are kinda buggy and also not very efficient. you can simply use
1
$tmp_id=currentid();
instead. other than that you can also use the parameter "self" instead of the two parameters "[CLASS]", [ID] in any command.

also, as I demonstrated in the script in my previous post, you can use
1
find 24, 1;
instead of
1
2
$id = create( "item", 24 );
store $id, "unit", 1;
if you want the player to find something. by this the items is also shown to the player as it "pops out" of the crosshair and then falls to the bottom of the screen, just like the effect when you pick something up from the ground. of course, if you dont want that, the create&store script is the way to go.

and you dont really need to use the freevar command since the variable $id is used again afterwads anyway.

further your s2 cmd msg command lines are wrong, you cant put 600 as the second parameter.

I'll rewrite the code once again:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
on:start {
	local "$branches", "$leaves";
	$branches=20;
	$leaves=30;
}

on:create {		//you need this because you can also plant trees after the start of the game!
	local "$branches", "$leaves";
	$branches=20;
	$leaves=30;
}

on:use {
	if(getplayerweapon()==87) {
		if($branches>0) {
			$branches--;
			$object_id=currentid();
			process "Removing branch", 1400, "removing_branch";
		}else{
			msg "Tree is stripped of branches!";
			speech "negative";
		}
	}elseif(getplayerweapon()==28) {
		if($leaves>0) {
			$leaves--;
			$object_id=currentid();
			process "Removing leaf", 1100, "removing_leaf";
		}else{
			msg "Tree is stripped of leaves!";
			speech "negative";
		}
	}else{
		msg "I need to equip an axe or a machete!";
		speech "negative";
	}
}

game_tree.inf:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
scriptkey=01, interrupt processes
script=start
on:removing_branch {
	find 24, 1;
	if($break==0) {
		event "use", "object", $object_id;
	}else{
		$break=0;
	}
}

on:removing_leaf {
	find 15, 1;
	if($break==0) {
		event "use", "object", $object_id;
	}else{
		$break=0;
	}
}

on:keyhit01 {
	$break=1;
}
script=end

I think my script looks both simpler and more efficient, doesnt it?

(Edit: little code optimization)
edited 3×, last 24.02.12 05:22:38 pm

old Re: Wait function

Kabbotta
User Off Offline

Quote
Yes, that was very helpful. A few things like the currentid() function and being able to call the built in events like "use" I didn't know about. Thanks Hurri.

I took your advice and tried to get rid of the extra events. I removed all of the separate "remove_branch", "remove_leaves" events and replaced them with one single "gather" event that knows what to give the player based on a global variable, but I couldn't figure out how to use the "find" because I couldn't get it to use a variable in it's argument.

1
2
3
4
5
6
7
8
9
// "$object_find" is a global variable defined in use event 
on:gather {
	find $object_find, 1;
	if( $break == 0 ) {
		event "use", "object", $object_id;
	} else {
		$break = 0;
	}
}
That didn't work. So I used the store again.
1
2
3
4
5
6
7
8
9
10
on:gather {
	local $id;
	$id = create( "item", $object_find );
	store $id, "unit", 1;
	if ( $break == 0 ) {
		event "use", "object", $object_id;
	} else {
		$break = 0;
	}
}

Thanks again for the great ideas.
edited 1×, last 26.02.12 11:24:27 pm

old Re: Wait function

Hurri04
Super User Off Offline

Quote
actually, you dont need to reduce the amount of events in this case since it's still only one event at a time that is triggered, either removing_branch or removing_leaf, so technically it wouldnt result in a better performance.

though, if you want to keep the script small, the script you wrote first should work if you write this line before the process command line:
1
$object_find=24;	//or 15 for the leaf
More >
To the start Previous 1 Next To the start
Log in to reply Scripts overviewStranded II overviewForums overview