Page 1 of 2
init script?
Posted: Thu Oct 29, 2015 8:58 pm
by Shadowblitz16
is there a init state for scripts?
I know you can do function love.load()
but what if I have a script that triggers after the game has started and done some things?
is there a way to init a script when it gets triggered for the first time?
for example
if I started at my menu state and I wanted to set a uncreated value once when the game enters the game state
Re: init script?
Posted: Thu Oct 29, 2015 9:04 pm
by TheMeq
Use hump gamestates, it's a live saver for things like this.
Re: init script?
Posted: Thu Oct 29, 2015 9:43 pm
by undef
You can check if a file (save data for example) exists in love.load using love.filesystem.isFile and if it doesn't you run your init script.
In your init script you create that file to make sure it only happens once.
Re: init script?
Posted: Thu Oct 29, 2015 10:16 pm
by Shadowblitz16
well I was going to do somethinglike states
and I had an idea to make my own state system
for example lets say my defaultState is "game.update.damaged"
I could use a variety of different functions to do things like
enterState("1")
which would put me in state "game.update.damaged.1"
exitState()
which would put me in "game.update"
switchState("heal")
which would put me in "game.update.heal"
changeState("menu.update")
which would put me in "menu.update"
getFullState()
which would return full state "game.update.damaged"
getPartState()
which would return "damaged"
but I'm not sure how to do things like exitState I would need to be able to split the string between the last "." and return the left string
can you guys explain how to do that?
Re: init script?
Posted: Thu Oct 29, 2015 10:38 pm
by TheMeq
Just questioning why, when there is a library that handles enter, exit, load, draw, the works, like hump ;-)
Re: init script?
Posted: Thu Oct 29, 2015 11:23 pm
by undef
Ok, I hope this example helps
Code: Select all
local gameStates = {}
gameStates.menu = {
update = function( dt ) --[[<...>]] end,
draw = function() --[[<...>]] end,
}
gameStates.gameLoop = {
update = function( dt ) --[[<...>]] end,
draw = function() --[[<...>]] end,
}
local state
function setState( state )
state = gameStates[state]
end
function love.load()
setState"menu"
end
function love.update( dt )
state.update( dt )
end
function love.draw()
state.draw()
end
local keys = {
left = function() setState"menu",
right = function() setState"gameLoop",
}
function love.keypressed( k )
local action = keys[k]
if action then return action() end
end
If you read this and try to figure out how it works you should figure out how to write a state handler yourself.
Play around with this example a bit (write your own draw functions for the states, for example).
If you get stuck ask again.
If you just want to get things done, consider using a libary like TheMeq suggested.
I recommend you try to do it without a libary for now, knowing how to do this yourself will help you in the future.
Re: init script?
Posted: Fri Oct 30, 2015 12:31 am
by Shadowblitz16
@undef
ya I have no idea I only want to perform certain code when the states are right
I thought my idea was better
anyways is there a way to split strings in two at a certain point and then return one of them?
Re: init script?
Posted: Fri Oct 30, 2015 12:45 pm
by TheMeq
You want string manipulation:
http://www.lua.org/manual/2.4/node22.html
specifically, strsub:
Code: Select all
strsub (s, i, [j])
Returns another string, which is a substring of s , starting at i and runing until j . If j is absent, it is assumed to be equal to the length of s . Particularly, the call strsub(s,1,j) returns a prefix of s with length j , while the call strsub(s,i) returns a suffix of s , starting at i .
Code: Select all
a = strsub("Hello, this is a test",0,5)
print(a) = "Hello"
Alternatively, you can use an explode function if you want to split when a certain character is occured, like comma seperated values.
Code: Select all
function explode(div,str) -- credit: http://richard.warburton.it
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
Re: init script?
Posted: Sat Oct 31, 2015 12:18 am
by undef
@Shadowblitz16
Well, you have to decide for yourself what you can work with best...
Shadowblitz16 wrote:is there a way to split strings in two at a certain point and then return one of them?
You should take a look at the Lua
5.1 reference manual and check out the string libary, for example
string.match.
I also recommend you look at the chapter about the string libary in Programming in Lua, particulary the part about pattern matching.
Re: init script?
Posted: Sat Oct 31, 2015 1:17 pm
by Shadowblitz16
is there a function that splits the string between the nth char?
and supports negative values?
for example
"12,3,68"
function(-1,".") = "68"
or function(1,".") = "12"
sorry there isn't really many examples on the lua 5.1 website
I learn mostly from examples