Page 1 of 1

Custom If Statements

Posted: Sun Oct 12, 2014 10:50 pm
by evenmonkeys
I'm creating some custom lua commands and need some help. I'm trying to figure out how to determine if a player meets certain conditions. For instance, is player's level more than x level... and is player's HP = 100%..

Code: Select all

---------------------------------------------------------------------------------------------------
-- func: teledem
-- auth: sarpixel
-- desc: Teleports a player to Dem Crag
---------------------------------------------------------------------------------------------------

cmdprops =
{
    permission = 0,
    parameters = "iiii"
};

function onTrigger(player, x, y, z, zone)
     if (player:level > x) then                        <-------------- HOW DO I MAKE THIS WORK?
          if (player:haskeyitem(x) == true) then
               player:setPos('0', '219.774', '19.567', '307.961', '108');
          else
               player:printtoplayer("You are not able to perform this action as you do not possess the Dem Gate Crystal.");
          end
     else
          player:printtoplayer("Your level is not high enough to perform this action.");
     end
end

Also, there isn't some sort of help guide.. or list of all sorts of commands? Like.. different kinds of if statements.. or whatever?

Re: Custom If Statements

Posted: Sun Oct 12, 2014 11:38 pm
by atom0s
If statements are generic to the language, not to DSP. You are just writing a conditional check for if statments.

Code: Select all

if (something) then
   do something
end
For a list of what you can access you can refer to the .cpp/.h files like:
src\map\lua\lua_baseentity.h
src\map\lua\lua_baseentity.cpp

What you need for what you are doing would be:

Code: Select all

if (player:getMainLvl() > x) then
However, it wont work the way you want it to in this regard since it would assume the player using the command is going to type something like:
@teledem 1

That way the if-check is seeing if they are higher than level 1. If you want to restrict the command to certain levels do something like this:

Code: Select all

---------------------------------------------------------------------------------------------------
-- func: teledem
-- auth: sarpixel
-- desc: Teleports a player to Dem Crag
---------------------------------------------------------------------------------------------------

require("scripts/globals/keyitems");

cmdprops =
{
    permission = 0,
    parameters = ""
};

function onTrigger(player, x, y, z, zone)
    if (player:getMainLvl() < 30) then
        player:PrintToPlayer('Your level is not high enough to perform this action.');
    else
        if (player:hasKeyItem(DEM_GATE_CRYSTAL)) then
            player:setPos(0, 219.774, 19.567, 307.961, 108);
        else
            player:PrintToPlayer('You are not able to perform this action as you do not possess the Dem Gate Crystal.');
        end
    end
end
Some stuff to also keep in mind:
- Lua is case-sensitive so be sure to use the proper casing for functions, objects, etc.
- Be sure to check what kind of arguments the functions expect, your setPos was trying to use strings. The function expects numbers.

And feel free to join our IRC channel for more help if you have other questions. :)

irc.rizon.net
#darkstarproject

Re: Custom If Statements

Posted: Mon Oct 13, 2014 12:07 am
by evenmonkeys
You rock. Thank you very much. I'm having more fun diving further into all of this.

Update: So awesome! I've already created a bunch more with the info from those files. Thank you.

Re: Custom If Statements

Posted: Mon Oct 13, 2014 10:40 pm
by evenmonkeys
Figured I'd add to my own.. I've been trying to rip apart other files to figure this out.. but I can't figure it out.

How can I create a choice in the game? Essentially something like:

"Are you sure you want to perform that action?"
- Yes
- No

Re: Custom If Statements

Posted: Mon Oct 13, 2014 10:41 pm
by kjLotus
you can't

Re: Custom If Statements

Posted: Mon Oct 13, 2014 10:44 pm
by evenmonkeys
Awww- really? Sad days. :(

Re: Custom If Statements

Posted: Mon Oct 13, 2014 11:33 pm
by atom0s
Dialog menus like that are hard-coded event ids which are somewhat client sided. So there is no option to generate custom ones sadly. (Other than the GM tell version of it but yea.. nothing for general quests.)

Re: Custom If Statements

Posted: Mon Oct 13, 2014 11:52 pm
by evenmonkeys
I made my own version of a "choice." I wanted to make a quick fix for someone if they get stuck and this should do fine for now. It works. Goes without saying that this isn't the perfect solution every time.. but is for some.

Code: Select all

---------------------------------------------------------------------------------------------------
-- func: stuck
-- auth: sarpixel
-- desc: Sets player status to KO'd.
---------------------------------------------------------------------------------------------------

cmdprops =
{
    permission = 0,
    parameters = "ss"
};

function onTrigger(player, stuck)

	-- Get Confirmation
	if (stuck == nil) then
		player:PrintToPlayer("Please confirm your request by typing the following command. This will KO you and you may return to your home point.\r\nCommand:: @stuck confirm");
		return;

	end

	-- PERFORM COMMAND
	if (stuck == 'confirm') then
		player:setHP(0);
		player:PrintToPlayer("You may now return to your home point.");

	-- COMMAND IS NOT CONFIRMED
	elseif (stuck ~= "confirm") then
		player:PrintToPlayer("Your request was not confirmed. Please type the following command for help.\r\nCommand:: @stuck help");

        end

end;
And ultimately.. I'm just playing around with all of this stuff because it's how I learn. :p

Re: Custom If Statements

Posted: Tue Oct 14, 2014 12:16 am
by atom0s
Not a problem to do it like that, it just wont be that customizable in the sense of being reusable for other stuff. But yea that should work.