I am about finished with a script I am writing but I have one last condition statement to add to my function.
fun whileloop (x:real,a:int,b:real) =
if (a<1)
then (x,a,b)
else whileloop(x+1.0,a-1,b-1.0)
This is my current loop I have created. It is basically accomplishing everything I need under one exception. I want it to exit its loop once the b variable hits zero[if this happens before a reaches zero). I believe Standard ML will not let me do a condition statement for a real variable...such as b<1.0. just to give you an idea of what I am trying to accomplish...I want the following code to work below:
fun whileloop (x:real,a:int,b:real) =
if (a<1 or b<1.0)
then (x,a,b)
else whileloop(x+1.0,a-1,b-1.0)
of course this code does not work due to the syntax and a condition statement being checked against a real number...but how could I accomplish this task while keeping my skeleton somewhat intact. I simply want to add another if condition statement to the existing skeleton. In C++ this was a fairly simple task.
Here is the answer. Thanks to John Coleman.
fun whileloop (x:real,a:int,b:real) =
if (a<1 orelse b<1.0)
then (x,a,b)
else whileloop(x+1.0,a-1,b-1.0)
Related
I am trying to code a gui that is dynamically assigned. I have four teams. I am getting stuck at a certain point. I want to make a function that, when a player joins the game, checks if the other teams have already scored to update their labels. It looks like this:
local function updateAllLabelsLateArrival(redPoints, bluePoints, yellowPoints, greenPoints)
game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam.Points.Text = redPoints
game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyBlueTeam.Points.Text = bluePoints
game.Players.LocalPlayer.PlayerGui.ScreenGui.NewYellerTeam.Points.Text = yellowPoints
game.Players.LocalPlayer.PlayerGui.ScreenGui.LimeGreenTeam.Points.Text = greenPoints
end
The function is remotely triggered from a server-side script when a player joins. The problem I have is that not all four labels might exist. Suppose a green team player joins in when there is only a red team player already playing. It will come back with the error
ReallyBlueTeam is not a valid member of ScreenGui
I thought wrapping each line in an if statement to check if the label exists, like so:
if game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam then game.Players.LocalPlayer.PlayerGui.ScreenGui.ReallyRedTeam.Points.Text = redPoints end
But this is giving the same error. So my question is, how do I check that a label has been created before updating the points? thanks
Assuming this is a localcsript, you can use WaitForChild() which will yield until the label has been created!
game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("ReallyRedTeam"):WaitForChild("Points").Text = redPoints
More information about WaitForChild here!
Alternatively, if you don't know for definite they will be created, you can use FindFirstChild. This won't yield.
if game.Players.LocalPlayer.PlayerGui.ScreenGui:FindFirstChild("ReallyRedTeam") then
print("it exists")
end
More information about FindFirstChild here!
Hope that helps!
If you want them all on one line each then the best to use would be FindFirstChild() as #jjwood1600 has said. I would also recommend making use of a variable to shorten your GUI paths as you can see below:
local function updateAllLabelsLateArrival(redPoints, bluePoints, yellowPoints, greenPoints)
local userGui = game.Players.LocalPlayer.PlayerGui.ScreenGui
if userGui:FindFirstChild("ReallyRedTeam") then userGui.ReallyRedTeam.Points.Text = redPoints end
if userGui:FindFirstChild("ReallyBlueTeam") then userGui.ReallyBlueTeam.Points.Text = bluePoints end
if userGui:FindFirstChild("NewYellerTeam") then userGui.NewYellerTeam.Points.Text = yellowPoints end
if userGui:FindFirstChild("LimeGreenTeam") then userGui.LimeGreenTeam.Points.Text = greenPoints end
end
In normal Lua you can indeed do the if statements the way you did where you don't use FindFirstChild but Roblox's own version RBX.Lua doesn't.
I'm trying out this code in applescript:
the first one works, the second doesn't
global theOpts
on saySomething()
--some code that it runs
end
set theOpts to {saySomething}
--the one that does
set t to theOpts's item 1
t()
--the one that doesn't
on runByIdx(idx)
set thefun to item 1 of theOpts
thefun()
end runByIdx
Is there a way I can get this to work?
What I want to do in summary is have a list of handlers that I can call by index rather than by name.
Don't do that. It's an undocumented behavior and known design flaw. Handlers aren't meant to be manipulated as objects, and it breaks the handler's bindings to the enclosing script.
The right way to do it is to wrap each handler in its own script object, and put those script objects in the list instead.
script Foo
on doIt()
say "this"
end doIt
end script
script Bar
on doIt()
say "that"
end doIt
end script
set opts to {Foo, Bar}
doIt() of item 1 of opts
Though you should also not underestimate the value of a simple if...else if... block:
if idx = 1 then
doThis()
else idx = 2 then
doThat()
else ...
Basically it depends on what problem you're trying to solve. But I'd lean towards the latter approach (i.e. KISS) unless it's a task that requires the extra flexibility, otherwise you're just adding unnecessary complexity and making work for yourself.
(FWIW, the AppleScript book I co-wrote a few years back has a chapter on working with script objects. The section on libraries doesn't cover the new library system in 10.9+, and the section on OOP has a corker of a technical error if you know where to look:p, but it's probably the best explanation of this subject you'll find so worth a look if you really want to know more.)
This way both work...
global theOpts
on saySomething()
return 1
end saySomething
set theOpts to {saySomething()}
--the one that does
set t to theOpts's item 1
--t
runByIdx(1)
on runByIdx(idx)
set thefun to item idx of theOpts
thefun
end runByIdx
I'm going totally crazy here. I'm using Visual Studio 2008, and I can't help but think it has a bug in it, or otherwise I've just forgotten some serious fundamentals.
It seems that when I remove the "initialization" (first field) of my for loop, my "post-increment" (third field) executes prior to the body of the loop.
std::list<VideoTranscoderStats>::iterator videoStatsIter = mTranscoderStatisticsList.begin();
for(;videoStatsIter != mTranscoderStatisticsList.end();videoStatsIter++)
{
// do stuff
}
As I debug, I can see that I'm executing "videoStatsIter++" before "do stuff", and also before testing the condition in the for loop.
On the other hand, if I move my initialization of videoStatsIter into the beginning of the for loop statement, everything works fine. "videoStatsIter++" is executed following the body of the for loop.
I've run into the following issue which is not difficult to solve by any stretch of the imagination but I would like to know what the best / most elegant solution is.
I have the following method that the prototype of looks like this:
bool Team::isEveryoneDead(int teamOnTurn);
There are two teams available and depending on what instance of the team is currently on turn, I would like to check whether every Character in the team is dead in this very particular order:
Loop trough all the Characters in the team that's not on turn first. Should there be any character that's alive, stop looping (and goto step 2.). Should there be noone alive, terminate the function and return.
Now that I know that the team that's not on turn contains at least one character that's alive, loop trough the team that's currently on turn and check for the same thing. Should I find someone alive, stop looping and terminate / return.
The argument int teamOnTurn allows me to resolve the instance of Team that's currently on turn. The order in which i evaluate the "alive condition" is of great importance here.
Now, there are several approaches that can be taken, say hardcoding the order (since there are only 2 possible orders) and resolving the order by checking who's on turn and then executing the branch that already has the specific order like this:
bool Team::isEveryoneDead(int teamOnTurn) {
if (Team::Blue == teamOnTurn) {
checkThis();
checkThat();
} else {
checkThat();
checkThis();
}
}
This solution however wouldn't quite work for say 5! permutations for specific call-ordering for more items. What technique should one deploy to solve this with the utmost elegance :) ?
Thanks in advance, Scarlet.
Try creating another internal method that actually does the checking, and let the isEveryoneDead() method orchestrate the order in which the teams are checked, maybe something like this:
bool Team::isEveryoneDead(int teamOnTurn) {
bool isFound = isEveryoneDeadInternal( /* params for team not on turn */ );
if(isFound) {
isFound = isEveryoneDeadInternal( /* params for team on turn */ );
}
return isFound;
}
// This method know nothing about on turn or off turn
bool Team:isEveryoneDeadInternal() {
// Loop through all characters in the team, checking if any are alive
// When the first live character is found, return true
// else return false
}
This is a concept called DRY : Dont Repeat Yourself
I'm working on a game engine in C++ using Lua for NPC behaviour. I ran into some problems during the design.
For everything that needs more than one frame for execution I wanted to use a linked list of processes (which are C++ classes). So this:
goto(point_a)
say("Oh dear, this lawn looks really scruffy!")
mowLawn()
would create a GotoProcess object, which would have a pointer to a SayProcess object, which would have a pointer to a MowLawnProcess object. These objects would be created instantly when the NPC is spawned, no further scripting needed.
The first of these objects will be updated each frame. When it's finished, it will be deleted and the next one will be used for updating.
I extended this model by a ParallelProcess which would contain multiple processes that are updated simultaneously.
I found some serious problems. Look at this example: I want a character to walk to point_a and then go berserk and just attack anybody who comes near. The script would look like that:
goto(point_a)
while true do
character = getNearestCharacterId()
attack(character)
end
That wouldn't work at all with my design. First of all, the character variable would be set at the beginning, when the character hasn't even started walking to point_a. Then, then script would continue adding AttackProcesses forever due to the while loop.
I could implement a WhileProcess for the loop and evaluate the script line by line. I doubt this would increase readability of the code though.
Is there another common approach I didn't think of to tackle this problem?
I think the approach you give loses a lot of the advantages of using a scripting language. It will break with conditionals as well as loops.
With coroutines all you really need to do is:
npc_behaviour = coroutine.create(
function()
goto(point_a)
coroutine.yield()
say("Oh dear, this lawn looks really scruffy!")
coroutine.yield()
mowLawn()
coroutine.yield()
end
)
goto, say and mowLawn return immediately but initiate the action in C++. Once C++ completes those actions it calls coroutine.resume(npc_behaviour)
To avoid all the yields you can hide them inside the goto etc. functions, or do what I do which is have a waitFor function like:
function waitFor(id)
while activeEvents[id] ~= nil do
coroutine.yield()
end
end
activeEvents is just a Lua table which keeps track of all the things which are currently in progress - so a goto will add an ID to the table when it starts, and remove it when it finishes, and then every time an action finishes, all coroutines are activated to check if the action they're waiting for is finished.
Have you looked at Finite State Machines ? If I were you I wouldn't use a linked list but a stack. I think the end result is the same.
stack:push(action:new(goto, character, point_a))
stack:push(action:new(say, character, "Oh dear, this lawn was stomped by a mammoth!"))
stack:push(action:new(mowLawn, character))
Executing the actions sequentially would give something like :
while stack.count > 0 do -- do all actions in the stack
action = stack:peek() -- gets the action on top of the stack
while action.over ~= true do -- continue action until it is done
action:execute() -- execute is what the action actually does
end
stack:pop() -- action over, remove it and proceed to next one
end
The goto and other functions would look like this :
function goto(action, character, point)
-- INSTANT MOVE YEAH
character.x = point.x
character.y = point.y
action.over = true -- set the overlying action to be over
end
function attack(action, character, target)
-- INSTANT DEATH WOOHOO
target.hp = 0
action.over = true -- attack is a punctual action
end
function berserk(action, character)
attack(action, character, getNearestCharacterId()) -- Call the underlying attack
action.over = false -- but don't set action as done !
end
So whenever you stack:push(action:new(berserk, character)) it will loop on attacking a different target every time.
I also made you a stack and action implementation in object lua here. Haven't tried it. May be bugged like hell. Good luck with your game !
I don't know the reasons behind you design, and there might be simpler / more idiomatic ways to it.
However, would writing a custom "loop" process that would somehow take a function as it's argument do the trick ?
goto(point_a)
your_loop(function ()
character = getNearestCharacterId()
attack(character)
end)
Since Lua has closures (see here in the manual), the function could be attached to your 'LoopProcess', and you call this same function at each frame. You would probably have to implement your LoopProcess so that that it's never removed from the process list ...
If you want your loop to be able to stop, it's a bit more complicated ; you would have to pass another function containing the test logic (and again, you LoopProcess would have to call this every frame, or something).
Hoping I understood your problem ...