Ok. I got bored for a moment and decided to have a looks at a simple Set/Reset logic in Binary. Then I was like, sweet, it returns a state known as Z in binary with this logic loop. So I wanted to apply it to my game. However, testing the loop in ruby first proved a problem it seemed. It was returning the 0b values (thats the prefix for boolean numbers in ruby) as negatives and Z was simply a 2. The logic was working, put proving an issue for the execution, because I had to establish a starting state for the script to function properly. So then I tried it using true and false as the only two states, and replaced the Z result with just true, because in reality, Z is on even if the logic says it shouldn't be. But I had to create some methods for dealing with true false boolean algebra. I did not create methods for XNAND, or XNOR (exclusive not-and & exclusive not-or), because they seemed redundant, you could add them if you wanted, they are just the inverse of the XAND & XOR methods.
so first we need to add the functions to the game. simply create a new script in the script editor by right clicking under Materials, above Main, you can call it whatever, I call it Boolean Algebra, because I loved Boolean Algebra as a kid.
Spoilerdef AND(val1, val2)
if val1 == val2
return true
else
return false
end
end
def OR(val1, val2)
if val1 == true || val2 == true
return true
else
return false
end
end
def NOR(val1, val2)
if val1 != true && val2 != true
return true
else
return false
end
end
def XOR(val1, val2)
if val1 != val2
return true
else
return false
end
end
def NAND(val1, val2)
if val1 != val2
return true
else
return false
end
end
def XAND(val1, val2)
if val2 != val1
return false
else
return true
end
end
def NOT(val)
if val == true
return false
else
return true
end
end
just CTRL+C-V into the new script window, the big blank screen which should just have the number 1 on the left side of that input window.
so there are many types of maths we can do with this, but I was interested in the Set/Reset operators, which for all purposes is two Nor gates in a loop (don't freak out, it wont put the game in an endless loop, and I'll post a video of the result if you like). You can do J/K Memory Loops, but they are more complex. Here is the simple Set/Reset:
Spoiler
Pretty basic right? That is how the first ram was made. For the purpose of this tutorial, we shall call Reset => $game_switches[12] and Set => $game_switches[13]. You can use any two switches you want, just remember the id numbers, or you will have problems later on when we use our methods on them. And Q => Q1 or $game_switches[14] and Q with the line => Q2 or $game_switches[15]. we will discuss why this is later.
Now I tested this set up before implementing it in the game, to see for certain what output I needed to pay attention to for best results. If all the inputs, including the closed loop, is false, the result is Q1 = true & Q2 = false. Where Q1 is merely a reset function, and Q2 is the memory we want to use for the game. So if Q1 is true, we don't want to display anything. but if Q2 is true, then we have memory. because Q2 can be true, and Q1 be false, if we fan-angle with the switches. You could do up a binary chart to see how it works:
Spoiler$g_s:x = $game_switches[x]
$g_s:12 | $g_s:13 | $g_s:14 | g_s:15
0 0 1 0
1 1 0 0
0 1 1 0
1 0 0 1*
* this is that Z state I was refering to, the memory has been set.
Next thing to do is to set up the RAM (Set/Reset). We aren't going to make it a method, because there doesn't appear to be a straightforward logic method available to use for dealing with it, and would require a lot of if then statements, or using the methods already mentioned prior. also, the $game_switches do not mean anything when the game is started to title screen, and would give us an error message when the engine executed the script with the method, if we did not include a condition for only having the method exist during game play. Now I don't know enough about ACE to do this yet, without messing with the core scripts, so let's not do that, OK?
If you have a map already made, click somewhere you don't mind having a hidden event, and create a new event. Set it to above the player, and parallel. Add a new Event Command, 3 -> Script. and add the following in the script window:
SpoilerQ1 = false
Q2 = false
Q1 = NOR($game_switches[12], Q2)
Q2 = NOR($game_switches[13], Q1)
$game_switches[14] = Q1
$game_switches[15] = Q2
this should go inside the same event as before, and can be separate or with the prior script. I personally prefer it separate because it looks nicer to me, I imagine so many will be on my case about that.
now place two switches on the map. create an event and click on the image to display, and try to find the switches, I used buttons. to make them a toggle switch seemed the easiest to implement, however you could give the player options to turn the switch on or off, but I like toggles. be sure to set trigger to action button and priority to same as characters (its really depth on Z plane, but they call it priority).
Spoiler
coming soon: A tutorial on making our first boolean computer using simple interpreter logic, and a eight bit display
you want to make another switch somewhere else, and set it up for $game_switches[12]. I named my two input switches Test-Ram-# to remember them easily.
finally, the difficult part. I could not figure out easily, how to create an event for the output, because it gives two results. However, if it were a LED display, you would be able to display an L or something. So I thought of it like that. Q2 is the result we want to activate a fire. I made a simple graphic for that, its just three circles, I use them in the game as flames for a pillar.
create an event somewhere, and set it as above character if you want, and leave everything else as is. add the graphic you want to display, and on page 1 under conditions, select switch 14 is on. make another page, leave it blank, and select switch 15 is on.
now you could create a third event for when both 14 and 15 are on, but I have not.
I will post a video showing what we have made in a while, it is getting late, and this took more time than I thought.
So why do this? Well, you can make more complex switching mechanics in your game, that leave the player wondering how to get results. You can use as many inputs and outputs as you desire, but it does help to make up a flow chart and boolean table to see if you get the results you desire. Applying boolean algebra can help increase game mechanics complexity a bit, and keep the game from being too easy.
Anyways, 1001001
the video as promised, with narration:
https://youtu.be/BwWZZtLQHAg
we assign Q1 and Q2 for this event, so that if we decide to add a conditional or eliminate them later on, the event doesn't interfere with the results. Ace sandboxes the scripts in events, so Q1 and Q2 can't be accessed once the event is done executing, or no longer exists on the screen. so to use it with another event, add the following:
本贴来自国际rpgmaker官方论坛作者:chungsie处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:
https://forums.rpgmakerweb.com/threads/doing-a-simple-set-reset-latch-using-boolean-algebra-aka-virtual-memory.61361/