Welcome to the Builder Academy

Question Question on Global Vars

More
15 Jul 2016 00:50 #6111 by Ocam
Question on Global Vars was created by Ocam
Trying to avoid using remote %actor.id%... and instead global some variables.

The code below is used in several rooms, which tracks the direction a player enters a room.
I'm using a variable to track the players movement through the rooms. When entering from different directions, the variable gets added/multiplied in different ways. This way, when a specific route is taken, it will result in a specific mathematical value. So, in the code below, when the value reaches 151, i know the player took the correct route, and a reward is given.

However, the trigger is loading a different instance of the variable for EACH room, not just one variable globally. It works fine if I remote the variable, the concept is sound. But I cannot seem to make it work with globals, no matter the context.


if %actor.is_pc%
wait 1 s
context 1
if %direction% == north
*entering from the north, southbound leaving previous room
eval steamworks_dungeon %steamworks_dungeon% + 3
global steamworks_dungeon
end
if %direction% == east
*entering from the east, westbound leaving previous room
eval steamworks_dungeon %steamworks_dungeon% + 1
global steamworks_dungeon
end
if %direction% == south
*entering from the south, northbound leaving previous room
eval steamworks_dungeon %steamworks_dungeon% * 2
global steamworks_dungeon
end
if %direction% == west
*entering from the west, eastbound leaving previous room
eval steamworks_dungeon %steamworks_dungeon% + 2
global steamworks_dungeon
end
%echo% dungeon level w/actor = %actor.steamworks_dungeon_level%
%echo% dungeon direction counter w/actor = %actor.steamworks_dungeon%
%echo% dungeon level = %steamworks_dungeon_level%
%echo% dungeon direction counter = %steamworks_dungeon%
if %steamworks_dungeon% == 151
%teleport% %actor% 32528
%echo% The floor gives way and you fall into a pit below.
%force% %actor% look
eval steamworks_dungeon_level %steamworks_dungeon_level% + 1
global steamworks_dungeon_level
*choose the appropriate mob difficulty based on the level reached
if %steamworks_dungeon_level% == 2
eval dungeon_mob 32505

[ Return to continue, (q)uit, (r)efresh, (b)ack, or page number (1/2) ]
elseif %steamworks_dungeon_level% == 3
eval dungeon_mob 32506
elseif %steamworks_dungeon_level% == 4
eval dungeon_mob 32507
elseif %steamworks_dungeon_level% == 5
eval dungeon_mob 32508
elseif %steamworks_dungeon_level% == 6
eval dungeon_mob 32509
elseif %steamworks_dungeon_level% > 6
eval dungeon_mob 32510
end
%at% 32528 %purge%
%at% 32529 %purge%
%at% 32530 %purge%
%at% 32531 %purge%
%at% 32532 %purge%
%at% 32533 %purge%
%at% 32534 %purge%
%at% 32535 %purge%
%at% 32536 %purge%
eval mob_loader 0
while %mob_loader% < 10
%at% 32528 %load% mob %dungeon_mob%
%at% 32529 %load% mob %dungeon_mob%
%at% 32530 %load% mob %dungeon_mob%
%at% 32531 %load% mob %dungeon_mob%
%at% 32532 %load% mob %dungeon_mob%
%at% 32533 %load% mob %dungeon_mob%
%at% 32534 %load% mob %dungeon_mob%
%at% 32535 %load% mob %dungeon_mob%
%at% 32536 %load% mob %dungeon_mob%
eval mob_loader %mob_loader% + 1
done
if %steamworks_dungeon_level% > 19
%at% 32536 %load% mob 32523
end
end
end
context 0

Please Log in or Create an account to join the conversation.

More
16 Jul 2016 21:47 #6112 by Parnassus
Replied by Parnassus on topic Question on Global Vars
I'm a little rusty, haven't been around much, so my answer might not be accurate.

Is there a reason you prefer to global than remote? If I remember correctly, remote puts the information on the player so everything adds to the same variable. Global puts the information on the instance of the trigger that is setting it. Because of this, each room that is entered will have the global with the information that relates to that particular entrance. Unless an entrance can add up to 151 or the person enters the room often enough to add up to 151, nothing should happen. Is this what you're finding?

If you have something that the person is going to be carrying around, you can put the global on that and, just like the remote, the numbers should add up correctly.

If you're worried about keeping the information on the person's profile, look into rdelete. You can seen Vatiken using it in this thread about globals and remotes .

Other notes:
these lines: eval dungeon_mob 3250x
I'd change the eval to set.
Fizban talks about eval vs set here .

Also, I haven't tried to work out the numbers but this line:
if %steamworks_dungeon% == 151
Wouldn't >= be better? Is there a possibility that the numbers will add up to 152? You've said a specific route but what happens if the person takes a few mis-steps and backtracks?

Please Log in or Create an account to join the conversation.

More
16 Jul 2016 22:34 #6113 by Ocam
Replied by Ocam on topic Question on Global Vars
Parnassus

Thanks for the reply. My troubleshooting results have matched exactly what you say in paragraph two. Each room/instance of the trigger has its own value, rendering my process unusable with globals.

To answer your questions:
I avoided remote because I thought I could use globals to avoid tacking more stored variables onto a players pfile (or wherever remotes are stored). But this was my misunderstanding of the actual use of globals.
Instead, I will use remote and study up on rdelete.


Next, eval vs. set is a bit confusing in the help files.
At its simplest, set seems to be used when I just want to assign a value to a variable, while eval is similar but will have some mathematical expression to calculate. But the help file left me a bit confused.

From the SET help file:
- Sets the variable foobar to the string "15 - 5"
- set result %self.hitp% * 100 / %self.maxhitp%
- The %result% var now works as a 'function' every time it is accessed.
- Set differs in that the expression is not evaluated until the variable is accessed.

Those four statements left me a bit confused as to how set really works. It seems to work with integer values, yet the first line makes it a STRING? It also somehow works as a function? And when is the calculation performed? Is it performed when I declare the Set? Or is it performed later, when the variable is accessed? If it is performed later when the variable is accessed (as it seems obvious), then it would seem that the Set command always creates Functions, and only Functions, it doesn't actually set values. The functions may be simple, like: set number 5. Or more complex, like: set number %actor.hitp% * 2. But it would appear to be a function, not an evaluation or a declaration of variable value. Is this accurate?

Fizban's examples make it clearer, thanks.

Bottom line: used eval in a sloppy way, because of a misunderstanding with set.


Finally, I settled on "== 151" because I wanted a very precise route. Above 151, could just be the result of roaming around long enough, which I didn't want to reward. I also added a separate trigger that zeroes the value if they leave the area.

Thanks again for all your assistance.

Please Log in or Create an account to join the conversation.

Time to create page: 0.232 seconds