SRL Archive - Jr. Member General Help and Discussion

Help me simplify this


Pakyakkistan

2014-03-11 17:08:14

I modified this function from the includes, and call for it in my hopping procedure. I feel like I can simplify this with 'for' loop? Array? Getting to be arrays and 'for' loops that I cannot seem to grasp yet…

If someone would be so kind as to help, or to explain to me how to go about it, I would much appreciate it.


//Uses const values to choose a world based on the current hop rotations
function RandomWorldSpEx(WorldChoice1, WorldChoice2, WorldChoice3, WorldChoice4 : Integer):Integer;
begin
If Hops = 0 then//This can be removed if Hops +1 is called before this function is called
begin
Result := WorldChoice4;
end else;
If Hops = 1 then
begin
Result := WorldChoice1;
end else;
If Hops = 2 then
begin
Result := WorldChoice2;
end else;
If Hops = 3 then
begin
Result := WorldChoice3;
end else
If Hops = 4 then
begin
Result := WorldChoice4;
Hops := 0;//After WorldChoice4, we then restart at WorldChoice1
//Hops integer needs to be added after this function is called so you don't Worldchoice4 x2
//Unless Hops = 0 if loop is removed and Hops +1 is called prior to this being called
end else;
end;

rj

2014-03-11 17:17:58

Could use a simple case statement:

function RandomWorldSpEx(WorldChoice1, WorldChoice2, WorldChoice3, WorldChoice4 : Integer):Integer;
begin
case Hops of
0: Result := WorldChoice4;
1: Result := WorldChoice1;
2: Result := WorldChoice2;
3: Result := WorldChoice3;
4:
begin
Result := WorldChoice4;
hops := 0;
end else
writeln('Hops was not 0 , 1 , 2 , 3 or 4');
end;
end;

Pakyakkistan

2014-03-11 17:31:14

From: Officer Barbrady
Could use a simple case statement:

function RandomWorldSpEx(WorldChoice1, WorldChoice2, WorldChoice3, WorldChoice4 : Integer):Integer;
begin
case Hops of
0: Result := WorldChoice4;
1: Result := WorldChoice1;
2: Result := WorldChoice2;
3: Result := WorldChoice3;
4:
begin
Result := WorldChoice4;
hops := 0;
end else
writeln('Hops was not 0 , 1 , 2 , 3 or 4');
end;
end;


Totally skipped cases in Coh3ns tutorial, had to go over them so I even understood what was going on (used them before but never like this).
Thank you Officer.

rj

2014-03-11 17:37:45

From: Pakyakkistan
Totally skipped cases in Coh3ns tutorial, had to go over them so I even understood what was going on (used them before but never like this).
Thank you Officer.


No Problem, if you really want to shorten it down…
function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
var
i:integer;
begin
for i := 1 to 4 do
if (hops = i) then
exit(choice[i - 1])
else if (i = 4) then
begin
hops := 0;
exit(choice[3]);
end;
end;

Is this function supposed to choose a random world from the entered parameters?

Pakyakkistan

2014-03-11 18:03:56

From: Officer Barbrady
No Problem, if you really want to shorten it down…
function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
var
i:integer;
begin
for i := 1 to 4 do
if (hops = i) then
exit(choice[i - 1])
else if (i = 4) then
begin
hops := 0;
exit(choice[3]);
end;
end;

Is this function supposed to choose a random world from the entered parameters?


Now you're getting me somewhere(Getting me further and touching on things that confuse the hell out of me). Was going to ask about array option… Now time for the questions!

(choice:array[0..3] of integer)
I'm assuming this would then make it have a variable amount of choices? This would require calling the array of choices prior, right?


Exit(Choice[i - 1];//Exit is used as result? And what does [i - 1] mean? Subtracting 1 from i/hops variable?

And yes and no, uses worlds specified in the const values to hop in a cycle from 1-4. (one I posted and the case one you showed me anyway, IDK the differences between this one and the last tho (arrays flipping confuse me, make me feel like things are changing)

rj

2014-03-11 19:44:10

From: Pakyakkistan
Now you're getting me somewhere(Getting me further and touching on things that confuse the hell out of me). Was going to ask about array option… Now time for the questions!

(choice:array[0..3] of integer)
I'm assuming this would then make it have a variable amount of choices? This would require calling the array of choices prior, right?


Exit(Choice[i - 1];//Exit is used as result? And what does [i - 1] mean? Subtracting 1 from i/hops variable?

And yes and no, uses worlds specified in the const values to hop in a cycle from 1-4. (one I posted and the case one you showed me anyway, IDK the differences between this one and the last tho (arrays flipping confuse me, make me feel like things are changing)


choice is simply just an array of integers, very confusing when you first learn them


For the second question, exit is like result, except it exits the function and sets the result:

function func5:integer;
begin
result := 5;
if (blah blah blah) then
doSomething;
end;

function func6:integer;
begin
exit(5);
// code after this line is unreachable because of the exit
end;

Unfortunately, the reflection include is current written in pascalscript so this feature isn't available, so you will have to stick with result and cannot put any parameters inside exit:

[spoiler=pascalscript Version]
function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
var
i:integer;
begin
for i := 1 to 4 do
if (hops = i) then
begin
result := choice[i - 1];
exit;
end
else if (i = 4) then
begin
hops := 0;
result := choice[3];
exit;
end;
end;
[/spoiler]

Now for the array part, an array as I said above is just a list of numbers. To referense a item in the last you put the variable name followed by brackets. Inside the brackets you put the index (or position) in the list you want, starting from 0.

Example:

var
choices:array[0..3] of integer;

begin
choices := [0, 4, 5, 3];
writeln(choices[0]);
writeln(choices[1]);
writeln(choices[2]);
end.

what array[0..3] does is set the length of the array to 4. You CANNOT change the length of the array if you do this. If you try to access something higher then the length, you will get a run time range check error (followed by the termination of the script).
This can also be done like this:

var
choices:array of integer;

begin
choices := [0, 4, 5, 3];
writeln(choices[0]);
writeln(choices[1]);
writeln(choices[2]);
end.

Except here you will notice that instead of array[0..3] I just declared choices as an array of a integer. This means it can be any length. After the variable is declared, if you want to add or remove any of the compenents you will have to change the length of the array like so:

setLength(choices, 5);

If you do not put anything in the 4th index (4th index = 5th position… which means choices[4] I know confusing right?) then it's value will be 0 (and if it is a string then the value will be '' (blank string) and if it's a boolean it would be false)


Now on to why I did choice

In the for loop, I stated

for i := 1 to 4 do

As I said, array index's start from 0, and i started at 1. This means that I must subtract 1 any time I want to reference the array, or else a wrong value would be given (or a range check error)
The reason I did 1 to 4 is so it is easier to understand in this case. It could also be done like this:

  for i := 0 to 3 do

And then I would no longer have to subtract 1 each time I want to get the value of a index in the array. (remember 0 to 3 = 4 loops!)


So I hope that gave you a basic understanding of the code lol

Pakyakkistan

2014-03-11 20:15:20

From: Officer Barbrady

So I hope that gave you a basic understanding of the code lol


Exactly what I needed. Thank you very much for all your help!

Brandon

2014-03-11 20:47:31

@Officer Barbrady; Get rid of the for loop.. There is no need to iterate the entire array to check if hops == i..


function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
begin
result := 0;
if ((hops > 0) and (hops < high(choice))) then
result := choice[hops - 1]
end;

rj

2014-03-11 20:56:33

From: Brandon
@Officer Barbrady; Get rid of the for loop.. There is no need to iterate the entire array to check if hops == i..


function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
begin
result := 0;
if ((hops > 0) and (hops < high(choice))) then
result := choice[hops - 1]
end;

lol what was i thinking

Pakyakkistan

2014-03-11 22:04:46

From: Brandon
@Officer Barbrady; Get rid of the for loop.. There is no need to iterate the entire array to check if hops == i..


function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
begin
result := 0;
if ((hops > 0) and (hops < high(choice))) then
result := choice[hops - 1]
end;


That makes sense… Could you call
if((hops > -1) and (hops < high(choice)))
?

masterBB

2014-03-12 11:22:45

From: Pakyakkistan
That makes sense… Could you call
if((hops > -1) and (hops < high(choice)))
?


That is possible. Or:

if((hops >= 0) and (hops < high(choice)))

Looks neater imo

Everyone removed the resseting of the hops though:

function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
begin
hops := hops mod 4;
result := choice[hops]
end;

e:
or if you don't care about the hops value:
function RandomWorldSpEx(choice:array[0..3] of integer):Integer;
begin
result := choice[hops mod 4]
end;

e: more foolproof
function RandomWorldSpEx(choice:array of integer):Integer;
begin
result := choice[hops mod Length(choice)]
end;

Maybe make the word choice plural?

Pakyakkistan

2014-03-12 11:59:10

From: masterBB

e: more foolproof
function RandomWorldSpEx(choice:array of integer):Integer;
begin
result := choice[hops mod Length(choice)]
end;

Maybe make the word choice plural?


what does 'Mod length' do?

Hops must be reset or it won't cycle. It would just, hop 4 times, and then the script would basically break. Resetting of hops would just be

If (hops = high(choice)) then
hops = 0;
Right? (hops only is 0 when the script starts, since I don't have it add a world hop until its actually hopped, that is why 0 = Choice 4, the end of the world cycle)

What do you mean make the world choice plural? :confused:

E: Also, if I use an array for choices, would this allow me call as many world choices as I wish in my cycle? Where do I need to declare the choice array? In its own function? Constants?

masterBB

2014-03-12 12:42:18

From: Pakyakkistan
what does 'Mod length' do?

Hops must be reset or it won't cycle. It would just, hop 4 times, and then the script would basically break. Resetting of hops would just be

If (hops = high(choice)) then
hops = 0;
Right? (hops only is 0 when the script starts, since I don't have it add a world hop until its actually hopped, that is why 0 = Choice 4, the end of the world cycle)

What do you mean make the world choice plural? :confused:

E: Also, if I use an array for choices, would this allow me call as many world choices as I wish in my cycle? Where do I need to declare the choice array? In its own function? Constants?


Mod is like when you learned to divide you often had bits left.
7 / 4 = 1 and 3 left
20 / 3 = 6 and 2 left
It is that bit which is left. So hops mod 4 would return:
[INDENT][table="width: 500"]
[tr]
[td]hops[/td]
[td]hops mod 4[/td]

[/tr]
[tr]
[td]1[/td]
[td]1[/td]
[/tr]
[tr]
[td]2[/td]
[td]2[/td]
[/tr]
[tr]
[td]3[/td]
[td]3[/td]
[/tr]
[tr]
[td]4[/td]
[td]0[/td]
[/tr]
[tr]
[td]5[/td]
[td]1[/td]
[/tr]
[tr]
[td]6[/td]
[td]2[/td]
[/tr]
[tr]
[td]7[/td]
[td]3[/td]
[/tr]
[tr]
[td]8[/td]
[td]0[/td]
[/tr]
[/table][/INDENT]

So it would work without resetting. Though if you want to reset it you can use:
hops := hops mod 4

The plural of choice is choices, to indicate the variable/parameter got more then one value.

e:
Yes, you can use as much worlds as you pass to the function:
RandomWorldSpEx([200, 201, 202, 205, 209]);

Pakyakkistan

2014-03-12 13:04:08

From: masterBB
Mod is like when you learned to divide you often had bits left.
7 / 4 = 1 and 3 left
20 / 3 = 6 and 2 left
It is that bit which is left. So hops mod 4 would return:
[INDENT][table="width: 500"]
[tr]
[td]hops[/td]
[td]hops mod 4[/td]

[/tr]
[tr]
[td]1[/td]
[td]1[/td]
[/tr]
[tr]
[td]2[/td]
[td]2[/td]
[/tr]
[tr]
[td]3[/td]
[td]3[/td]
[/tr]
[tr]
[td]4[/td]
[td]0[/td]
[/tr]
[tr]
[td]5[/td]
[td]1[/td]
[/tr]
[tr]
[td]6[/td]
[td]2[/td]
[/tr]
[tr]
[td]7[/td]
[td]3[/td]
[/tr]
[tr]
[td]8[/td]
[td]0[/td]
[/tr]
[/table][/INDENT]

So it would work without resetting. Though if you want to reset it you can use:
hops := hops mod 4

The plural of choice is choices, to indicate the variable/parameter got more then one value.

e:
Yes, you can use as much worlds as you pass to the function:
RandomWorldSpEx([200, 201, 202, 205, 209]);


Oh, well then in that case I like the 'mod' 4 idea. Sweet man, thanks for the knowledge and the help! Will be implementing an array into my world function, I like them now that I know how they work! Lol

Pakyakkistan

2014-03-12 15:49:26

This is what I ended up coming up with

function RandomWorldSpEx(WArray: TIntegerArray):Integer;
begin
If hops = NumOfWorlds then Hops := 0;
if ((hops >= Low(WArray)) and (hops <= high(WArray)+1)) then
result := WArray[hops-1];
end;