• Vote in May Mapping Competition to select a winner!
  • There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Problem with first steps of programming

SennyK

New Member
Joined
Jan 17, 2017
Messages
12
Best answers
0
Reaction score
0
Hi guys. I have problem. Im trying to write first simple script but when im try login i cant


Lua:
local config = {
    ["Monday"] = 1.0,
    ["Tuesday"] = 1.0,
    ["Wednesday"] = 1.0,
    ["Thursday"] = 1.0,
    ["Friday"] = 1.0,
    ["Saturday"] = 1.5,
    ["Sunday"] = 2.0
}

function onLogin(player)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Dostajesz dodatkowego EXP'a bo dziś jest "..os.date("%A").."!")
end

function onGainExperience(player, exp, rawExp)
  exp = exp * config[os.date("%A")]
  return exp
end
I have no errors in console but when im joining to server i have in console:
SennyK has logged in.
SennyK haslogged out.

in creaturescripts.xml i have:
<event type="login" name="TestLua" script="test.lua" />

Somebody can help me with it?

Sorry for my bad english... :c
 

roriscrave

Intermediate OT User
Joined
Dec 7, 2011
Messages
919
Best answers
23
Reaction score
114
Hi guys. I have problem. Im trying to write first simple script but when im try login i cant


Lua:
local config = {
    ["Monday"] = 1.0,
    ["Tuesday"] = 1.0,
    ["Wednesday"] = 1.0,
    ["Thursday"] = 1.0,
    ["Friday"] = 1.0,
    ["Saturday"] = 1.5,
    ["Sunday"] = 2.0
}

function onLogin(player)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Dostajesz dodatkowego EXP'a bo dziś jest "..os.date("%A").."!")
end

function onGainExperience(player, exp, rawExp)
  exp = exp * config[os.date("%A")]
  return exp
end
I have no errors in console but when im joining to server i have in console:
SennyK has logged in.
SennyK haslogged out.

in creaturescripts.xml i have:
<event type="login" name="TestLua" script="test.lua" />

Somebody can help me with it?

Sorry for my bad english... :c
you need to register in login.lua
before last return true, put
Lua:
player:registerEvent("TestLua")
 

forgee

Advanced OT User
Joined
May 9, 2011
Messages
273
Best answers
9
Reaction score
239
Location
Sweden
Your onLogin() function needs to return true.
Login events does not need to be registered in login.lua.

Experience change is in data/events/scripts/player.lua. Just add your code in the exisiting Player:onGainExperience() method.
 

Lessaire

Omniscient Hypervisor
Premium User
Joined
Dec 29, 2009
Messages
1,241
Best answers
44
Reaction score
371
Location
Oregon
trying to write first script
Lua:
function onLogin(player)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "Dostajesz dodatkowego EXP'a bo dziś jest "..os.date("%A").."!")
end
Oh good, you haven't gotten attached to a lot of bad habits yet.
As a "best practices" approach it would be ideal to make stuff like that block I quoted 👆 in this fashion instead:

Lua:
local greeting = "Dostajesz dodatkowego EXP'a bo dziś jest %s!"
function onLogin(player)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, string.format(greeting, os.date("%A")))
end
This way you can put all your language-specific stuff in a single block, so if you want to share something, it's much easier to have translated. You know since OTs are a very international thing with large English, Portuguese, Polish, Spanish parts, and many others too.

string format works almost just like the C version. When you need advanced usage you can look it up, but for now its enough to just know:
%s in the template gets replaced by provided parameter strings. If you need an actual % in the template you must use %%
Lua:
  do
    local template = "The cat goes %s, The dog goes %s, The cow goes %s... %s"
    local question = "But what does the fox say?"
    print(string.format(template, "meow", "woof", "moo", question))
    print(string.format("You need to be about %s%% cooler to use percents", "20"))
  end
-- The cat goes meow, The dog goes woof, The cow goes moo... But what does the fox say?
-- You need to be about 20% cooler to use percents
 
OP
SennyK

SennyK

New Member
Joined
Jan 17, 2017
Messages
12
Best answers
0
Reaction score
0
Code:
local config = {
    ["Monday"] = 1.0,
    ["Tuesday"] = 1.0,
    ["Wednesday"] = 2.0,
    ["Thursday"] = 1.0,
    ["Friday"] = 1.0,
    ["Saturday"] = 1.5,
    ["Sunday"] = 2.0
}

local greeting = "%s!! Your exp is multiplied by %sx"
function onLogin(player)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, string.format(greeting, os.date("%A"), config[os.date("%A")))
    return true
end

function onGainExperience(player, exp, rawExp)
  exp = exp * config[os.date("%A")]
  return exp
end

thanks guys it's working but not giving me more exp

in player.lua i have
Code:
-- Apply experience stage multiplier
    exp = exp * Game.getExperienceStage(self:getLevel())
i must to it somethink or i can change exp from my script ?


EDIT:

Ok its working but i dont know it's a good solution:
in player.lua:
Code:
exp = exp * Game.getExperienceStage(self:getLevel()) * config[os.date("%A")]
and i change array from local to global.
it can be like that ?
 
Last edited:

roriscrave

Intermediate OT User
Joined
Dec 7, 2011
Messages
919
Best answers
23
Reaction score
114
Code:
local config = {
    ["Monday"] = 1.0,
    ["Tuesday"] = 1.0,
    ["Wednesday"] = 2.0,
    ["Thursday"] = 1.0,
    ["Friday"] = 1.0,
    ["Saturday"] = 1.5,
    ["Sunday"] = 2.0
}

local greeting = "%s!! Your exp is multiplied by %sx"
function onLogin(player)
    player:sendTextMessage(MESSAGE_STATUS_DEFAULT, string.format(greeting, os.date("%A"), config[os.date("%A")))
    return true
end

function onGainExperience(player, exp, rawExp)
  exp = exp * config[os.date("%A")]
  return exp
end

thanks guys it's working but not giving me more exp

in player.lua i have
Code:
-- Apply experience stage multiplier
    exp = exp * Game.getExperienceStage(self:getLevel())
i must to it somethink or i can change exp from my script ?


EDIT:

Ok its working but i dont know it's a good solution:
in player.lua:
Code:
exp = exp * Game.getExperienceStage(self:getLevel()) * config[os.date("%A")]
and i change array from local to global.
it can be like that ?
its fine bro!
 

Lessaire

Omniscient Hypervisor
Premium User
Joined
Dec 29, 2009
Messages
1,241
Best answers
44
Reaction score
371
Location
Oregon
and i change array from local to global.
Lexical scoping considerations are something it may take you a while to fully grasp. I suggest searching the web for information on those highlighted words, and you may even be able to find it in your native language.
 
OP
SennyK

SennyK

New Member
Joined
Jan 17, 2017
Messages
12
Best answers
0
Reaction score
0
Thanks you very much guys!
 
Top