init script?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

init script?

Post 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
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: init script?

Post by TheMeq »

Use hump gamestates, it's a live saver for things like this.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: init script?

Post 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.
twitter | steam | indieDB

Check out quadrant on Steam!
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: init script?

Post 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?
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: init script?

Post by TheMeq »

Just questioning why, when there is a library that handles enter, exit, load, draw, the works, like hump ;-)
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: init script?

Post 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.
twitter | steam | indieDB

Check out quadrant on Steam!
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: init script?

Post 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?
User avatar
TheMeq
Citizen
Posts: 56
Joined: Fri Sep 02, 2011 9:56 pm
Location: Nottingham, UK

Re: init script?

Post 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
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: init script?

Post 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.
twitter | steam | indieDB

Check out quadrant on Steam!
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: init script?

Post 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
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests